使用Python的Tkinter库来创建一个GUI界面,生成三个按钮控制程序的运行和结束
import tkinter as tk
from time import sleep
from tkinter import messagebox
# 定义全局变量作为程序的执行状态
paused = False
stoped=False
def pause_button():
global paused
if not paused:
# 如果未被暂停则进入暂停模式
paused = True
button.config(text="已暂停")
def continue_button():
global paused
if paused:
# 如果已经处于暂停模式则取消暂停
paused = False
button.config(text="暂停")
def stop_button():
global stoped
if not stoped:
# 如果未处于停止状态就停止
stoped = True
button.config(text="停止")
if messagebox.askyesno("确认", "你确定要退出程序吗?"):
root.destroy()#关闭面板,否则程序退出面板未关闭
root = tk.Tk()
# 创建暂停/继续按钮
button = tk.Button(root, text="暂停", command=pause_button)
button.pack()
button.place(x=10, y=30)
continue_button = tk.Button(root, text="继续", command=continue_button)
continue_button.pack()
continue_button.place(x=60, y=30)
stop_button=tk.Button(root, text="停止", command=stop_button)
stop_button.pack()
stop_button.place(x=110, y=30)
# 主循环
while True:
root.update()
if not paused:
print("程序正在运行...")
sleep(1)
if stoped:
break