使用前必须先安装 PySimpleGUI
https://pysimplegui.readthedocs.io/en/latest/cookbook/#2020-updates-are-in-progress
pip inatall PySimpleGUI
由于官网没有中文教程 只能自己看看别人的项目
和官方的文档
由于PySimpleGUI是个人维护,不过开发得比较完善了
官网文档没有中文 基本看不懂,只能一边看原文一边看翻译,一边看官网示例,一个个功能测试
现在把自己测试的记录下来
#首先设置窗口样式
sg.theme('BluePurple')
#设置窗体里面的内容,是以列表样式
layout = [[sg.Text('请输入站号:',size=15,1)),sg.InputText(key='-INzhaohao-',size=(20,1))],
[sg.Button('查询'), sg.Exit()]]
#窗口实例化 并设置窗口名,把内容放进去
window = sg.Window('区域站要素查询', layout)
#主题循环
while True:
#读取按键和各个插件的值 window.read()窗口显示
#event获取按键值
#values[‘控件的KEY’]
event, values = window.read()
#print(event, values)
if event in (None, 'Exit'):
break
elif event == '查询':
shuju=xiaoshishuju(str(values['-INzhaohao-']),str(values['-INriqi-']))
if shuju == None:
sg.popup('提示','找不到'+str(values['-INzhaohao-'])+'站的数据')
else:
xianshushuju(shuju)
#窗口关闭
window.close()
sg.theme('BluePurple')
要查看您的PySimpleGUI版本的上述预览,请进行以下调用以生成所有可用主题的预览:
sg.theme_previewer()
如果您想在系统上看到一个窗口,如上面的主题预览屏幕截图,那么进行此调用,您将看到相同的窗口:
import PySimpleGUI as sg
sg.preview_all_look_and_feel_themes()
您还可以通过致电获取主题名称列表 theme_list
import PySimpleGUI as sg
theme_name_list = sg.theme_list()
sg.Text()和sg.T()是一样的效果
sg.Text(‘请输入站号:’,size=(15,1))
sg.Input()、sg.In()、InputText()效果一样
InputText(key=’-INzhaohao-’,size=(20,1))
sg.Button()、sg.B() 效果一样
您可以通过enable_events=True在Button调用中进行设置来启用其他
sg.OK()??
sg.Button(‘查询’)
sg.Exit()
Sites_dict=[]
sg.Listbox(values=Sites_dict, size=(20, 12), key=’-LIST-’, enable_events=True)
enable_events?未知属性
Sites_dict=[]
layout = [[sg.Text('Theme Browser')],
[sg.Text('Click a Theme color to see demo window')],
[sg.Listbox(values=Sites_dict, size=(20, 12), key='-LIST-', enable_events=True)],
[sg.Button('Ok'),sg.Button('Exit')]]
window = sg.Window('Theme Browser', layout)
while True: # Event Loop
event, values = window.read()
if event in (None, 'Exit'):
break
#sg.theme(values['-LIST-'][0])
elif event == 'Ok':
print(values['-LIST-'][0])
sg.popup('This is {}'.format(values['-LIST-'][0]))
window.close()
window['-OUTPUT-']
window.FindElement('-OUTPUT-')
window.find_element('-OUTPUT-')
window.Element('-OUTPUT-')
window['-OUTPUT-'].update(values['-IN-'])
window[’-OUTPUT-’]:获取key为’-OUTPUT-‘的空间
.update()方法更新文字内容
values[’-IN-’]:获取key为’-IN-'的内容文字
values[’-LIST-’][0]控件内容为列表的值
sg.popup(‘窗口标题’,‘提醒内容’)
import PySimpleGUI as sg
sg.theme('Light Blue 2')
layout = [[sg.Text('Enter 2 files to comare')],
[sg.Text('File 1', size=(8, 1)), sg.Input(), sg.FileBrowse()],
[sg.Text('File 2', size=(8, 1)), sg.Input(), sg.FileBrowse()],
[sg.Submit(), sg.Cancel()]]
window = sg.Window('File Compare', layout)
event, values = window.read()
window.close()
print(f'You clicked {event}')
print(f'You chose filenames {values[0]} and {values[1]}')
[sg.Text('Your Folder', size=(15, 1), auto_size_text=False, justification='right'),
sg.InputText('Default Folder'), sg.FolderBrowse()],
sg.Checkbox(‘My second checkbox!’, default=True)
default=True 默认选取
[sg.Radio('My first Radio! ', “RADIO1”, default=True), sg.Radio(‘My second Radio!’, “RADIO1”)],
"RADIO1"好像是都要一样的
default=True 默认选取
[sg.Multiline(default_text=‘This is the default Text shoulsd you decide not to type anything’,
scale=(2, 10))],
sg.InputCombo([‘Combobox 1’, ‘Combobox 2’], size=(20, 3))
sg.Print(‘Re-routing the stdout’, do_not_reroute_stdout=False)
print(‘This is a normal print that has been re-routed.’)
同时将print和sg.Print输出文本到您的调试窗口。
仅当不将标准输出重新路由到调试窗口时,才可以进行彩色打印。
如果彩色打印很重要,请不要将标准输出重新路由到调试窗口。仅使用Print对的输出,而无需更改标准输出设置,您将可以进行彩色打印。
import PySimpleGUI as sg
sg.Print('This text is white on a green background', text_color='white', background_color='green', font='Courier 10')
sg.Print('The first call sets some window settings like font that cannot be changed')
sg.Print('This is plain text just like a print would display')
sg.Print('White on Red', background_color='red', text_color='white')
sg.Print('The other print', 'parms work', 'such as sep', sep=',')
sg.Print('To not extend a colored line use the "end" parm', background_color='blue', text_color='white', end='')
sg.Print('\nThis line has no color.')
window[’-ML1-’+sg.WRITE_ONLY_KEY].print(1,2,3,4,end=’’, text_color=‘red’, background_color=‘yellow’)
sg.Slider(range=(1, 100), orientation=‘h’, size=(34, 20), default_value=85)
(range=(1, 100):数值范围
orientation=拖动方向 ‘h’ :横向 ‘v’:竖向
size=(34, 20):大小
default_value=85默认值
sg.InputOptionMenu((‘Menu Option 1’, ‘Menu Option 2’, ‘Menu Option 3’))
menu_def = [['File', ['Open', 'Save', 'Exit', 'Properties']],
['Edit', ['Paste', ['Special', 'Normal', ], 'Undo'], ],
['Help', 'About...'], ]
layout = [
[sg.Menu(menu_def, tearoff=True)]
window = sg.Window('Everything bagel', layout, default_element_size=(40, 1), grab_anywhere=False)
event, values = window.read()
window.close()
column1 = [[sg.Text('Column 1', background_color='#F7F3EC', justification='center', size=(10, 1))],
[sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 1')],
[sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 2')],
[sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 3')]]
layout = [ sg.Column(column1, background_color='#F7F3EC')],
[sg.Listbox(values=('Listbox 1', 'Listbox 2', 'Listbox 3'), size=(30, 3)),
sg.Frame('Labelled Group',[[
sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=25),
sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=75),
sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=10),
sg.Column(column1, background_color='#F7F3EC')]])],
sg.ProgressBar(1000, orientation=‘h’, size=(20, 20), key=‘progbar’)
1000:数值
orientation=‘h’/v 方向
当较小的元素左侧有一个较高的元素时,则需要一列。
在此示例中,左侧有一个3行高的列表框。在它的右边是3个单行文本和输入。这3行在Column Element中。
为了更轻松地在窗口中查看“列”,“列”背景已被涂成蓝色阴影。由于蓝色阴影,该代码比正常的单词更难读。列中的每个元素都需要设置颜色以匹配蓝色背景。
import PySimpleGUI as sg
# Demo of how columns work
# GUI has on row 1 a vertical slider followed by a COLUMN with 7 rows
# Prior to the Column element, this layout was not possible
# Columns layouts look identical to GUI layouts, they are a list of lists of elements.
sg.ChangeLookAndFeel('BlueMono')
# Column layout
col = [[sg.Text('col Row 1', text_color='white', background_color='blue')],
[sg.Text('col Row 2', text_color='white', background_color='blue'), sg.Input('col input 1')],
[sg.Text('col Row 3', text_color='white', background_color='blue'), sg.Input('col input 2')]]
layout = [[sg.Listbox(values=('Listbox Item 1', 'Listbox Item 2', 'Listbox Item 3'), select_mode=sg.LISTBOX_SELECT_MODE_MULTIPLE, size=(20,3)), sg.Column(col, background_color='blue')],
[sg.Input('Last input')],
[sg.OK()]]
# Display the Window and get values
event, values = sg.Window('Compact 1-line Window with column', layout).Read()
sg.popup(event, values, line_width=200)