Ich bin ein ziemlich neuer und unerfahrener Programmierer und arbeite mit python, sqlite3 und tkinter, um ein Business-Management-Tool zu erstellen, um Details über die Mitarbeiter und Kunden eines Unternehmens zu verwalten. Das Problem, mit dem ich konfrontiert bin, ist, dass ich ein Menü in tkinter habe, das den Benutzer auffordert, die Aktion auszuwählen, die er abschließen möchte, aber bei Eingabe der Option wird die Schaltfläche nur gedrückt und die eigentliche Funktion zum Abschließen des Schaltflächenbefehls wird nicht ausgeführt. Ich habe 3 Tage damit verbracht, nach dem Fehler zu suchen, aber ich kann ihn nicht finden. Bitte, ich freue mich über jede Hilfe. Leider kann ich meinen gesamten Code nicht teilen, da die Projektleiter wegen Plagiaten ausflippen. Das problematische Code-Bit und der Screenshot von run sind unten. Danke euch allen.
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()
Dieses Menü wird angezeigt, aber die Schaltfläche führt nicht employee_menu aus
Lösung des Problems
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()
In den obigen Codezeilen select_box
wird erstellt, auf dem Bildschirm angezeigt und noch bevor der Benutzer etwas eingeben kann, erhalten Sie den Wert aus dem Eingabefeld. Das ist der Grund, warum selection1
immer gleich ein leerer String ist und die menu_validate
Funktion nicht darüber hinausgehtwhile selection1!= "":
.
Stattdessen müssen Sie den Wert aus dem Eintrag erst abrufen, nachdem ein Ereignis ausgelöst wurde, z. B. das Klicken auf eine Schaltfläche.
In Ihrem Code können Sie direkt tunwhile select_box.get()!= "":
anstelle von while selection1!= "":
. (Vergessen Sie nicht, select_box
eine globale Variable zu erstellen, damit Sie in darauf zugreifen können. menu_validate
)
Auch in Ihrem Fall besteht keine Notwendigkeit zu verwendenpartial
werden, um eine Funktion an den commmand
Parameter zu übergeben. Sie können direkt Folgendes tun:
command=menu_validate
anstatt
command=partial(menu_validate)
Korrigierter Code:
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.
Keine Kommentare:
Kommentar veröffentlichen