小编Mat*_*era的帖子

TKinter - 如何使用停止按钮停止循环?

我有这个程序每秒都会发出哔哔声,直到它停止.问题是,在我按下"开始"并开始发出哔声后,我无法点击"停止"按钮,因为窗口冻结了.欢迎任何帮助.

#!/usr/bin/python
import Tkinter, tkMessageBox, time, winsound, msvcrt

running = True

Freq = 2500
Dur = 150

top = Tkinter.Tk()
top.title('MapAwareness')
top.geometry('200x100') # Size 200, 200

def start():
    sec = 0
    while running:
        if sec % 1 == 0:
            winsound.Beep(Freq, Dur)

        time.sleep(1)
        sec += 1

def stop():
    running = False

startButton = Tkinter.Button(top, height=2, width=20, text ="Start", command = start)
stopButton = Tkinter.Button(top, height=2, width=20, text ="Stop", command = stop)

startButton.pack()
stopButton.pack()

top.mainloop()
Run Code Online (Sandbox Code Playgroud)

python tkinter

5
推荐指数
1
解决办法
6951
查看次数

标签 统计

python ×1

tkinter ×1