Python图形界面编程 PySimpleGUI(1)

基本操作

  • 定义 layout
  • 创建窗口
  • 循环事件
  • 关闭窗口
import PySimpleGUI as pg
'''定义布局,确定行数
    创建窗口
    事件循环
    关闭窗口'''
layout = [[pg.Text("姓名",enable_events=True),pg.In(key='name')]
          ,[pg.Text("年龄"),pg.In()]
          ,[pg.Text("性别"),pg.In()]
          ,[pg.Text("籍贯"),pg.In()]
         ,[pg.Button("确定"),pg.Button("取消")]]
layout
[[,
  ],
 [,
  ],
 [,
  ],
 [,
  ],
 [,
  ]]
# 创建窗口
window = pg.Window("GUI",layout)

# 循环事件
while True:
    # event 是触发的事件一般有鼠标触发
    # values 是人为输入的内容
    event, values = window.read()
    
    if event == None:
        break
    if event == pg.WIN_CLOSED:  # 一定要写这一个语句,否则点 x 关闭窗口也会继续消耗内存
        break
    if event == '确定':
        print('hello')
        pg.Popup("hello") # 弹窗任务
#         break
    if event == '取消':
        break
    if event == '姓名':
        print(values)
        print(values[0])  # 当不指定 key 的时候可以返回对应的值,指定 key 的时候只能通过 key 索引
        print(values['name'])
        
# 关闭窗口
window.close()
{'name': '', 0: '', 1: '', 2: ''}

Python图形界面编程 PySimpleGUI(1)_第1张图片

  • 其中,‘姓名’被定义成一个事件,是可以触发行为的

设定主题

  • 如何查看有效主题
    在这里插入图片描述
pg.theme_previewer()

  • 如何获取有效主题
pg.theme_list()
['Black',
 'BlueMono',
 'BluePurple',
 'BrightColors',
 'BrownBlue',
 'Dark',
 'Dark2',
 'DarkAmber',
 'DarkBlack',
 'DarkBlack1',
 'DarkBlue',
 'DarkBlue1',
 'DarkBlue10',
 'DarkBlue11',
 'DarkBlue12',
 'DarkBlue13',
 'DarkBlue14',
 'DarkBlue15',
 'DarkBlue16',
 'DarkBlue17',
 'DarkBlue2',
 'DarkBlue3',
 'DarkBlue4',
 'DarkBlue5',
 'DarkBlue6',
 'DarkBlue7',
 'DarkBlue8',
 'DarkBlue9',
 'DarkBrown',
 'DarkBrown1',
 'DarkBrown2',
 'DarkBrown3',
 'DarkBrown4',
 'DarkBrown5',
 'DarkBrown6',
 'DarkBrown7',
 'DarkGreen',
 'DarkGreen1',
 'DarkGreen2',
 'DarkGreen3',
 'DarkGreen4',
 'DarkGreen5',
 'DarkGreen6',
 'DarkGreen7',
 'DarkGrey',
 'DarkGrey1',
 'DarkGrey10',
 'DarkGrey11',
 'DarkGrey12',
 'DarkGrey13',
 'DarkGrey14',
 'DarkGrey2',
 'DarkGrey3',
 'DarkGrey4',
 'DarkGrey5',
 'DarkGrey6',
 'DarkGrey7',
 'DarkGrey8',
 'DarkGrey9',
 'DarkPurple',
 'DarkPurple1',
 'DarkPurple2',
 'DarkPurple3',
 'DarkPurple4',
 'DarkPurple5',
 'DarkPurple6',
 'DarkPurple7',
 'DarkRed',
 'DarkRed1',
 'DarkRed2',
 'DarkTanBlue',
 'DarkTeal',
 'DarkTeal1',
 'DarkTeal10',
 'DarkTeal11',
 'DarkTeal12',
 'DarkTeal2',
 'DarkTeal3',
 'DarkTeal4',
 'DarkTeal5',
 'DarkTeal6',
 'DarkTeal7',
 'DarkTeal8',
 'DarkTeal9',
 'Default',
 'Default1',
 'DefaultNoMoreNagging',
 'GrayGrayGray',
 'Green',
 'GreenMono',
 'GreenTan',
 'HotDogStand',
 'Kayak',
 'LightBlue',
 'LightBlue1',
 'LightBlue2',
 'LightBlue3',
 'LightBlue4',
 'LightBlue5',
 'LightBlue6',
 'LightBlue7',
 'LightBrown',
 'LightBrown1',
 'LightBrown10',
 'LightBrown11',
 'LightBrown12',
 'LightBrown13',
 'LightBrown2',
 'LightBrown3',
 'LightBrown4',
 'LightBrown5',
 'LightBrown6',
 'LightBrown7',
 'LightBrown8',
 'LightBrown9',
 'LightGray1',
 'LightGreen',
 'LightGreen1',
 'LightGreen10',
 'LightGreen2',
 'LightGreen3',
 'LightGreen4',
 'LightGreen5',
 'LightGreen6',
 'LightGreen7',
 'LightGreen8',
 'LightGreen9',
 'LightGrey',
 'LightGrey1',
 'LightGrey2',
 'LightGrey3',
 'LightGrey4',
 'LightGrey5',
 'LightGrey6',
 'LightPurple',
 'LightTeal',
 'LightYellow',
 'Material1',
 'Material2',
 'NeutralBlue',
 'Purple',
 'Python',
 'Reddit',
 'Reds',
 'SandyBeach',
 'SystemDefault',
 'SystemDefault1',
 'SystemDefaultForReal',
 'Tan',
 'TanBlue',
 'TealMono',
 'Topanga']
  • 如何设定主题
    • 直接 pg.theme(主题名称) 来设定喜欢的主题
    • 通过 pg.theme(’’)设置随机主题
pg.theme('Black')
'Black'
pg.theme('')
pg.Popup('演示')
** Warning -  Theme is not a valid theme. Change your theme call. **
valid values are ['Black', 'BlueMono', 'BluePurple', 'BrightColors', 'BrownBlue', 'Dark', 'Dark2', 'DarkAmber', 'DarkBlack', 'DarkBlack1', 'DarkBlue', 'DarkBlue1', 'DarkBlue10', 'DarkBlue11', 'DarkBlue12', 'DarkBlue13', 'DarkBlue14', 'DarkBlue15', 'DarkBlue16', 'DarkBlue17', 'DarkBlue2', 'DarkBlue3', 'DarkBlue4', 'DarkBlue5', 'DarkBlue6', 'DarkBlue7', 'DarkBlue8', 'DarkBlue9', 'DarkBrown', 'DarkBrown1', 'DarkBrown2', 'DarkBrown3', 'DarkBrown4', 'DarkBrown5', 'DarkBrown6', 'DarkBrown7', 'DarkGreen', 'DarkGreen1', 'DarkGreen2', 'DarkGreen3', 'DarkGreen4', 'DarkGreen5', 'DarkGreen6', 'DarkGreen7', 'DarkGrey', 'DarkGrey1', 'DarkGrey10', 'DarkGrey11', 'DarkGrey12', 'DarkGrey13', 'DarkGrey14', 'DarkGrey2', 'DarkGrey3', 'DarkGrey4', 'DarkGrey5', 'DarkGrey6', 'DarkGrey7', 'DarkGrey8', 'DarkGrey9', 'DarkPurple', 'DarkPurple1', 'DarkPurple2', 'DarkPurple3', 'DarkPurple4', 'DarkPurple5', 'DarkPurple6', 'DarkPurple7', 'DarkRed', 'DarkRed1', 'DarkRed2', 'DarkTanBlue', 'DarkTeal', 'DarkTeal1', 'DarkTeal10', 'DarkTeal11', 'DarkTeal12', 'DarkTeal2', 'DarkTeal3', 'DarkTeal4', 'DarkTeal5', 'DarkTeal6', 'DarkTeal7', 'DarkTeal8', 'DarkTeal9', 'Default', 'Default1', 'DefaultNoMoreNagging', 'GrayGrayGray', 'Green', 'GreenMono', 'GreenTan', 'HotDogStand', 'Kayak', 'LightBlue', 'LightBlue1', 'LightBlue2', 'LightBlue3', 'LightBlue4', 'LightBlue5', 'LightBlue6', 'LightBlue7', 'LightBrown', 'LightBrown1', 'LightBrown10', 'LightBrown11', 'LightBrown12', 'LightBrown13', 'LightBrown2', 'LightBrown3', 'LightBrown4', 'LightBrown5', 'LightBrown6', 'LightBrown7', 'LightBrown8', 'LightBrown9', 'LightGray1', 'LightGreen', 'LightGreen1', 'LightGreen10', 'LightGreen2', 'LightGreen3', 'LightGreen4', 'LightGreen5', 'LightGreen6', 'LightGreen7', 'LightGreen8', 'LightGreen9', 'LightGrey', 'LightGrey1', 'LightGrey2', 'LightGrey3', 'LightGrey4', 'LightGrey5', 'LightGrey6', 'LightPurple', 'LightTeal', 'LightYellow', 'Material1', 'Material2', 'NeutralBlue', 'Purple', 'Python', 'Reddit', 'Reds', 'SandyBeach', 'SystemDefault', 'SystemDefault1', 'SystemDefaultForReal', 'Tan', 'TanBlue', 'TealMono', 'Topanga']
Instead, please enjoy a random Theme named DarkGreen4

Python图形界面编程 PySimpleGUI(1)_第2张图片

对主题进行修改和设置

pg.theme_background_color()
'#044343'

设置主题参数

pg.Popup('设置颜色前的background颜色')

Python图形界面编程 PySimpleGUI(1)_第3张图片

pg.theme_background_color('black')
pg.Popup('重设颜色后的background颜色')

Python图形界面编程 PySimpleGUI(1)_第4张图片

重新定义窗口

for 循环快速定义元素

用 for 循环遍历字符串产生布局元素

# 用 for 循环遍历字符串产生布局元素
pg.theme_background_color('blue')
layout = [[pg.Text(i) for i in 'abcd']]  # 这里是双层列表,千万别弄错了
window = pg.Window('字符串定义元素',layout)
while True:
    event, values = window.read()
    if event == None:
        break
window.close()

Python图形界面编程 PySimpleGUI(1)_第5张图片

用 for 循环遍历列表产生布局元素

pg.theme_background_color('blue')
layout = [[pg.Text(i)] for i in ['北京','上海','深圳','厦门']]  # 这里是双层列表,千万别弄错了
window = pg.Window('列表定义元素',layout)
while True:
    event, values = window.read()
    if event == None:
        break
window.close()

Python图形界面编程 PySimpleGUI(1)_第6张图片

设置文本元素的参数

 pg.Text(
    text='',           # 文本内容
    size=(None, None), # 窗口大小
    s=(None, None),   
    auto_size_text=True, # 窗口自适应显示
    click_submits=False,
    enable_events=False,  # 允许文本产生事件
    relief=None,          # 浮雕设计 "RELIEF_" - `RELIEF_RAISED, RELIEF_SUNKEN, RELIEF_FLAT, RELIEF_RIDGE, RELIEF_GROOVE, RELIEF_SOLID`
    font=None,            # ('宋体',20) 字体、大小
    text_color=None,      # 字体颜色
    background_color=None,# 文本背景颜色
    border_width=None,    # 设定 relief 后用来设定边界宽度
    justification=None,   # 对齐方式 'left', 'right', 'center'
    pad=None,             # 元素间隔设定,
    p=None,
    key=None,             # 给一个键用来索引数据
    k=None,
    right_click_menu=None, # 右击显示下拉菜单![1,[1,2,3]]
    expand_x=False,
    expand_y=False,
    grab=None,          # 如果为 True 那么点击此元素可以拖拽窗口
    tooltip=None,       # 光标放上的时候显示提示文本
    visible=True,       # 元素是否可见
    metadata=None,
)

你可能感兴趣的:(日常学习,python自动化和GUI编程,python,postgresql,开发语言)