简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM