0

I am new to python and have been trying to pause my code by clicking the pause button and resume it as soon as the Resume button is clicked. But the but don't show up during the execution even when I have defined the buttons before the function call. My code is as follows:

from tkinter import *
from time import sleep

master = Tk()

frame = Frame(master)
frame.grid()

global is_asleep 

def important_function():
    i=0
    while(True):
        print(i)
        i = i+1
        sleep(1)

def pause():
    #pause the execution
    try:
        is_asleep = True
        while(is_asleep):
            sleep(1)
    except:
        print('Error Occured')

def resume():
    is_asleep = False

def stop():
    #stop the execution
    try:
        print('Stop')
        exit()
    except:
        print('Error Occured')

pause = Button(frame, text = "Pause", command = pause).grid()
stop = Button(frame, text = "Stop", command = stop).grid()
resume = Button(frame, text = "Resume", command = resume).grid()

important_function()

master.mainloop()

Please help me understand where am I going wrong.

toheedNiaz
  • 1,435
  • 1
  • 10
  • 14
Aakanksha Choudhary
  • 515
  • 2
  • 5
  • 19
  • Looks like you're going to get stuck in the while loop inside `important_function` and never reach `master.mainloop()`. Does any window appear at all or just no buttons? – busybear Mar 31 '18 at 20:56
  • Just no window nor any button appears.@busybear – Aakanksha Choudhary Mar 31 '18 at 20:58
  • You'll need to execute `master.mainloop()` in order for you GUI to work/appear. I would suggest looking up how tkinter works if it's not apparent why this has to happen. I'm not sure what you are trying to accomplish with `important_function` but using `after` might be useful: https://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method – busybear Mar 31 '18 at 21:05
  • Hey thanks @busybear! I did this: `pause = Button(master, text = "Pause", command = pause).grid() \n stop = Button(master, text = "Stop", command = stop).grid()\n resume = Button(master, text = "Resume", command = resume).grid() \n master.after(100,important_function) \n master.mainloop()` And it seems to help me display the buttons! – Aakanksha Choudhary Mar 31 '18 at 21:14
  • `sleep` does exactly what it says: it puts the whole application to sleep. That means it can't process events, refresh the screen, etc. – Bryan Oakley Mar 31 '18 at 23:01

1 Answers1

0

Issues : infinite while loop important_function() which is blocking the call of master.mainloop()

Fix : I have added the fixed while(true) call and simplified the code for you. running infinite loop on a thread and pausing and resuming based on events of button clicks .

from tkinter import *
from time import sleep
import threading

master = Tk()

global is_asleep


def important_function(e):
    i = 0

    while (True):
        event_is_set = pauseEvent.wait()
        print(i)
        i = i + 1
        sleep(1)


def pause():
    # pause the execution
    # e.wait()
    pauseEvent.clear()


def resume():
    pauseEvent.set()


def stop():
    # stop the execution
    try:
        print('Stop')
        pauseEvent.clear()
        master.quit()
        sys.exit("some error message")
    except:
        print('Error Occured')


pauseButton = Button(master, text="Pause", command=pause)
pauseButton.pack()
stopButton = Button(master, text="Stop", command=stop)
stopButton.pack()
resumeButtom = Button(master, text="Resume", command=resume)
resumeButtom.pack()

pauseEvent = threading.Event()
t1 = threading.Thread(target=important_function,
                      args=(pauseEvent,))
t1.daemon =  True
t1.start()
pauseEvent.set()
mainloop()
toheedNiaz
  • 1,435
  • 1
  • 10
  • 14