https://pysimplegui.readthedocs.io/en/latest/
pip install pysimplegui
pip3 install pysimplegui
sudo apt-get install python3-tk
import PySimpleGUI as sg
print(sg)
print(sg.version)
Popup(args=*<1 or N object>,
title=None,
button_color=None,
background_color=None,
text_color=None,
button_type=0,
auto_close=False,
auto_close_duration=None,
custom_text=(None, None),
non_blocking=False,
icon=None,
line_width=None,
font=None,
no_titlebar=False,
grab_anywhere=False,
keep_on_top=False,
location=(None, None),
any_key_closes=False,
image=None,
modal=True)
sg.popup('popup') # Shows OK button
sg.popup_ok('popup_ok') # Shows OK button
sg.popup_yes_no('popup_yes_no') # Shows Yes and No buttons
sg.popup_cancel('popup_cancel') # Shows Cancelled button
sg.popup_ok_cancel('popup_ok_cancel') # Shows OK and Cancel buttons
sg.popup_error('popup_error') # Shows red error button
sg.popup_timed('popup_timed') # Automatically closes
sg.popup_auto_close('popup_auto_close') # Same as PopupTimed
popup_scrolled(args=*<1 or N object>,
title=None,
button_color=None,
background_color=None,
text_color=None,
yes_no=False,
auto_close=False,
auto_close_duration=None,
size=(None, None),
location=(None, None),
non_blocking=False,
no_titlebar=False,
grab_anywhere=False,
keep_on_top=False,
font=None,
image=None,
modal=True)
sg.popup_scrolled(my_text)
sg.popup_scrolled(my_text, size=(80, None))
popup_no_wait(args=*<1 or N object>,
title=None,
button_type=0,
button_color=None,
background_color=None,
text_color=None,
auto_close=False,
auto_close_duration=None,
non_blocking=True,
icon=None,
line_width=None,
font=None,
no_titlebar=False,
grab_anywhere=False,
keep_on_top=False,
location=(None, None),
image=None,
modal=False)
popup_get_text(message,
title=None,
default_text="",
password_char="",
size=(None, None),
button_color=None,
background_color=None,
text_color=None,
icon=None,
font=None,
no_titlebar=False,
grab_anywhere=False,
keep_on_top=False,
location=(None, None),
image=None,
modal=True)
text = sg.popup_get_text('Title', 'Please input something')
sg.popup('Results', 'The value returned from PopupGetText', text)
popup_get_file(message,
title=None,
default_path="",
default_extension="",
save_as=False,
multiple_files=False,
file_types=(('ALL Files', '*.*'),),
no_window=False,
size=(None, None),
button_color=None,
background_color=None,
text_color=None,
icon=None,
font=None,
no_titlebar=False,
grab_anywhere=False,
keep_on_top=False,
location=(None, None),
initial_folder=None,
image=None,
modal=True)
popup_get_folder(message,
title=None,
default_path="",
no_window=False,
size=(None, None),
button_color=None,
background_color=None,
text_color=None,
icon=None,
font=None,
no_titlebar=False,
grab_anywhere=False,
keep_on_top=False,
location=(None, None),
initial_folder=None,
image=None,
modal=True)
popup_animated(image_source,
message=None,
background_color=None,
text_color=None,
font=None,
no_titlebar=True,
grab_anywhere=True,
keep_on_top=True,
location=(None, None),
alpha_channel=None,
time_between_frames=0,
transparent_color=None,
title="",
icon=None)
one_line_progress_meter(title,
current_value,
max_value,
key="OK for 1 meter",
args=*<1 or N object>,
orientation="v",
bar_color=(None, None),
button_color=None,
size=(20, 20),
border_width=None,
grab_anywhere=False,
no_titlebar=False)
for i in range(1,10000):
sg.one_line_progress_meter('My Meter', i+1, 10000, 'key','Optional message')
Debug Output(easy_print = Print = eprint)
for i in range(100):
sg.Print(i)
sg.theme('Dark Blue 3') # please make your windows colorful
layout = [ [sg.Text('Enter a Number')],
[sg.Input()],
[sg.OK()] ],
[sg.InputText(), sg.FileBrowse()],
[sg.Submit(), sg.Cancel()]]
window = sg.Window('Enter a number example', layout)
event, values = window.read()
使用菜单
# ------ Menu Definition ------ #
menu_def = [['&File', ['&Open', '&Save', 'E&xit', 'Properties']],
['&Edit', ['Paste', ['Special', 'Normal', ], 'Undo'], ],
['&Help', '&About...'], ]
layout = [
[sg.Menu(menu_def, tearoff=True)],
# 定义右键菜单
right_click_menu = ['Unused', ['Right::_Right_', '!&Click', '&Menu', 'E&xit', 'Properties']]
# 定义布局
layout = [[sg.Menu(menu_def, tearoff=False, pad=(20,1))],
# 底部菜单
[sg.ButtonMenu('ButtonMenu',key='-BMENU-', menu_def=menu_def[0])],]
# 定义 Window
window = sg.Window("Windows-like program",layout,
default_element_size=(12, 1),
grab_anywhere=True, # 非阻塞
right_click_menu=right_click_menu, # 添加右键菜单
default_button_element_size=(12, 1)
)
'Right::_Right_' 是指定该菜单的 Key 的方式。!禁用
Theme Name
There are 130 themes available. You can preview these themes by calling theme_previewer() which will create a LARGE window displaying all of the color themes available. Default is Dark Blue 3
Dark Color #
or
Light Color #
DarkAmber
# print(sg.ListOfLookAndFeelValues())
# ['SystemDefault', 'Reddit', 'Topanga', 'GreenTan', 'Dark', 'LightGreen', 'Dark2', 'Black', 'Tan', 'TanBlue',
# 'DarkTanBlue', 'DarkAmber', 'DarkBlue', 'Reds', 'Green', 'BluePurple', 'Purple', 'BlueMono', 'GreenMono',
# 'BrownBlue', 'BrightColors', 'NeutralBlue', 'Kayak', 'SandyBeach', 'TealMono']
sg.theme()窗口样式
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()
Making your window modal
To make a Modal Wio=ndow you have 2 options.
Set the moodel=True parameter in your Window calls.
Call the method Window.make_modal() to chance a window from non-modal to modal. There is no modal to non-modal. Don't see the need for one. If one comes up, sure!
MPORTANT - Many of the Window methods require you to either call Window.read or Window.Finalize (or set finalize=True in your Window call) before you call the method. This is because these 2 calls are what actually creates the window using the underlying GUI Framework. Prior to one of those calls, the methods are likely to crash as they will not yet have their underlying widgets created.
"Elements" are the building blocks used to create windows. Some GUI APIs use the term "Widget" to describe these graphic elements.
Text
Single Line Input
Buttons including these types:
File Browse
Folder Browse
Calendar picker
Date Chooser
Read window
Close window ("Button" & all shortcut buttons)
Realtime
Checkboxes
Radio Buttons
Listbox
Slider
Multi-line Text Input/Output
Multi-line Text Output (not on tkinter version)
Scroll-able Output
Vertical Separator
Progress Bar
Option Menu
Menu
ButtonMenu
Frame
Column
Graph
Image
Table
Tree
Tab, TabGroup
StatusBar
Pane
Stretch (Qt only)
Sizer (plain PySimpleGUI only)
Text Element | T == Txt == Text
Window.FindElement(key) shortened to Window[key]
window['-MULTILINE KEY-'].print('My variables are', a, b, c) # Routed to your multiline element
Text Input Element | InputText == Input == In
Combo Element | Combo == InputCombo == DropDown == Drop
layout = [[sg.Listbox(values=['Listbox 1', 'Listbox 2', 'Listbox 3'], size=(30, 6))]]
layout = [[sg.Slider(range=(1,500),default_value=222,size=(20,15),orientation='horizontal',font=('Helvetica', 12))]]
layout = [[sg.Radio('My first Radio!', "RADIO1", default=True),sg.Radio('My second radio!', "RADIO1")]]
Checkbox Element | CBox == CB == Check
layout = [[sg.Checkbox('My first Checkbox!', default=True), sg.Checkbox('My second Checkbox!')]]
layout = [[sg.Spin([i for i in range(1,11)], initial_value=1), sg.Text('Volume level')]]
layout = [[sg.Image(r'C:\PySimpleGUI\Logos\PySimpleGUI_Logo_320.png')],]
Button Element
Button= ReadButton = RButton = ReadFormButton (Use Button, others are old methods)
CloseButton = CButton
RealtimeButton
DummyButton
layout = [[sg.Button('Ok'), sg.Button('Cancel')]] layout = [[sg.Ok(), sg.Cancel()]]
Button Element Shortcuts
OK
Ok
Submit
Cancel
Yes
No
Exit
Quit
Help
Save
SaveAs
Open
Chooser" Buttons
These buttons are used to show dialog boxes that choose something like a filename, date, color, etc.. that are filled into an InputText Element (or some other "target".... see below regarding targets)
CalendarButton
ColorChooserButton
FileBrowse
FilesBrowse
FileSaveAs
FolderBrowse
he code for the entire window could be:
layout = [[sg.T('Source Folder')],
[sg.In()],
[sg.FolderBrowse(target=(-1, 0)), sg.OK()]]
or if using keys, then the code would be:
layout = [[sg.T('Source Folder')],
[sg.In(key='input')],
[sg.FolderBrowse(target='input'), sg.OK()]]
sg.Button('Restart Song', button_color=(sg.theme_background_color(), sg.theme_background_color()),
image_filename=image_restart, image_size=(50, 50), image_subsample=2, border_width=0)
VerticalSeparator(pad=None)
sg.OneLineProgressMeter('My Meter', i+1, 1000, 'key', 'Optional message')
Output(size=(80,20))
Qt Designer
https://github.com/nngogol/PySimpleGUIDesigner
pip install PySimpleGUIDesigner
Use GUI(by default):
PySimpleGUIDesigner
Use CLI:
PySimpleGUIDesigner -xml “~/folder1/test.ui” -ob “somegroupBox”
#removing (if installed) PySimpleGUIDesigner: 源码安装
pip uninstall -y PySimpleGUIDesigner
mkdir psgdesigner
cd psgdesigner
git clone https://github.com/nngogol/PySimpleGUIDesigner
python3 -m PySimpleGUIDesigner
python3 main.py –xmlfile=”~/folder1/test.ui” –objname=”somegroupBox”
#a bit shorter command:
python3 main.py -xml “~/folder1/test.ui” -ob “somegroupBox”