0

Trying tkinter for first time. The code below has 4 print# statements. Two questions here -

1)- When executing, I expected the of flow of code would be same as print# statements but seems the flow is different . I see print2,3 first and then print1. why is it so ?

2)- Only print1 and 4 have value of variable u_input ( or e1.get ) . Why are print2,3 dont show the same value ? .. its probably same reason as flow of code.

from tkinter  import *

w = Tk()
w.title(" Main Window")
w.geometry('800x800')      

def test ():
    ux = u_input.get()
    print("print1 : " , ux )

u_input = StringVar()

e1 = Entry(w , textvariable = u_input )
e1.grid(row=0,column=1, padx = 300 , pady= 20 )
b1 = Button(w, text ="button1", width = 12 , justify = "center", command = test )
b1.grid(row=10,column=1 , padx= 300 , pady= 40)


print("print 2 : ", str(u_input.get()))
print("print 3 : ", str(e1.get()))

w.mainloop()

User_Entry = str(u_input.get())
print("print 4 ", User_Entry)

===============================

comparing above code to one below - this one prints in the order the functions are called.

a= 1
b= 2
def func2 ():
    a=100
    b=200
    print("print3 :", a+b)
def func1 ():
    a=10
    b=20
    print("print1 :",a+b)

func1()
print("print2 :", a+b)
func2()
DS_
  • 247
  • 2
  • 10
  • "I see print2,3 first and then print1. why is it so ?" In your own words, why do you expect otherwise? Why should the code inside `test` happen before the other print calls are reached? – Karl Knechtel Sep 07 '20 at 01:23
  • Also: your code has no print statements in 3.x. `print` is a function now. – Karl Knechtel Sep 07 '20 at 01:23
  • `Button()` doesn't wait for your click - it only inform `mainloop()` what widget to display in window - and `mainloop()` starts program and display window. So `"print 2"` `"print 3"` is executed before `mainloop()` shows window - and before you click button which runs code with `"print 1"` – furas Sep 07 '20 at 02:15

1 Answers1

0

All GUIs work different then command input().

Button() doesn't wait for your click - it only creates information for mainloop() what widget it has to display in window - and later mainloop() starts program and it displays window and all widgets in this window. And it runs loop which gets keyboard/mouse events from system, sends them to widgets, runs function when you click some button, (re)draw widgets, etc.

So "print 2", "print 3" is executed before mainloop() shows window - and before you can click button which runs code with "print 1".

And the same is with second problem. In "print 2", "print 3" it prints values before mainloop() displays window and before you can put any text in Entry.


As I said the same way works other GUIs - not only tkinter but PyQt, PyGTK, etc. And not only in Python but also in other languages C, C++, Java, etc.

The same way may work also games - they also may have some event loop. In PyGame you have to even write this loop on your own.

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thanks furas!! .. I kind of get your point that GUIs always work this way that all code before mainloop() is just information and the function defined under test are not actually called. But just posted another regular code which is what i was comparing it to. – DS_ Sep 07 '20 at 19:32
  • you compare `tkinter` with code which works in normal way. It runs `func1()` directly but `tkinter` runs `test()` in `mainloop()`. When you click inside your program window then system sends mouse event to your program, `mainloop()` gets this event, it sends it to all widgets, every widget check if mouse was in its area, when button recognized that it was in its area and it was clicked `left mouse button` and button has defined `command=` then it runs assigned function `test()`. – furas Sep 07 '20 at 20:26