我尝试使用tkinter
对登录页面进行编码,但出现错误。这是我的代码:
from tkinter import *
def register(event):
screen1 = Toplevel(screen)
screen1.title("Register")
screen1.geometry("320x250")
username =StringVar()
password =StringVar()
contact =StringVar()
email =StringVar()
Label(screen1,text="Please enter your details below").pack()
Label(screen1,text="Username").pack()
Entry(screen1,textvariable = username)
Label(screen1,text="Contact").pack()
Entry(screen1, textvariable=contact)
Label(screen1, text="Email id").pack()
Entry(screen1, textvariable=email)
Label(screen1, text="Password").pack()
Entry(screen1, textvariable=password)
Button(screen1, text="Register", width=10, height=1).pack()
def login(event):
screen2 = Toplevel(screen)
screen2.title("Register")
screen2.geometry("320x250")
username = StringVar()
password = StringVar()
Label(screen2, text="Username").pack()
Entry(screen2, textvariable=username)
Label(screen2, text="Password").pack()
Entry(screen2, textvariable=password)
Button(screen2, text="Login", width=10, height=1).pack()
def main_screen():
global screen
screen = Tk() #creating tk window
screen.geometry("320x250")
screen.title= ("notes 1.0")
Label(screen,text="").pack()
Button(screen,text="Login",height="2", width="30",command=login).pack()
Label(screen,text="").pack()
Button(screen,text="Register",height="2", width="30",command=register).pack()
screen.mainloop()
main_screen()
我收到此错误:
Traceback (most recent call last):
File "C:\Users\Matrena\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
TypeError: register() missing 1 required positional argument: 'event'
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Matrena\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
TypeError: login() missing 1 required positional argument: 'event'
答案 0 :(得分:0)
这里。首先,您不应该将event
传递给register()
和login()
函数。如果使用.bind()
函数,则必须使用事件,否则,将给您一个错误。
这是您的代码:
from tkinter import *
screen = Tk() #creating tk window
screen.geometry("320x250")
screen.title("notes 1.0")
Label(screen,text="").pack()
def login():
screen2 = Toplevel()
screen2.title("Register")
screen2.geometry("320x250")
username = StringVar()
password = StringVar()
Label(screen2, text="Username").pack()
Entry(screen2, textvariable=username)
Label(screen2, text="Password").pack()
Entry(screen2, textvariable=password)
Button(screen2, text="Login", width=10, height=1).pack()
def register():
screen1 = Toplevel(screen)
screen1.title("Register")
screen1.geometry("320x250")
username =StringVar()
password =StringVar()
contact =StringVar()
email =StringVar()
Label(screen1,text="Please enter your details below").pack()
Label(screen1,text="Username").pack()
Entry(screen1,textvariable = username)
Label(screen1,text="Contact").pack()
Entry(screen1, textvariable=contact)
Label(screen1, text="Email id").pack()
Entry(screen1, textvariable=email)
Label(screen1, text="Password").pack()
Entry(screen1, textvariable=password)
Button(screen1, text="Register", width=10, height=1).pack()
Button(screen,text="Login",height="2", width="30",command = login).pack()
Label(screen,text="").pack()
Button(screen,text="Register",height="2", width="30",command=register).pack()
screen.mainloop()
不过,我注意到该项目尚未完成,因此,如果您要我完成该项目,只需ping我即可!
希望这会有所帮助!