Python Tkinter callbackfunction executes before mainloop -


i'm trying build gui communication serial device. i'm using tkinter. problem is, every time execute script estcon-function executed , mainloop, , therefore gui never started. if place definition of estcon function after main loop says estcon function not found.

def estcon():     # establish connection     while true:         try:             ser = serial.serial(port, baud, bytesize)             print('connected.')             break         except serial.serialexception:             print('waiting device ' + port + ' available.')              time.sleep(3)      starttime = time.time()     outfile = open(filename, 'a')     doprint = true      root = tk()  estconbutton = button(root, text="establish serial connection",                       command=estcon()) estconbutton.pack()  root.mainloop() 

you need change line:

estconbutton = button(root, text="establish serial connection", command=estcon()) 

to:

estconbutton = button(root, text="establish serial connection", command=estcon) 

notice lack of parentheses (). basically, need pass reference function called when press button , not actual call.


Comments