PythonGUI快速学习及使用-PySimpleGUI(四)
程序员文章站
2022-04-11 17:48:48
...
多个窗口
import PySimpleGUI as sg
# Create some widgets
ok_btn = sg.Button('Open Second Window')
cancel_btn = sg.Button('Cancel')
layout = [[ok_btn, cancel_btn]]
# Create the first Window
window = sg.Window('Window 1', layout)
win2_active = False
# Create the event loop
while True:
event1, values1 = window.read(timeout=100)
if event1 in (None, 'Cancel'):
# User closed the Window or hit the Cancel button
break
if not win2_active and event1 == 'Open Second Window':
win2_active = True
layout2 = [[sg.Text('Window 2')],
[sg.Button('Exit')]]
window2 = sg.Window('Window 2', layout2)
if win2_active:
events2, values2 = window2.Read(timeout=100)
if events2 is None or events2 == 'Exit':
win2_active = False
window2.close()
window.close()
点击“Open Second Window”后将打开窗口2
实例:用户名与密码不匹配(即密码输入错误)
import PySimpleGUI as sg
id_key_dict = dict()
id_key_dict["123"] = '456'
# 创建第一个窗口
layout = [[sg.T('输入您的ID'), sg.In(key='-ID-')],
[sg.T('输入您的密码'), sg.In(key='-KEY-')],
[sg.B('登录'), sg.B('取消')]]
window = sg.Window('登录窗口', layout)
win2_active = False
win3_active = False
# 创建事件循环
while True:
event1, values1 = window.read(timeout=100)
a = values1['-ID-']
b = values1['-KEY-']
if event1 in (None, '取消'):
# 用户关闭了窗口或点击了“取消”
break
if not win2_active and event1 == '登录':
if (a, b) != (a, id_key_dict[a]):
win2_active = True
layout2 = [[sg.Text('密码错误!')],
[sg.Button('退出')]]
window2 = sg.Window('输入错误', layout2)
else:
win3_active = True
layout2 = [[sg.Text('登录成功!')],
[sg.Button('退出')]]
window3 = sg.Window('输入正确', layout2)
if win2_active:
events2, values2 = window2.Read(timeout=100)
if events2 is None or events2 == '退出':
win2_active = False
window2.close()
if win3_active:
events3, values3 = window3.Read(timeout=100)
if events3 is None or events3 == '退出':
win3_active = False
window2.close()
window.close()
输入正确:
输入错误:
上一篇: PythonGUI学习笔记
推荐阅读
-
使用jQuery快速高效制作网页交互特效——04 第四章JavaScript对象及初识面向对象
-
使用jQuery快速高效制作网页交互特效——04 第四章JavaScript对象及初识面向对象
-
PythonGUI快速学习及使用-PySimpleGUI(一)
-
PythonGUI快速学习及使用-PySimpleGUI(二)
-
PythonGUI快速学习及使用-PySimpleGUI(三)
-
PythonGUI快速学习及使用-PySimpleGUI(四)
-
【Silverlight】Bing Maps学习系列(四):使用图钉层(Pushpin layer)及地图图层(MapLayer) BingSilverlightBlendMicrosoft
-
【深度学习】使用清华镜像快速下载TensorFlow各版本及已安装版本查看