使用函数更改面向对象的tkinter代码

时间:2017-09-16 22:03:40

标签: python tkinter

当使用面向对象的编写代码的方式时,我似乎无法使用函数更改GUI中的按钮文本。

单击按钮后,我可以在控制台中打印它,但我无法更改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()

1 个答案:

答案 0 :(得分:0)

您需要从self

的电话中删除configure
self.button.configure(text='now1`)

此外,您无需在self.button.pack()中致电change。在这个例子中它是无害的,但是如果你有其他的小部件,那么它可能会做你不期望的事情。

相关问题