3 sub questions:
[1] python GUIs are polling based?
it seems to me that both tk and qtpy are polling based, if the gui calls a function that takes a while to excecute, the entire gui hangs.
I learnt about gui long time ago, I remembered that modern gui should be interrupt based, even when gui were executing something big, gui should be responsive all the time. gui may not display the result from those big calculations, but it will respond to resize, show button click animation etc. So my question is, Is there an option like:
#psuedo code
root=tkinter.Tk()
root.setInterruptMode(True)
[2] Is tk.mainloop() just a gigantic loop?
if my first question is a pipe dream, and I will just have to learn about threading and multiprocess, then my next question is about root.mainloop()
(or qtpy's exec_()
).
My impression is that mainloop() doesn't really start a thread or anything in python, it just packs a gigantic and invisible tkinter's gui polling+painting loop into my main line. Is my impression correct?
[3] why putting mainloop in Main line?
Does mainloop()
have to reside in the Main line? can I thread/multiprocess it out? so that my Main line can concentrate on big calculations, and the Main line governs gui process and IO processes. All the examples I came across have mainloop()
in the Main line, I am not sure it is a recommended approach or what the benefits are.
Below is the code I wrote while trying to learn about python gui:
import tkinter
import random
class myGUI():
def __init__(self, arg_tkroot):
self.GUI_display = tkinter.Label(arg_tkroot, text='init-ed')
self.GUI_button = tkinter.Button(arg_tkroot, text='click')
self.GUI_display.pack()
self.GUI_button.pack()
self.GUI_button.bind('<Button-1>', self.handle_user_interaction)
self.list_bigData = []
#handles GUI interaction, and call bigData_and_bigCalculation()
def handle_user_interaction(self, arg_event):
print(arg_event, ' detected by myGUI')
strResult_toFeedbackToUser = self.bigData_and_bigCalculation()
self.GUI_display.config(text=strResult_toFeedbackToUser)
print('finished handling user interact')
# slow calculations and memory consuming operations
def bigData_and_bigCalculation(self):
self.list_bigData[:]=[]
for i in range(500000):
self.list_bigData.append( ''.join(random.choice('asdfas') for k in range(10)) )
return self.list_bigData[-1]
# Main()
if __name__ == '__main__':
root = tkinter.Tk()
mygui = myGUI(root)
root.mainloop()