【发布时间】:2018-10-13 11:48:40
【问题描述】:
我想在 Python 中单击按钮时调用另一个类中的方法并更改父类。解释;使用 Tkinter 可视化编程,我点击按钮改变主窗口,我不能用另一个类方法改变主窗口中的属性。
我收到以下错误消息。
Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):
文件“/usr/lib/python3.5/tkinter/_ init _.py”,第 1553 行,在 _ 打电话 _
return self.func(*args) TypeError: buton_goster() missing 1 required positional argument: 'event'
class Butonol(object): #Button class
def __init__(self):
...
def buton_goster(self, event ): # Properties kisminda ozellik gosterir
Test.countshow = Test.countshow + 1;
if(Test.countshow >1):
Test.props0.pack_forget()
Test.props.pack_forget()
...
这里是测试类
class Test(Frame):
countshow = 0
...
def new_Button(self):
self.nesne = Butonol()
self.but= Button(self.mainFrame,text = self.nesne.text)
self.but.bind('<Button-1>',Butonol.buton_goster)
self.but.bind('B1-Motion>',self.label_tasi)
self.but.pack(side = LEFT,anchor = N)
Butonol.butonsay = Butonol.butonsay + 1
Butonol.butonliste.append(self.but)
【问题讨论】:
-
将
bind('<Button-1>', Butonol.buton_goster)更改为bind('<Button-1>', self.but.buton_goster)。 -
“buton_ol”方法在 Butonol 类中,所以当我这样做时,我希望在 Test 类中使用“buton_goster”方法。我想从 Butonol 类中调用 buton_goster 方法。