我的 tkinter 按钮可以工作,但 运行 不是它的相关命令吗?
My tkinter button works but isn't running its relevant command?
我是一个相当新的、没有经验的程序员,我正在使用 python、sqlite3 和 tkinter 创建一个业务管理工具来管理公司员工和客户的详细信息。我面临的问题是我在 tkinter 中有一个菜单要求用户选择他们想要完成的操作,但是在输入选项后,按钮被按下并且完成按钮命令的实际功能不是 运行。我花了 3 天时间寻找错误,但找不到。如果有任何帮助,我将不胜感激。不幸的是,由于项目负责人对剽窃感到震惊,因此无法共享我的全部代码。 运行 的问题代码和屏幕截图如下。谢谢大家
def menu():
global menu_screen
menu_screen= Tk()
menu_screen.title('Main Menu') #title of the window
menu_screen.geometry("1000x500")
label1=Label(menu_screen,text="(*******menu*******)") #function to print menu options
label1.pack()
label2=Label(menu_screen,text="1) Employee Table") #will go to employee menu
label2.pack()
label3=Label(menu_screen,text="2) Client table") #client menu
label3.pack()
label4=Label(menu_screen,text="3) Staff rota") #timetable
label4.pack()
label5=Label(menu_screen,text="4) performance table") #performance menu
label5.pack()
label6=Label(menu_screen,text="5) re-display menu") #main menu again
label6.pack()
label7=Label(menu_screen,text="6) quit") #finished
label7.pack()
labelsel = Label(menu_screen,text = "Please enter your selection:" ,bg="blue",
width="300", height="2", font=("Helvetica", 13)).pack()
select_box= Entry(menu_screen,text= "")#takes selection input
select_box.place(x = 120, y= 20, width = 100, height =25)
select_box.focus()
global selection1 #allows for selcetion variable to be access throughout program
selection1= select_box.get()
button = Button(master=menu_screen, text='confirm choice', command=partial(menu_validate))
button.pack()#button allows to update different detail
menu_screen.mainloop()
def menu_validate():
while selection1!= "":
if selection1 =="1":
employee_menu()#calls and executes employee table function
break
elif selection1 =="2":
client_menu()#calls and executes client table function
break
elif selection1 == "3":
timetable() #calls timetable functio
break
elif selection1 == "4":
performance()#calls and executes performance table function
break
elif selection1 == "5":
menu()#re displays the menu
break
elif selection1 =="6":
quit #quitting
else:
label_incorrect= Label(text = "Unknown option selected!")
label_incorrect(x=50, y=60, width=100, height=25)
menu()#allows the user to choose from menu again if incorrect input, for user
friendliness.
def employee_menu():# this function prints a new menu within the employee table
global emp_screen
emp_screen = Toplevel(menu_screen)
emp_screen.geometry("350x300")
menubar = Menu(menu_screen) #create a menu bar at the top of the window
emp_menu = Menu(menubar, tearoff=0)
emp_menu.add_command(label="Add a new employee", command=emp_sel1) #each option goes to a
new
function
emp_menu.add_command(label="Update an existing employees details", command=emp_sel2)
emp_menu.add_command(label="Delete an employees details", command=emp_sel3)
emp_menu.add_command(label="Find an employees details", command=emp_sel4)
emp_menu.add_command(label="Re-display menu", command=employee_menu)
emp_menu.add_command(label="Quit.", command=quit)
menubar.add_cascade(label="Employee menu", menu=emp_menu) #adds title to the drop down
menu
with sqlite3.connect("SamaritanCare.db") as db: #creates new employee table
cursor=db.cursor()
cursor. execute("""CREATE TABLE if not exists employees(
employeeID text PRIMARY KEY,
firstname text NOT NULL,
surname text NOT NULL,
age integer NOT NULL,
hours_weekly integer NOT NULL,
days_work integer NOT NULL);""")
db.commit()
this menu is displayed but the button won't run employee_menu
您应该在按钮内的函数调用之前添加 lambda:
。否则你正在调用函数,结果存储在 command
.
示例:
Button(
root,
text="button test",
command=lambda: name_function(parameter1, parameter2,...)
)
select_box= Entry(menu_screen,text= "")#takes selection input
select_box.place(x = 120, y= 20, width = 100, height =25)
select_box.focus()
global selection1 #allows for selcetion variable to be access throughout program
selection1= select_box.get()
在上面的代码行中,select_box
被创建并显示在屏幕上,甚至在用户可以向其中输入任何内容之前,您就从输入框中获取了值。这就是为什么 selection1
总是等于一个空字符串并且 menu_validate
函数不会超出 while selection1!= "":
.
的原因
相反,您必须做的是仅在触发某些事件(例如单击按钮)后才从条目中获取值。
在您的代码中,您可以直接执行 while select_box.get() != "":
而不是 while selection1!= "":
。 (不要忘记将 select_box
设置为全局变量,以便您可以在 menu_validate
中访问它)
此外,在您的情况下,无需使用 partial
将函数传递给 commmand
参数。你可以直接做:
command=menu_validate
而不是
command=partial(menu_validate)
更正后的代码:
def menu():
global menu_screen, select_box
menu_screen= Tk()
menu_screen.title('Main Menu') #title of the window
menu_screen.geometry("1000x500")
label1=Label(menu_screen,text="(*******menu*******)") #function to print menu options
label1.pack()
label2=Label(menu_screen,text="1) Employee Table") #will go to employee menu
label2.pack()
label3=Label(menu_screen,text="2) Client table") #client menu
label3.pack()
label4=Label(menu_screen,text="3) Staff rota") #timetable
label4.pack()
label5=Label(menu_screen,text="4) performance table") #performance menu
label5.pack()
label6=Label(menu_screen,text="5) re-display menu") #main menu again
label6.pack()
label7=Label(menu_screen,text="6) quit") #finished
label7.pack()
labelsel = Label(menu_screen,text = "Please enter your selection:" ,bg="blue",
width="300", height="2", font=("Helvetica", 13)).pack()
select_box= Entry(menu_screen,text= "")#takes selection input
select_box.place(x = 120, y= 20, width = 100, height =25)
select_box.focus()
button = Button(master=menu_screen, text='confirm choice', command=menu_validate)
button.pack()#button allows to update different detail
menu_screen.mainloop()
def menu_validate():
selection1 = select_box.get()
while selection1!= "":
if selection1 =="1":
employee_menu()#calls and executes employee table function
break
elif selection1 =="2":
client_menu()#calls and executes client table function
break
elif selection1 == "3":
timetable() #calls timetable functio
break
elif selection1 == "4":
performance()#calls and executes performance table function
break
elif selection1 == "5":
menu()#re displays the menu
break
elif selection1 =="6":
quit #quitting
else:
label_incorrect= Label(text = "Unknown option selected!")
label_incorrect(x=50, y=60, width=100, height=25)
menu()#allows the user to choose from menu again if incorrect input, for user
friendliness.
我是一个相当新的、没有经验的程序员,我正在使用 python、sqlite3 和 tkinter 创建一个业务管理工具来管理公司员工和客户的详细信息。我面临的问题是我在 tkinter 中有一个菜单要求用户选择他们想要完成的操作,但是在输入选项后,按钮被按下并且完成按钮命令的实际功能不是 运行。我花了 3 天时间寻找错误,但找不到。如果有任何帮助,我将不胜感激。不幸的是,由于项目负责人对剽窃感到震惊,因此无法共享我的全部代码。 运行 的问题代码和屏幕截图如下。谢谢大家
def menu():
global menu_screen
menu_screen= Tk()
menu_screen.title('Main Menu') #title of the window
menu_screen.geometry("1000x500")
label1=Label(menu_screen,text="(*******menu*******)") #function to print menu options
label1.pack()
label2=Label(menu_screen,text="1) Employee Table") #will go to employee menu
label2.pack()
label3=Label(menu_screen,text="2) Client table") #client menu
label3.pack()
label4=Label(menu_screen,text="3) Staff rota") #timetable
label4.pack()
label5=Label(menu_screen,text="4) performance table") #performance menu
label5.pack()
label6=Label(menu_screen,text="5) re-display menu") #main menu again
label6.pack()
label7=Label(menu_screen,text="6) quit") #finished
label7.pack()
labelsel = Label(menu_screen,text = "Please enter your selection:" ,bg="blue",
width="300", height="2", font=("Helvetica", 13)).pack()
select_box= Entry(menu_screen,text= "")#takes selection input
select_box.place(x = 120, y= 20, width = 100, height =25)
select_box.focus()
global selection1 #allows for selcetion variable to be access throughout program
selection1= select_box.get()
button = Button(master=menu_screen, text='confirm choice', command=partial(menu_validate))
button.pack()#button allows to update different detail
menu_screen.mainloop()
def menu_validate():
while selection1!= "":
if selection1 =="1":
employee_menu()#calls and executes employee table function
break
elif selection1 =="2":
client_menu()#calls and executes client table function
break
elif selection1 == "3":
timetable() #calls timetable functio
break
elif selection1 == "4":
performance()#calls and executes performance table function
break
elif selection1 == "5":
menu()#re displays the menu
break
elif selection1 =="6":
quit #quitting
else:
label_incorrect= Label(text = "Unknown option selected!")
label_incorrect(x=50, y=60, width=100, height=25)
menu()#allows the user to choose from menu again if incorrect input, for user
friendliness.
def employee_menu():# this function prints a new menu within the employee table
global emp_screen
emp_screen = Toplevel(menu_screen)
emp_screen.geometry("350x300")
menubar = Menu(menu_screen) #create a menu bar at the top of the window
emp_menu = Menu(menubar, tearoff=0)
emp_menu.add_command(label="Add a new employee", command=emp_sel1) #each option goes to a
new
function
emp_menu.add_command(label="Update an existing employees details", command=emp_sel2)
emp_menu.add_command(label="Delete an employees details", command=emp_sel3)
emp_menu.add_command(label="Find an employees details", command=emp_sel4)
emp_menu.add_command(label="Re-display menu", command=employee_menu)
emp_menu.add_command(label="Quit.", command=quit)
menubar.add_cascade(label="Employee menu", menu=emp_menu) #adds title to the drop down
menu
with sqlite3.connect("SamaritanCare.db") as db: #creates new employee table
cursor=db.cursor()
cursor. execute("""CREATE TABLE if not exists employees(
employeeID text PRIMARY KEY,
firstname text NOT NULL,
surname text NOT NULL,
age integer NOT NULL,
hours_weekly integer NOT NULL,
days_work integer NOT NULL);""")
db.commit()
this menu is displayed but the button won't run employee_menu
您应该在按钮内的函数调用之前添加 lambda:
。否则你正在调用函数,结果存储在 command
.
示例:
Button(
root,
text="button test",
command=lambda: name_function(parameter1, parameter2,...)
)
select_box= Entry(menu_screen,text= "")#takes selection input
select_box.place(x = 120, y= 20, width = 100, height =25)
select_box.focus()
global selection1 #allows for selcetion variable to be access throughout program
selection1= select_box.get()
在上面的代码行中,select_box
被创建并显示在屏幕上,甚至在用户可以向其中输入任何内容之前,您就从输入框中获取了值。这就是为什么 selection1
总是等于一个空字符串并且 menu_validate
函数不会超出 while selection1!= "":
.
相反,您必须做的是仅在触发某些事件(例如单击按钮)后才从条目中获取值。
在您的代码中,您可以直接执行 while select_box.get() != "":
而不是 while selection1!= "":
。 (不要忘记将 select_box
设置为全局变量,以便您可以在 menu_validate
中访问它)
此外,在您的情况下,无需使用 partial
将函数传递给 commmand
参数。你可以直接做:
command=menu_validate
而不是
command=partial(menu_validate)
更正后的代码:
def menu():
global menu_screen, select_box
menu_screen= Tk()
menu_screen.title('Main Menu') #title of the window
menu_screen.geometry("1000x500")
label1=Label(menu_screen,text="(*******menu*******)") #function to print menu options
label1.pack()
label2=Label(menu_screen,text="1) Employee Table") #will go to employee menu
label2.pack()
label3=Label(menu_screen,text="2) Client table") #client menu
label3.pack()
label4=Label(menu_screen,text="3) Staff rota") #timetable
label4.pack()
label5=Label(menu_screen,text="4) performance table") #performance menu
label5.pack()
label6=Label(menu_screen,text="5) re-display menu") #main menu again
label6.pack()
label7=Label(menu_screen,text="6) quit") #finished
label7.pack()
labelsel = Label(menu_screen,text = "Please enter your selection:" ,bg="blue",
width="300", height="2", font=("Helvetica", 13)).pack()
select_box= Entry(menu_screen,text= "")#takes selection input
select_box.place(x = 120, y= 20, width = 100, height =25)
select_box.focus()
button = Button(master=menu_screen, text='confirm choice', command=menu_validate)
button.pack()#button allows to update different detail
menu_screen.mainloop()
def menu_validate():
selection1 = select_box.get()
while selection1!= "":
if selection1 =="1":
employee_menu()#calls and executes employee table function
break
elif selection1 =="2":
client_menu()#calls and executes client table function
break
elif selection1 == "3":
timetable() #calls timetable functio
break
elif selection1 == "4":
performance()#calls and executes performance table function
break
elif selection1 == "5":
menu()#re displays the menu
break
elif selection1 =="6":
quit #quitting
else:
label_incorrect= Label(text = "Unknown option selected!")
label_incorrect(x=50, y=60, width=100, height=25)
menu()#allows the user to choose from menu again if incorrect input, for user
friendliness.