用layout设置一个按钮,点击按钮弹出询问窗口,如果确认就退出,如果取消间隔10秒再次提示询问窗口,直至确认为止
时间: 2024-10-13 17:17:42 浏览: 32
为了创建一个这样的交互式按钮,你可以使用Python的Tkinter库来构建一个简单的图形界面。首先确保已经安装了Tkinter,如果没有,可以通过`pip install tk`命令进行安装。以下是一个基本的例子:
```python
import tkinter as tk
from tkinter import messagebox
from time import sleep
def confirm_popup():
response = messagebox.askquestion("Confirmation", "Do you want to exit?", icon="warning")
if response == 'yes':
root.quit() # Exit the application when confirmed
else:
print("Confirmation cancelled, retrying in 10 seconds...")
sleep(10) # Wait for 10 seconds before prompting again
# 创建主窗口
root = tk.Tk()
root.title("Exit Confirmation")
# 设置按钮
button = tk.Button(root, text="Exit", command=confirm_popup)
button.pack(pady=10) # 添加间距
# 运行主循环
root.mainloop()
```
在这个例子中,我们定义了一个名为`confirm_popup`的函数,当按钮被点击时,这个函数会被调用。函数会显示一个带有"Confirm Exit"文本的警告对话框,用户可以选择"OK"(确认)或"Cancel"(取消)。如果选择确认,程序会结束;否则,每10秒钟会再显示一次确认对话框。
阅读全文
相关推荐



















