Krowa
Krowa

Reputation: 43

How to close the program by pressing button?

I hope you can help me with this problem..

I have the following code:

from Tkinter import *
import ttk
import time

def start():
    start_stop.config(text="QUIT", command=stop)
    for i in xrange(5):
        pbar_det.step(19.99)
        master.update()
        # Busy-wait
        time.sleep(1)
    master.quit()

def stop():
    master.quit()

master = Tk()

start_stop = Button(master, text='START', command=start)
start_stop.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)

pbar_det = ttk.Progressbar(master, orient="horizontal", length=600, mode="determinate")
pbar_det.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S)

master.mainloop()

I press the start button and the progress bar starts, the button text changes to "QUIT" and ends (and the program is closed) when the bar is full (five seconds)

How I can do so that pressing "QUIT" close the program at that time? (not waiting for the bar fills)

I hope you can help me! Thank you!

Upvotes: 1

Views: 104

Answers (1)

Jpaji Rajnish
Jpaji Rajnish

Reputation: 1501

Try this:

from Tkinter import *
import ttk
import time
import threading

def start():
    start_stop.config(text="QUIT", command=stop)
    thread = threading.Thread(target=progBar, args=())
    thread.daemon = True
    thread.start()

def progBar():
    for i in xrange(5):
        pbar_det.step(19.99)
        master.update()
        # Busy-wait
        time.sleep(1)
    master.quit()

def stop():
    master.quit()

master = Tk()

start_stop = Button(master, text='START', command=start)
start_stop.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)

pbar_det = ttk.Progressbar(master, orient="horizontal", length=600, mode="determinate")
pbar_det.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S)

master.mainloop()

Edit

Variable name fixed.

Upvotes: 1

Related Questions