2014-01-13 1211 views
2

我有一個列表框,我想每分鐘更新一次。它拉取XML數據,解析並自動將其放入列表框中。我已經想出了使用.after方法,但是在實現它之外的類中,我嘗試使它運行後出現bug。我相信我的主要問題是沒有正確調用應用程序,但我可能是錯的。這是一些相關的代碼。使用.after方法更新Python Tkinter GUI

這是所有的主類

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' 
+0

什麼方法做'Application.getXML'? –

+0

它從站點中提取XML並解析它,但它需要傳遞一個字符串變量才能正常運行 – ddaniels

回答

2

我想你應該直接在函數調用中使用apprefresher

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() 
0

它看起來像這個問題是在這裏:

Application.entryVar(Application) 

Application是一類,而不是一個對象,所以我的猜測是,你應該在該代碼中使用Application這兩個地方的實例。