2016-05-16 20 views
0

所以我一直在自學自己面向對象編程的Tkinter項目,因爲我清楚地看到它們對於大量編碼更加有組織。然而,我必須承認,我一直在通過簡單地複製來自在線的各種編碼,而不是完全理解它的目的是什麼。面向對象的Tkinter編碼:改變文本和更新

這讓我覺得我的代碼根本不起作用,我不知道爲什麼不行。第一個問題是簡單地改變其他小部件的一個問題。

我有這樣的示例代碼:

import Tkinter as tk 

LARGE_FONT = ("Verdana", 12) 

class SeaofBTCapp(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     container = tk.Frame(self) 
     container.pack(side="top", fill = "both", expand = True) 
     container.grid_rowconfigure(0, weight = 1) 
     container.grid_columnconfigure(0 , weight = 1) 

     self.frames = {} 
     frame = StartPage(container, self) 
     self.frames[StartPage] = frame 

     frame.grid(row = 0, column = 0, sticky = "nsew") 

     self.show_frame(StartPage) 

    def show_frame(self, cont): 
     frame = self.frames[cont] 
     frame.tkraise() 

class StartPage(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text = "Start Page", font = LARGE_FONT) 
     label.pack(pady = 10, padx = 10) 
     button = tk.Button(self, text = "Change Label", command = self.change) 
     button.pack(pady = 10, padx = 10) 

    def change(self): 
     label["text"] = "It has changed" 

app = SeaofBTCapp() 
app.mainloop() 

這應該是一個很簡單的代碼,按下一個按鈕,標籤從「起始頁」改爲「它改變了」。但是每當我運行它時,它都會說全局變量「label」沒有被定義。此外,如果我然後將其更改爲self.label,則表明StartPage實例沒有屬性「標籤」。我不知道我做錯了什麼。

此外,以類似的方式,我正在研究一個SideBar類和一個Main類綁定到一個MainApplication類的項目。 Main類獲取一個值並將其顯示在Main類的Frame中。在此之後,SideBar中的按鈕將該值增加1.但Main顯示屏不會更新,我不知道如何將Side更新與SideBar中的按鈕綁定。

import Tkinter as tk 

something = [0, 6] 

class Main(): 
    def __init__(self, root): 
     mainboard = tk.Frame(root, height = 100, width = 100) 
     self.maincanvas = tk.Canvas(mainboard, bd = 1, bg = "white") 

     mainboard.grid(row = 0, column = 0) 
     self.maincanvas.grid(row = 0, column = 0) 

     self.maincanvas.create_text(45, 50, anchor = "center", text = str(something[1])) 

class SideBar(): 
    def __init__(self, root): 
     sidebarframe = tk.Frame(root, height = 100, width = 100) 
     button = tk.Button(sidebarframe, width = 20, text = "Change Value", command = self.add) 

     sidebarframe.grid(row = 0, column = 1) 
     button.grid(row = 0, column = 0) 

    def add(self): 
     something[1] += 1 
     print something[1] 


class MainApplication(): 
    def __init__(self, parent): 
     self.parent = parent 
     self.sidebar = SideBar(self.parent) 
     self.main = Main(self.parent) 

if __name__ == "__main__": 
    root = tk.Tk() 
    MainApplication(root) 
    root.mainloop() 

所有幫助將不勝感激,但請儘量不要用大量的專業術語,因爲我仍然在學習。

回答

0

在第一個場景替換:

label = tk.Label(self, text = "Start Page", font = LARGE_FONT) 
label.pack(pady = 10, padx = 10) 

有了:

self.label = tk.Label(self, text = "Start Page", font = LARGE_FONT) 
self.label.pack(pady = 10, padx = 10) 

以及在功能change把它像這樣:

self.label["text"] = "It has changed" 

而且在你的第二個問題,我改變了一下代碼,所以它的工作原理:

import Tkinter as tk 

something = [0, 6] 

class Main(): 
    def __init__(self, root): 
     mainboard = tk.Frame(root, height = 100, width = 100) 
     self.maincanvas = tk.Canvas(mainboard, bd = 1, bg = "white") 

     mainboard.grid(row = 0, column = 0) 
     self.maincanvas.grid(row = 0, column = 0) 

     self.maincanvas.create_text(45, 50, anchor = "center", text = str(something[1])) 

class SideBar(): 
    def __init__(self, root, main): 
     self.main = main            # Putting the main object to self.main 
     sidebarframe = tk.Frame(root, height = 100, width = 100) 
     button = tk.Button(sidebarframe, width = 20, text = "Change Value", command = self.add) 

     sidebarframe.grid(row = 0, column = 1) 
     button.grid(row = 0, column = 0) 

    def add(self): 
     something[1] += 1 
     self.main.maincanvas.delete("all")               # Removing everything from canvas 
     self.main.maincanvas.create_text(45, 50, anchor = "center", text = str(something[1]))  # Inserting the new value 
     print something[1] 


class MainApplication(): 
    def __init__(self, parent): 
     self.parent = parent 
     self.main = Main(self.parent)      # The main needs to run first 
     self.sidebar = SideBar(self.parent, self.main)  # So that SideBar can use its canvas 

if __name__ == "__main__": 
    root = tk.Tk() 
    MainApplication(root) 
    root.mainloop()