简体   繁体   English

尝试从wxpython中的按钮调用函数时出错

[英]Error when attempting to call function from button in wxpython

As a beginner in wxpthon, I'm creating a simple login script that creates two buttons: one to open a window for the user to create an account, and one for them to register an account. 作为wxpthon的初学者,我正在创建一个简单的登录脚本,创建两个按钮:一个用于打开一个窗口供用户创建一个帐户,另一个用于注册帐户。 My relevant code is: 我的相关代码是:

 yesbutton = wx.Button(panel, label="Yes,  I wish to log in", pos=(50,150), size=(150,60))
    self.Bind(wx.EVT_BUTTON, Login.login(Login), yesbutton)


 nobutton = wx.Button(panel, label="No,  I wish to register", pos=(270,150), size=(150,60))
    self.Bind(wx.EVT_BUTTON, Register.register(Register), nobutton)


class Login:


    def login(self):
        print("login")


class Register:

    def register(self):
        print("register")

However when I run this code i get: 但是,当我运行此代码时,我得到:

TypeError: unbound method login() must be called with Login instance as first argument (got classobj instance instead) TypeError:必须使用Login实例作为第一个参数调用unbound方法login()(获取classobj实例)

I've looked a lot for this answer, but I can't make any solutions work. 我已经看了很多这个答案,但我无法解决任何问题。 Thanks in advance. 提前致谢。

Your functions Login.login() and Register.register() take no arguments, but you're passing the Login and Register classes into them. 您的函数Login.login()Register.register()不带参数,但您将LoginRegister类传递给它们。 Your second line should instead be: 你的第二行应该是:

self.Bind(wx.EVT_BUTTON, Login.login, yesbutton)

You don't need the parentheses after Login.login in this case since it's within the Bind function. 在这种情况下,您不需要在Login.login之后使用括号,因为它在Bind函数中。 Similarly adjust your other binding. 同样调整你的其他绑定。

Edit: You also need to instantiate a Login object and a Register object before calling anything from those classes. 编辑:您还需要在从这些类调用任何内容之前实例化Login对象和Register对象。 Unfortunately I don't have access to wxPython at the moment and can't test it, but try this: 不幸的是我暂时无法访问wxPython而无法测试它,但试试这个:

Edit 2: This will also pass the event into the function, so make sure the functions you are calling account for this. 编辑2:这也会将事件传递给函数,因此请确保您调用的函数对此进行说明。

yesbutton = wx.Button(panel, label="Yes,  I wish to log in", pos=(50,150), size=(150,60))
log = Login()
self.Bind(wx.EVT_BUTTON, log.login, yesbutton)


nobutton = wx.Button(panel, label="No,  I wish to register", pos=(270,150), size=(150,60))
reg = Register()
self.Bind(wx.EVT_BUTTON, reg.register, nobutton)


class Login:

    def login(self, evt):
        print("login")

class Register:

    def register(self, evt):
        print("register")

Have you tried using the lambda function in doing this? 你尝试过使用lambda函数吗?

eg: self.Bind(wx.EVT_BUTTON, lambda: <INSTANCE OF LOGIN>.login(), yesbutton) 例如: self.Bind(wx.EVT_BUTTON, lambda: <INSTANCE OF LOGIN>.login(), yesbutton)

You would do the same thing for rthe register command 你会为rthe register命令做同样的事情

When you use the bind function, the second parameter where you pass your function is called the "handler". 使用bind函数时,传递函数的第二个参数称为“handler”。 By default, the only thing that is passed to this function is a wx Event. 默认情况下,传递给此函数的唯一内容是wx事件。 The correct syntax for doing this (using your code) would be 执行此操作(使用您的代码)的正确语法将是

self.Bind(wx.EVT_BUTTON, Login.login, yesbutton)

For future reference, if you want to call a function that does not take an event as a parameter on an Event, do this: 为了将来参考,如果要调用不将事件作为事件参数的函数,请执行以下操作:

self.Bind(wx.EVT_BUTTON, lambda event: Login.login(), yesbutton)

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

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