0
MyButton1 =Button(master, text='Quit',bg="grey",width=20, 
command=master.quit)
MyButton1.place(x=200, y=100)
MyButton2 =Button(master, text='Propagate', bg="grey",width=20, 
command=mainmethod)
MyButton2.place(x=1000, y=100)
master.geometry("1500x1500")
master.mainloop( )

In the above code after pressing propagate button mainmethod is invoking.. I wrote my logic in main method where this method alone taking 2minutes to execute in the mean time GUI going unresponsive state for few min and later displaying all my required output on text box i inserted

whether any away to avoid the unresponsive issue apart from using multi threading

and i am looking such that after pressing propagate button button should disabled and window should not go unresponsive and display text.insert statements continuously which i added in main method ?????

saikiran
  • 81
  • 8

1 Answers1

-1

To prevent hanging, you need to separate the calculations in the mainmethod from Tkinter's main loop by executing them in different threads. However, threading system in Python is not that well-developed as in other languages (AFAIK) because of GIL (Global Interpreter Lock), but there is an alternative - using processes just like threads. This is possible with multiprocessing library.

In order to just prevent hanging, you could create another function

from multiprocessing import Process
def mainmethodLaunch():
    global mainmethodProcess
    mainmethodProcess = Process(target=mainmethod)
    mainmethodProcess.start()

And bind this function to MyButton2 instead of mainmethod itself.

Docs: https://docs.python.org/2/library/multiprocessing.html#the-process-class

You can see p.join in the example. join method will cause your main process to wait for the other one to complete, which you don't want.

So when you press the button, mainmethodLaunch function will be invoked, and it will create another process executing mainmethod. mainmethodLaunch function's own run duration should be insignificant. Due to usage of another process, Tkinter window will not hang. However, if you do just this, you will not be able to interact with mainmethod process in any kind while it will be working.

In order to let these processes communicate with each other, you could use pipes (https://docs.python.org/2/library/multiprocessing.html#exchanging-objects-between-processes) I guess the example is quite clear.

In order to receive some data from the mainmethod process over time, you will have to poll the parent_conn once a little time, let's say, second. This can be achieved with Tkinter's after method (tkinter: how to use after method)

IMPORTANT NOTE: when using multiprocessing, you MUST initialize the program in if __name__ == '__main__': block. I mean, there should be no doing-something code outside functions and this block, no doing-something code with zero indent.

This is because multiprocessing is going to fork the same Python executable file, and it will have to distinguish the main process from the forked one, and not do initializing stuff in the forked one.

Check twice if you have done that because if you make such a mistake, it can cost you hanging of not just Tkinter window, but the whole system :) Because the process will be going to fork itself endlessly, consuming all RAM you have, regardless of how much you have.

Andrew Che
  • 928
  • 7
  • 20
  • But i wrote my code in procedural style and in mainmethod if i want to update text box (which is declared in if __name__ == '__main__':)as below def mainmethod: ..... txt.insert(1.0,"some print ") ...... i am getting as Traceback (most recent call last): File "setup.py", line 366, in master.mainloop( ) File "C:\CRMAPPS\APPS\PYTHON262\lib\lib-tk\Tkinter.py", line 1017, in mainloop self.tk.mainloop(n) Whether object txt can be passed as input to process? please help me – saikiran Aug 27 '17 at 18:06
  • As I said, you will need to establish a pipe connection between mainmethod process and the Tkinter app. Mainmethod will have to send that lines to print via the pipe, and Tkinter app will have to poll that pipe for incoming lines to print. You will not be able to operate Tkinter window directly from mainmethod, this is the price for using another process. – Andrew Che Aug 27 '17 at 18:19
  • @saikiranvura Oh, and function definitions must be outside `if __name__ == '__main__':` block – Andrew Che Aug 27 '17 at 18:57
  • Thanks Andrew Cherevatkin i will try – saikiran Aug 27 '17 at 19:01