python实现登陆界面
时间: 2023-09-15 09:16:34 浏览: 124
好的,下面是一个简单的 Python 登录界面实现示例:
```python
# 导入 Tkinter 库
import tkinter as tk
# 定义登录函数
def login():
# 获取输入框中的用户名和密码
username = entry_username.get()
password = entry_password.get()
# 判断用户名和密码是否正确
if username == "admin" and password == "123456":
# 登录成功,弹出提示框
tk.messagebox.showinfo("提示", "登录成功!")
else:
# 登录失败,弹出错误提示框
tk.messagebox.showerror("错误", "用户名或密码错误!")
# 创建窗口
root = tk.Tk()
root.title("登录界面")
# 创建标签
label_username = tk.Label(root, text="用户名:")
label_username.grid(row=0, column=0)
label_password = tk.Label(root, text="密码:")
label_password.grid(row=1, column=0)
# 创建输入框
entry_username = tk.Entry(root)
entry_username.grid(row=0, column=1)
entry_password = tk.Entry(root, show="*")
entry_password.grid(row=1, column=1)
# 创建登录按钮
button_login = tk.Button(root, text="登录", command=login)
button_login.grid(row=2, column=0, columnspan=2)
# 进入主循环
root.mainloop()
```
需要导入 `tkinter` 库来实现 GUI 界面,然后定义一个 `login()` 函数来判断用户名和密码是否正确。创建标签和输入框来显示和获取输入的用户名和密码,最后创建一个登录按钮来触发 `login()` 函数。运行后会弹出一个窗口,输入正确的用户名和密码点击登录即可登录成功。
阅读全文
相关推荐
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.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)