1

Ive been trying to make a program with 2 buttons, pressing one of them will start an infinite loop and pressing the other one will stop it.

All the methods ive tried will just pause the loop.

from Tkinter import *
import time
s = 0

def stopit():
    s = 1
    print "stoped"
#
def callback():

    if s == 0:
        while True:
            print "called the callback!"
            time.sleep(3)
            if s == 1:
                break
            #
        #
    #
#

root = Tk()

def main():


    # create a menu
    menu = Menu(root)
    root.config(menu=menu)

    b = Button(root, command=stopit)
    b.pack()


    filemenu = Menu(menu)
    menu.add_cascade(label="File", menu=filemenu)
    filemenu.add_command(label="New", command=callback)
    filemenu.add_command(label="Open...", command=callback)
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=callback)

    helpmenu = Menu(menu)
    menu.add_cascade(label="Help", menu=helpmenu)
    helpmenu.add_command(label="About...", command=callback)

    mainloop()
    time.sleep(3)
#

main()
4
  • What is the question? Also, please post the relevant code. Commented Dec 8, 2013 at 18:27
  • Im trying to make it so if you press one button it will run a loop and if you press another button it will stop it. Commented Dec 8, 2013 at 18:37
  • @tobias_k. What do you mean? I want it so it breaks out of the loop instead of just pausing. Commented Dec 8, 2013 at 18:38
  • 1
    When I run the program, the GUI just freezes, because the callback method never finishes, so I can not press the "stop" button at all. You should try to use Tkinter.after instead. Commented Dec 8, 2013 at 18:43

1 Answer 1

2

There are two problems with your code:

  • Your callback method never finishes (due to the infinite loop), causing the GUI to freeze. Instead, use the after method to schedule another execution of callback after the method finishes.
  • Your stopit method creates a local variable s instead of changing the global one. Use the global keyword to fix this.

Change the two methods to something like this, and it should work:

def stopit():
    global s
    s = 1
    print "stopped"

def callback():
    if s == 0:
        print "called the callback!"
        root.after(3000, callback)
Sign up to request clarification or add additional context in comments.

2 Comments

I tried this pastebin.com/gmwc9nTx but i got this error Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1473, in call return self.func(*args) File "gui.py", line 12, in callback if s == 0: NameError: global name 's' is not defined
I fixed the error by doing this pastebin.com/SwknxkCs but now when i click stop it just says stoped but keeps going on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.