在刚才的代码中增加取消登录按钮
时间: 2023-08-20 11:03:57 浏览: 46
好的,我们来修改一下代码,在登录按钮下面增加一个取消按钮。当用户点击取消按钮时,弹出一个提示框询问是否确定取消登录,如果确定则关闭登录窗口。以下是修改后的代码:
```python
import tkinter as tk
from tkinter import messagebox
def login():
username = entry_username.get()
password = entry_password.get()
if username == "admin" and password == "123456":
messagebox.showinfo(title="登录成功", message="欢迎您,管理员!")
else:
messagebox.showerror(title="登录失败", message="用户名或密码错误!")
def cancel():
result = messagebox.askyesno(title="取消登录", message="确定要取消登录吗?")
if result:
root.destroy()
root = tk.Tk()
root.title("登录窗口")
root.geometry("300x150")
label_username = tk.Label(root, text="用户名:")
label_username.grid(row=0, column=0, padx=10, pady=10)
entry_username = tk.Entry(root)
entry_username.grid(row=0, column=1)
label_password = tk.Label(root, text="密 码:")
label_password.grid(row=1, column=0, padx=10, pady=10)
entry_password = tk.Entry(root, show="*")
entry_password.grid(row=1, column=1)
btn_login = tk.Button(root, text="登录", command=login)
btn_login.grid(row=2, column=0, padx=10, pady=10)
btn_cancel = tk.Button(root, text="取消", command=cancel)
btn_cancel.grid(row=2, column=1)
root.mainloop()
```
现在,登录窗口下方有一个“取消”按钮,当用户点击时会弹出一个提示框。如果用户选择“确定”,登录窗口就会关闭。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)