如何按按钮关闭程序?

时间:2015-12-18 03:54:21

标签: python linux button tkinter

我希望你能帮我解决这个问题。

我有以下代码:

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()

我按下开始按钮,进度条开始,按钮文字变为" QUIT"当栏满(五秒)时结束(程序关闭)

我怎样才能按下" QUIT"当时关闭程序? (不等待酒吧填充)

我希望你能帮助我! 谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

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()

修改

变量名称已修复。