基本操作
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: ''}
![Python图形界面编程 PySimpleGUI(1)_第1张图片](http://img.e-com-net.com/image/info8/fe178fbd539a47769d50148753c008b4.jpg)
设定主题
- 如何查看有效主题
![在这里插入图片描述](http://img.e-com-net.com/image/info8/a22202cc3d554fbc8e470e4461d125f4.jpg)
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张图片](http://img.e-com-net.com/image/info8/75d5b9f8e40e44dfb65bbada7dac999c.jpg)
对主题进行修改和设置
pg.theme_background_color()
'#044343'
设置主题参数
pg.Popup('设置颜色前的background颜色')
![Python图形界面编程 PySimpleGUI(1)_第3张图片](http://img.e-com-net.com/image/info8/2db3ec30fa1c41889e41d323b9747caf.jpg)
pg.theme_background_color('black')
pg.Popup('重设颜色后的background颜色')
![Python图形界面编程 PySimpleGUI(1)_第4张图片](http://img.e-com-net.com/image/info8/8bb42676792646ea9ea49d99c3a39137.jpg)
重新定义窗口
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张图片](http://img.e-com-net.com/image/info8/ef6c63cf5ac641b59e53f66758a1d115.jpg)
用 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张图片](http://img.e-com-net.com/image/info8/8e97cf8dceb1469c895958bd71b37428.jpg)
设置文本元素的参数
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,
)