2017-09-16 70 views
0

當使用面向對象的編寫代碼的方式時,我似乎無法使用函數更改GUI中的按鈕文本。使用函數來更改面向對象的tkinter代碼

我能夠在控制檯中打印它,當我單擊按鈕但我無法更改GUI。

我可以再次以非面向對象的方式完成這項工作。

我真的很感謝所有幫助我一直停留在此的方式來長笑

感謝

from tkinter import * 
import tkinter as tk 


# tk class open 
class main(tk.Tk): 
    def __init__(self, *args): 
     tk.Tk.__init__(self, *args) 

     main.minsize(self, width=250, height=300) 

     frame = tk.Frame(self, bg='red') 
     frame.pack(side='left', fill='both', expand=True) 

     self.button = Button(frame, text='heloo', command=self.change) 
     self.button.pack() 

    def change(self): 
     self.button.configure(self, text='now1') 
     self.button.pack() 
     print('This works but does not change the button in the GUI') 

app = main() 
app.mainloop() 

回答

0

你需要從通話中刪除selfconfigure

self.button.configure(text='now1`) 

此外,您無需撥打change致電self.button.pack()。在這個例子中它是無害的,但如果你有其他的小部件,那麼它可能會做你不期望的事情。

相關問題