Home > OS >  My tkinter button works but isn't running its relevant command?
My tkinter button works but isn't running its relevant command?

Time:04-14

I am a fairly new and inexperienced programmer and I am working with python, sqlite3 and tkinter to create a business management tool to manage details about a company's employees and clients. The problem I am facing is that I have a menu in tkinter that asks user to choose the action they want to complete, but upon input of option, the button is just pressed and the actual function to complete the button command isn't run. I have spent 3 days looking for the error, but can't find it. Please I will appreciate any help at all. Unfortunately cannot share my whole code due to the project heads freaking over plagiarism. The problematic bit of code and screenshot of run is below. thank you all.

    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

CodePudding user response:

you should add lambda: before the function call inside the button. Otherwise you are calling the function and result is stored in "command". Example: Button(root, text="button test", command=lambda: name_function(parameter1, parameter2,...))

CodePudding user response:

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 the above code lines, select_box is created, displayed on the screen and even before the user can enter anything into it, you are getting the value from the entry box. That's the reason why selection1 is always equal to an empty string and the menu_validate function does not go beyond while selection1!= "":.

Instead, what you must do is get the value from the entry only after some event is triggered, like the clicking of a button.

In your code, you can directly do while select_box.get() != "": instead of while selection1!= "":. (Don't forget to make select_box a global variable so that you can access it in menu_validate)

Also, in your case, there is no need to use partial to pass a function to the commmand parameter. You can directly do:

command=menu_validate

instead of

command=partial(menu_validate)

Corrected 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.
  • Related