【问题标题】:Updating Python Tkinter GUI using .after method使用 .after 方法更新 Python Tkinter GUI
【发布时间】:2014-02-01 16:43:36
【问题描述】:

我有一个列表框,我想每分钟左右更新一次。它提取 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'

【问题讨论】:

  • 方法Application.getXML是做什么的?
  • 它从站点中提取 XML 并解析它,但它需要传递一个字符串变量才能正常运行

标签: python tkinter attributeerror


【解决方案1】:

我认为你应该在对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()

【讨论】:

    【解决方案2】:

    看来问题出在这里:

    Application.entryVar(Application)
    

    Application 是一个类而不是一个对象,所以我猜你应该在代码中的两个地方都使用Application 的实例。

    【讨论】:

      猜你喜欢
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多