python - 必须使用instance作为第一个参数调用unbound方法(没有任何内容)

时间:2016-03-10 10:21:24

标签: python tkinter

我正在尝试创建一个登录表单,如果用户还没有帐户,则会让用户转到注册表单。这是我的代码。

来自Tkinter import *

class myLogin():
def __init__(self):
    self.form = Tk()
    self.form.title("Log in")
    self.form.option_add("*font",("Arial",12,"normal"))
    self.form.geometry("500x400+430+200")
    self.frm = Frame(self.form, width = 700, height = 500)
    self.lab1 = Label(self.frm, text="Username:")
    self.lab1.grid(row=0,sticky=W)
    self.entr1 = Entry(self.frm, width = 15)
    self.entr1.focus_set()
    self.entr1.grid(row=0, column=1, sticky=W)

    self.lab2 = Label(self.frm, text="Password:")
    self.lab2.grid(row=1, sticky=W)

    self.entr2 = Entry(self.frm, width=15, show='*')
    self.entr2.grid(row=1, column=1, sticky=W)

    checkbox = Checkbutton(self.frm, text="Keep me logged in")
    checkbox.grid(column=1,columnspan=2)

    self.btn2 = Button(self.frm, text = "Log-in", command = self.login, width=10)
    self.btn2.grid(row=3, sticky = E, columnspan = 2)

    self.lab3 = Label(self.frm, text="Don't have an account?")
    self.lab3.grid(row=4,column=1)
    self.btn3 = Button(self.frm, text = "Register", command = mySignUp.register, width=10)
    self.btn3.grid(row=5, column = 0, columnspan = 2)

    self.frm.pack()
    self.form.mainloop()



def login(self):
    user=[]
    pw=[]
    ctr=0

    with open('users.txt', 'r') as f:
        for line in f:
            for word in line.split():
                if(ctr%2==0):
                    user.append(word)
                else:
                    pw.append(word)
                ctr+=1

    userctr=0;
    for eachuser in user:
        if((user[userctr] == self.entr1.get()) and (pw[userctr]== self.entr2.get())):
            print("ok")
            userctr+=1

class mySignUp(myLogin):
def register(self):
    #self.destroy()
    root = Tk()
    top = Toplevel(root)

    top.title("Register")
    top.geometry("500x400+430+200")

    top.transient(root)
    top.lab1 = Label(top, text="Username:")
    top.lab1.grid(row=0,sticky=W)
    top.inpusername = Entry(top, width = 15)
    top.inpusername.focus_set()
    top.inpusername.grid(row=0, column=1, sticky=W)

    top.lab2 = Label(top, text="Password:")
    top.lab2.grid(row=1, sticky=W)

    top.inppw = Entry(top, width=15, show='*')
    top.inppw.grid(row=1, column=1, sticky=W)

    top.btn2 = Button(top, text = "Sign up", command = self.addUser, width=10)
    top.btn2.grid(row=3, sticky = E, columnspan = 2)

    #top.lab1.pack()
    #top.form.mainloop()
    root.mainloop()

def addUser(self):
    ob = register()
    import cgi
    form = cgi.FieldStorage()
    print 
    username = ob.top.inpusername.get()
    password = ob.top.inppw.get()
    print(username)
    print(password)
    #print "The user entered %s" % form.getvalue("entr1")



if __name__ == "__main__":
    app = myLogin()
    #login()
    #window = Window(None)

    #window.title("title")

    #window.mainloop()

当用户点击注册按钮时,它应该调用mySignUp()类的注册方法,并且应该弹出注册表单 但是我收到了这个错误

Tkinter回调中的异常 回溯(最近一次调用最后一次):

文件" C:\ Python27 \ lib \ lib-tk \ Tkinter.py",第1536行,致电     return self.func(* args) TypeError:必须使用mySignUp实例作为第一个参数调用未绑定方法register()(没有任何替代)

可能出现什么问题?

2 个答案:

答案 0 :(得分:1)

您必须使用类register的实例调用绑定方法mySignUp,即addUser的第一行应该读取

ob = self.register()

因为python不知道你指的是哪个mySignUp

答案 1 :(得分:1)

您将其称为类方法,请使用:

mysignup= mySignUp()
self.btn3 = Button(self.frm, text = "Register", command = mysignup.register, width=10)