Python using tkinter GUI

Thread Starter

zazas321

Joined Nov 29, 2015
936
I am making a GUI for my application and decided to use python tkinter. The GUI is very simple and just needs to guide an operator through the process(just print messages on the GUI telling him what to do and ocasionally allow him to input a barcode).

I have been using python serial monitor as UI as I can simply print messages there and allow user input. But thats not very convenient thats why I want to switch to something better. I have not used tkinter before and I have some problems understanding exactly how it works.

I have all my code running in an infinite loop. The program begins by waiting for user input ( user needs to scan a barcode). Once the user scans a barcode, program then decides what to do.



Code:
main()
myConnection = mysql.connector.connect( host=hostname, user=username, passwd=password, db=database )
counter = 0

window = tk.Tk()
window.title("GUI")
window.geometry("400x200")

while(1):

    # OPERACIJOS KODAI:
    # 0 - PILDYMAS
    # 1 - KOMPLEKTAVIMAS
    # 2 - NETINKAMAS KODAS
    tk.Label(window,text = "Scan barcode here:").pack()
    entry = tk.Entry(window)
    entry.pack()
    var = tk.IntVar()
    button = tk.Button(window,text="Continue",command = lambda: var.set(1))
    button.pack()
    print("waiting...")
    button.wait_variable(var)
    result = entry.get()
    print("Entry string=",result)
    var.set(0)

    
    operacijos_kodas=Scanning_operation(myConnection,result)
    print("operacijos kodas=",operacijos_kodas)
    if(operacijos_kodas == 0):
        tk.label(window,text = "PILDYMO OPERACIJA:").pack()

        pildymo_operacija(myConnection)
   
        
    elif(operacijos_kodas == 1):
        tk.Label(window,text = "PAKAVIMO OPERACIJA:").pack()

        insertData_komplektacija(myConnection,"fmb110bbv801.csv");
        update_current_operation(myConnection);
        picking_operation();
        
    elif(operacijos_kodas == 2):
        print("Skenuokite dar karta")
        #break

    window.mainloop()


I have a few questions though:

1.
The first part of tkinter where an user needs to input a barcode and press Continue seems to be working fine:
2020-08-26-095522_1920x1080_scrot.png

However, once the barcode is scanned, I want to display some text based on the desired operation as following:
tk.label(window,text = "PILDYMO OPERACIJA:").pack()
or
tk.label(window,text = "PILDYMO OPERACIJA:").pack()

However, for some reasons these commands do not print anything on my GUI, why would that be the case? I am not fully understanding the pack() method that I use ( I found the examples online that is why I use it). Maybe thats causing the message to not appear on my GUI


2. What is the exact purpose of calling window.mainloop() function where window = tk.Tk()?


3. Once the operation is completed, I go back to the beggining of while loop and start over again. However, I am greeted with an error message:2020-08-26-100712_1920x1080_scrot.png

Appreciate any help
 
Top