python - 使用 .after 方法更新 Python Tkinter GUI

标签 python tkinter attributeerror

我有一个列表框,我想每分钟左右更新一次。它自动提取 XML 数据、解析数据并将其放入列表框中。我已经想出使用 .after 方法,但是在类之外实现它时,我在尝试使其运行时遇到了一个又一个的错误。我相信我的主要问题只是没有正确调用应用程序,但我可能是错的。这是一些相关代码。

这一切都在主类之外

def refresher(frame):
    subreddit=Application.entryVar(Application)
    Application.getXML(subreddit)
    frame.after(1000,refresher,frame)

main = tk.Tk()
main.wm_title("Readdit")
# main.geometry("350x400")
app = Application(master=main)
# Begins the applications GUI loop
# app.__init__()
refresher(main)
app.mainloop()

这里是程序的开头,也是它最终遇到所有错误的地方。

class Application(tk.Frame):

    print("what about this?")
    def __init__(self, master=None):
        self.threadTitle = tk.StringVar()
        self.threadAuth = tk.StringVar()
        self.threadPub = tk.StringVar()
        self.threadArtLink = tk.StringVar()
        self.threadLink = tk.StringVar()
        self.threadImg = tk.StringVar()
        self.threadArtLink.set('Click something to display thread info')
        self.photo = Image.open("temp.png")
        self.photo = self.photo.resize((250,250), Image.ANTIALIAS)
        self.threadImage = ImageTk.PhotoImage(self.photo)
        self.errMes = tk.StringVar()
        if not os.path.exists('Pics'):
            os.makedirs('Pics')
        # print('Something')



        # Intializes tkinter gui framework
        tk.Frame.__init__(self, master)
        # Packs widgets needed
        self.grid()
        # Creates the widgets functions
        self.createWidgets()
        # Intializes the man rss.xml
        self.initial()

    def createWidgets(self):
        # Create entrybox and align to grid
        self.send_entry = tk.Entry(self)
        self.send_entry.grid(row=0,column=0)

        # Create button,allign to grid, get xml
        self.change_sub = tk.Button(self,text='Change Subreddit',padx=5, pady=5, command=lambda :self.entryVar())

这是完整的错误

Traceback (most recent call last):
  File "S:/Projects/xmlParser.py", line 306, in <module>
    refresher(main)
  File "S:/Projects/xmlParser.py", line 296, in refresher
    subreddit=Application.entryVar(Application)
  File "S:/Projects/xmlParser.py", line 290, in entryVar
    rawInput=self.createWidgets(self).send_entry.get()
  File "S:/Projects/xmlParser.py", line 40, in createWidgets
    self.send_entry = tk.Entry(self)
  File "C:\Python33\lib\tkinter\__init__.py", line 2506, in __init__
    Widget.__init__(self, master, 'entry', cnf, kw)
  File "C:\Python33\lib\tkinter\__init__.py", line 2068, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Python33\lib\tkinter\__init__.py", line 2046, in _setup
    self.tk = master.tk
AttributeError: type object 'Application' has no attribute 'tk'

最佳答案

我认为您应该直接在对 refresher 的函数调用中使用您的 app:

def refresher(frame):
    frame.getXML()# I Don`t know what do this function, just an example
    frame.after(1000,refresher,frame)

main = tk.Tk()
main.wm_title("Readdit")
# main.geometry("350x400")
app = Application(master=main)
# Begins the applications GUI loop
# app.__init__()
refresher(app) #use app here
app.mainloop()

关于python - 使用 .after 方法更新 Python Tkinter GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21099727/

相关文章:

python - 异步超时

python - 如何制作在大字符串上迭代的类似字符串的变量?

python - 有没有办法创建一个增量大小逐渐减小的计数器?

python - getattr() 中有两个参数意味着什么?

python - 具有 0-n 关系的 Association_proxy

python - 如何找出可以在 Python 模块中导入的内容?

python-3.x - Tkinter 对象从错误的线程被垃圾收集

python - “元组”对象没有属性 'layer'

python - AttributeError: 'Model' 对象没有属性 'epoch' - Keras

python - AttributeError: 'module' 对象没有属性 '__version__'