欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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()

PythonGUI快速学习及使用-PySimpleGUI(四)
点击“Open Second Window”后将打开窗口2
PythonGUI快速学习及使用-PySimpleGUI(四)
实例:用户名与密码不匹配(即密码输入错误)

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快速学习及使用-PySimpleGUI(四)
输入错误:
PythonGUI快速学习及使用-PySimpleGUI(四)

相关标签: python