我编写了一个程序,用于输入用户输入(姓名,高中课程),当用户输入完成后,可以按下我创建的名为“Enter”的按钮。但是,为了使我的程序更整洁,我希望用户能够使用键盘上的回车键。
如何将当前链接到我已定义的'Enter'按钮的功能分配给键盘上的enter键,以便用户可以使用?
name_var = StringVar()
def event_enter_1():
self.Enter_1.config(state=(NORMAL if name_var.get() else DISABLED))
name_var.trace('w', lambda name, index, mode: event_enter_1())
enter_name = Entry(self, textvariable=name_var)
enter_name.pack()
enter_name.focus_set()
def enter_button_1():
if len(name_var.get()) > 10 or any(l not in string.ascii_letters for l in name_var.get()):
tkMessageBox.showerror("Error", "Please enter a real name.")
else:
global student_name
student_name = name_var.get()
self.Enter_1.config(state="disabled")
self.display_surname= tk.Label(self, width=40, height=2, text = "Now please enter your surname.")
self.display_surname.pack(side="top", fill="both", expand=True)
surname_var = StringVar()
def event_enter_2():
self.Enter_2.config(state=(NORMAL if surname_var.get() else DISABLED))
surname_var.trace('w', lambda name, index, mode: event_enter_2())
surname = Entry(self, textvariable=surname_var)
surname.pack()
surname.focus_set()
def enter_button_2():
if len(surname_var.get()) > 10 or any(l not in string.ascii_letters for l in surname_var.get()):
tkMessageBox.showerror("Error", "Please enter a real name.")
else:
global student_surname
student_surname = surname_var.get()
self.Enter_2.config(state="disabled")
self.display_tutor = tk.Label(self, width=40, height=2, text = "Now please enter your tutor group.")
self.display_tutor.pack(side="top", fill="both", expand=True)
tutor_var = StringVar()
def event_enter_3():
self.Enter_3.config(state=(NORMAL if tutor_var.get() else DISABLED))
tutor_var.trace('w', lambda name, index, mode: event_enter_3())
tutor = Entry(self, textvariable=tutor_var)
tutor.pack()
tutor.focus_set()
def enter_button_3():
if tutor_var.get() not in ["5A1", "5A2", "5B1", "5B2", "5B3", "5C1", "5C2", "6A1", "6A2", "6B1", "6B2", "6C1", "6C2", "6C3"]:
tkMessageBox.showerror("Error", "Please enter a tutor group that exists.")
else:
global student_tutor_group
student_tutor_group = tutor_var.get()
self.Enter_3.config(state="disabled")
tkMessageBox.showinfo("Details Confirmation", "Thank you for entering your details. Now you can begin the quiz. Good luck!")
parent.question_1.config(state="normal")
parent.enter_name.config(state="disabled")
self.destroy
self.Enter_3 = Button(self, text="Enter", width=10, command=enter_button_3)
self.Enter_3.pack()
self.Enter_3.config(state="disabled") #Initially the enter button is disbabled as the user hasn't started typing
self.Enter_2 = Button(self, text="Enter", width=10, command=enter_button_2)
self.Enter_2.pack()
self.Enter_2.config(state="disabled") #Initially the enter button is disbabled as the user hasn't started typing
self.Enter_1 = Button(self, text="Enter", width=10, command=enter_button_1)
self.Enter_1.pack()
self.Enter_1.config(state="disabled") #Initially the enter button is disbabled as the user hasn't started typing
如果这令人困惑,我很抱歉,我是OO编程的新手,所以也许有更有效的方法。非常感谢帮助。感谢
答案 0 :(得分:2)
您需要添加绑定以将事件连接到函数。在这种情况下,事件为<Return>
。你会这样做:
enter_name.bind('<Return>', enter_button_1)
当绑定触发时,它将使用一个参数调用该函数,该参数是描述该事件的对象。您需要修改您的函数才能接受该事件:
def enter_button_1(event=None)