简体   繁体   English

在Python中从类外部调用方法-Tkinter事件

[英]Calling methods from outside a class in Python - Tkinter events

I want to call a method in another class and make changes to the parent class when the button is clicked in Python. 我想在另一个类中调用一个方法,并在Python中单击按钮时对父类进行更改。 To explain; 解释; With Tkinter visual programming, I click the button to change the main window and I can not change the properties in the main window with another class method. 使用Tkinter可视化编程,我单击按钮即可更改主窗口,而无法使用其他类方法更改主窗口中的属性。

I get the error message below. 我收到以下错误消息。

Exception in Tkinter callback Traceback (most recent call last): Tkinter回调Traceback中的异常(最近一次调用为last):

File "/usr/lib/python3.5/tkinter/_ init _.py", line 1553, in _ call _ _ 调用 _中的文件“ /usr/lib/python3.5/tkinter/_ init _.py”,行1553

return self.func(*args) TypeError: buton_goster() missing 1 required positional argument: 'event' 返回self.func(* args)TypeError:buton_goster()缺少1个必需的位置参数:'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()
            ...

And here is the Test Class 这是测试班

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)

The error is correct, When you do you bind, you do not pass on the event, you simply add the function pointer. 错误是正确的,当您进行绑定时,您不会传递事件,您只需添加函数指针即可。

If the buton_goster does not need "event" you should remove it or you should provide event as input when calling the function. 如果buton_goster不需要“事件”,则应将其删除,或者应在调用函数时提供事件作为输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM