Understanding tkinter mainloop

Thread Starter

zazas321

Joined Nov 29, 2015
627
Hello. I am trying to build a tkinter GUI using mainloop. I have initially not used mainloop and managed to build and interface just by updating the tk.window when required. However, I would like to understand how to properly use the mainloop and build the interface with it.

I am using Raspberry PI python as a programming environment.

This is my main function that is being called:

Code:
def login():
 
    global window
    global canvas_tk
    global username_verify
    global password_verify
    global username_login_entry
    global password_login_entry
    window = tk.Tk()   # create a GUI window
 
    window.geometry("1920x1080") # set the configuration of GUI window
    canvas_tk = tk.Canvas(window,bg='ivory2',width=1920,height=1080)
    #canvas_tk.resizable(0,0)
    window.title("Pick To Light")
    canvas_tk.pack()

    username_verify = tk.StringVar()
    password_verify = tk.StringVar()

    username_label = tk.Label(canvas_tk, text="Username * ")
    username_login_entry = tk.Entry(canvas_tk, textvariable=username_verify)
    username_login_entry.focus_set()
    password_label = tk.Label(canvas_tk, text="Password * ")
    password_login_entry = tk.Entry(canvas_tk, textvariable=password_verify, show= '*')
 
    canvas_tk.create_text(960,20,text="Please enter details below to login")
    canvas_tk.create_window(960,75,window=username_label)
    canvas_tk.create_window(960,100,window=username_login_entry)
    canvas_tk.create_window(960,125,window=password_label)
    canvas_tk.create_window(960,150,window=password_login_entry)
 
    username_login_entry.bind('<Return>',(lambda event: login_verify()))
    password_login_entry.bind('<Return>',(lambda event: login_verify()))
 
    window.mainloop() # start the GUI
As you can see. At the end of login i use mainloop. Inside this login function, I am using bind function to bind enter key to execute function login_verify().
When I enter the login details and press enter, the login_verify will be executed:

Code:
ef login_verify():
#get username and password
    username1 = username_verify.get()
    password1 = password_verify.get()
# this will delete the entry after login button is pressed
    username_login_entry.delete(0, tk.END)
    password_login_entry.delete(0, tk.END)

#The method listdir() returns a list containing the names of the entries in the directory given by path.
    list_of_files = os.listdir("/home/pi/Desktop/lukas_programming/PTL_python")
#defining verification's conditions
    if username1 in list_of_files:
        file1 = open(username1, "r")   # open the file in read mode
        verify = file1.read().splitlines()
        if password1 in verify:
            window.after(1000,Full_operation)
            #login_sucess()
        else:
            password_not_recognised()

    else:
        user_not_found()
If the password matches the expected password, I then call the function Full_operation:
window.after(1000,Full_operation)
which is supposed to handle all the tasks that I need


Full operation code:
Code:
def Full_operation():
    print("starting full operation")
    Application_GUI()
    operacijos_kodas=Scanning_operation(myConnection,Scanned_serial)
    print("CODE=",operacijos_kodas)
    if(operacijos_kodas == 0):   
        update_canvas_label2(scan_label,960,75,"Press button")
        pildymo_operacija(myConnection,Scanned_serial) 
    elif(operacijos_kodas == 1):
        picking_gui()
        #insertData_komplektacija(myConnection,"fmb110bbv801.csv");  
        gaminio_kodas = komplektacijos_ID+".csv"
        insertData_komplektacija(myConnection,gaminio_kodas);
        update_current_operation(myConnection);  
        update_canvas_label2(scan_label,960,75,"take")
        gaminio_kodas_text = "Gaminio kodas="+Scanned_serial
        gaminio_serial_text = "Serial="+komplektacijos_Serial
        gaminio_IMEI_Text = "IMEI="+komplektacijos_Imei
        canvas_tk.create_text(960,400,text=gaminio_kodas_text,font='Helvetica 12 bold')
        canvas_tk.create_text(960,425,text=gaminio_serial_text,font='Helvetica 12 bold')
        canvas_tk.create_text(960,450,text=gaminio_IMEI_Text,font='Helvetica 12 bold') 
        picking_operation(myConnection);
        
    elif(operacijos_kodas == 2):
        update_canvas_label2(scan_label,960,75,"invalid code")
        #sss
        #canvas_tk.update()
        time.sleep(1)
     
    window.after(1000,Full_operation())
Inside full operation code, I want to replace login GUI with my application gui therefore I call application_GUI function:
Code:
def Application_GUI():
    global Scanned_serial
    global scan_label
    global scan_entry
    global cont
    global entry_code
    cont = 0
    canvas_tk.delete("all")
    start_label = tk.Label(canvas_tk,text = "scan code:",font='Helvetica 16 bold',bg='ivory2',foreground="red")
    entry_code = tk.Entry(canvas_tk) # entry = guid
    entry_code.focus_set()
    end_program_button = tk.Button(canvas_tk, text="Baigti",font='Helvetica 12 bold', width=20, height=2, command=end_program)
    ota_button = tk.Button(canvas_tk, text="Atnaujinti",font='Helvetica 12 bold', width=20, height=2, command=OTA_gui)
    remove_item_button = tk.Button(canvas_tk, text="Isvalyti dezute",font='Helvetica 12 bold', width=20, height=2, command=Remove_item_gui)
    new_operation_button = tk.Button(canvas_tk, text="Nauja operacija",font='Helvetica 12 bold', width=20, height=2, command = lambda:New_operation())
    canvas_tk.create_text(960,20,text="PICK TO LIGHT",font='Helvetica 16 bold')
    scan_label = canvas_tk.create_window(960,75,window=start_label)
    scan_entry=canvas_tk.create_window(960,125,window=entry_code)
    canvas_tk.create_window(1210,200,window=ota_button)
    canvas_tk.create_window(960,200,window=remove_item_button)
    canvas_tk.create_window(710,200,window = new_operation_button)
    canvas_tk.create_window(1210,400,window = end_program_button)
    #sss
    #canvas_tk.update()
    entry_code.bind('<Return>',get_callback)
    while (cont != 1):
        time.sleep(0.1)
        print("waiting")
        #sss
        #canvas_tk.update()
    cont = 0
    Scanned_serial = entry_code.get()
    print("SCanned serial = ",Scanned_serial)
Here I delete the previous canvas and create a new widgets for my application. I can see that the code executes and then gets stuck in my while loop:
entry_code.bind('<Return>',get_callback)
while (cont != 1):
time.sleep(0.1)
print("waiting")
I am stuck on this while loop untill I press enter key.

The problem is that no widgets are displayed after deleting the login canvas despite executing all the widget code in Application_GUI.
Thats how my GUI looks like after the login. It is supposed to destroy all previous widgets and create my application window:
You can see that it does not even delete the previous canvas properly as the text "Please enter details below to login" is still there
2020-09-16-094812_1920x1080_scrot.png
Any help is appreciated!
 
Last edited:
Top