pysimplegui登录、注册与主界面的跳转
时间: 2023-05-19 15:02:40 浏览: 639
对于pysimplegui登录、注册与主界面的跳转,可以使用以下代码实现:
```python
import PySimpleGUI as sg
# 定义登录界面的布局
login_layout = [
[sg.Text('用户名:'), sg.Input(key='username')],
[sg.Text('密码:'), sg.Input(key='password', password_char='*')],
[sg.Button('登录'), sg.Button('注册')]
]
# 定义注册界面的布局
register_layout = [
[sg.Text('用户名:'), sg.Input(key='username')],
[sg.Text('密码:'), sg.Input(key='password', password_char='*')],
[sg.Text('确认密码:'), sg.Input(key='confirm_password', password_char='*')],
[sg.Button('注册')]
]
# 定义主界面的布局
main_layout = [
[sg.Text('欢迎使用PySimpleGUI')],
[sg.Button('退出')]
]
# 创建窗口并设置默认界面为登录界面
window = sg.Window('PySimpleGUI登录系统', login_layout)
# 创建事件循环
while True:
event, values = window.read()
# 处理登录界面的事件
if event == '登录':
# 进行登录验证
if values['username'] == 'admin' and values['password'] == '123456':
# 登录成功,关闭登录窗口,打开主界面窗口
window.close()
window = sg.Window('PySimpleGUI登录系统', main_layout)
else:
# 登录失败,弹出提示框
sg.popup('用户名或密码错误!')
# 处理注册界面的事件
elif event == '注册':
# 关闭登录窗口,打开注册窗口
window.close()
window = sg.Window('PySimpleGUI注册系统', register_layout)
# 处理注册界面的事件
elif event == '注册':
# 进行注册操作
if values['password'] == values['confirm_password']:
# 注册成功,弹出提示框
sg.popup('注册成功!')
# 关闭注册窗口,打开登录窗口
window.close()
window = sg.Window('PySimpleGUI登录系统', login_layout)
else:
# 注册失败,弹出提示框
sg.popup('两次输入的密码不一致!')
# 处理主界面的事件
elif event == '退出':
# 关闭主界面窗口,退出程序
window.close()
break
# 退出事件循环,程序结束
```