我正在尝试创建一个简单的用户输入tkinter GUI,用户可以在其中输入基本细节。虽然我在网上搜索过,但我无法克服这个问题:
from tkinter import *
def user():
print(user_entry.widget.get())
print(email_entry.widget.get())
print(pass1_entry.widget.get())
print(pass2_entry.widget.get())
pass
#main loop initial visual
main_window = Tk()
main_window.title('Register')
main_window.geometry('250x350+200+200')
main_window.configure(bg = 'Blue')
wel = Label(main_window, text="""Hello, thanks for using my software.
Please put in the details, it will be kept
safe and sent to our database""", bg = 'white')
wel.place(x = 0,y = 10)
username_lab = Label(main_window, text="Username:").place(x=0, y=64)
#us = StringVar()
user_entry = Entry(main_window)
user_entry.place(x=70,y=64)
user_entry.bind("<Return>", user())
email_lab = Label(main_window, text="Email: ").place(x=0, y=90)
#emi = StringVar()
email_entry = Entry(main_window)
email_entry.place(x=70,y=90)
email_entry.bind("<Return>", user())
pass_lab = Label(main_window, text="Password:").place(x=0, y=116)
#pas1 = StringVar()
pass1_entry = Entry(main_window, show="*")
pass1_entry.place(x=70,y=116)
pass1_entry.bind("<Return>", user())
pass_lab = Label(main_window, text="Password repeat:").place(x=0, y=140)
#pas2 = StringVar()
pass2_entry = Entry(main_window, show="*")
pass2_entry.place(x=100,y=140)
pass2_entry.bind("<Return>", user())
ok = Button(main_window, text="OK", command = user(), width=8).place(x=15, y= 175)
main_window.mainloop()
不幸的是,我一直收到这个错误,但我无法弄清楚原因。请帮助至少一件事(即用户名),其余的将遵循类似的规则。 :
Traceback (most recent call last):
File "C:/Users/Milosz/Desktop/Programming/Register/main.py", line 28, in <module>
user_entry.bind("<Return>", user())
File "C:/Users/Milosz/Desktop/Programming/Register/main.py", line 5, in user
print(user_entry.widget.get())
AttributeError: 'Entry' object has no attribute 'widget'
答案 0 :(得分:0)
当您需要在输入字段上使用get()
时,您只需要在变量名称上使用它。 widget()
没有做你认为它在这里做的事情而且不需要。
在user():
类中替换所有print语句。
自:
def user():
print(user_entry.widget.get())
print(email_entry.widget.get())
print(pass1_entry.widget.get())
print(pass2_entry.widget.get())
pass
要:
def user():
print(user_entry.get())
print(email_entry.get())
print(pass1_entry.get())
print(pass2_entry.get())
# removed pass as it is not needed and does nothing here.
这应该将内容打印到控制台。