基本操作
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 = window.read()
if event == None:
break
if event == pg.WIN_CLOSED:
break
if event == '确定':
print('hello')
pg.Popup("hello")
if event == '取消':
break
if event == '姓名':
print(values)
print(values[0])
print(values['name'])
window.close()
{'name': '', 0: '', 1: '', 2: ''}
设定主题
- 如何查看有效主题
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
对主题进行修改和设置
pg.theme_background_color()
'#044343'
设置主题参数
pg.Popup('设置颜色前的background颜色')
pg.theme_background_color('black')
pg.Popup('重设颜色后的background颜色')
重新定义窗口
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()
用 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()
设置文本元素的参数
pg.Text(
text='',
size=(None, None),
s=(None, None),
auto_size_text=True,
click_submits=False,
enable_events=False,
relief=None,
font=None,
text_color=None,
background_color=None,
border_width=None,
justification=None,
pad=None,
p=None,
key=None,
k=None,
right_click_menu=None,
expand_x=False,
expand_y=False,
grab=None,
tooltip=None,
visible=True,
metadata=None,
)