【问题标题】:Calling methods from outside a class in Python - Tkinter events从 Python 中的类外部调用方法 - Tkinter 事件
【发布时间】: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('&lt;Button-1&gt;', Butonol.buton_goster) 更改为bind('&lt;Button-1&gt;', self.but.buton_goster)
  • “buton_ol”方法在 Butonol 类中,所以当我这样做时,我希望在 Test 类中使用“buton_goster”方法。我想从 Butonol 类中调用 buton_goster 方法。

标签: python tkinter


【解决方案1】:

错误是正确的, 当您绑定时,您不会传递事件,您只需添加函数指针。

如果 buton_goster 不需要“事件”,则应将其删除,或者在调用函数时提供事件作为输入。

【讨论】:

  • 在该方法的以下部分中,分配是作为 event.widget 进行的。删除事件参数时出现错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
相关资源
最近更新 更多