PySimpleGUI 中的主题
主题用于设计漂亮的窗口。它让用户可以选择用颜色在 GUI 上展示他的创造力。使用 1 行代码即可为 GUI 窗口着色。
sg.theme('Dark Amber 5')
注意: Dark Amber是主题的名称, 5是版本或补丁。
我们调用主题属性,该属性可以设置创建 GUI 窗口时使用的颜色。它可用于为文本、背景、输入字段、按钮等着色。我们可以看到主题的预览,进行此调用以生成可用主题的预览:
import PySimpleGUI as sg
sg.theme_previewer()
输出:
主题列表和获取主题名称
除了拥有这些主题之外,人们不再需要指定确切的字符串来拥有他/她想要的主题。现在您可以添加空格、更改大小写,甚至移动单词,您仍然可以获得正确的主题。例如,主题“DarkAmber2”也可以指定为“Dark Amber 2” 。如果您无法记住名称并且出错了,您肯定会在控制台上打印出可用选项的文本列表。您还可以通过调用 theme_list 来获取主题列表
例子:
import PySimpleGUI as sg
theme_name_list = sg.theme_list()
print(theme_name_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’, ‘DarkGreen’, ‘DarkGreen1’, ‘DarkGreen2’, ‘DarkGreen3’, ‘DarkGreen4’, ‘DarkGreen5’, ‘DarkGreen6’, ‘DarkGrey’, ‘DarkGrey1’, ‘DarkGrey2’, ‘DarkGrey3’, ‘DarkGrey4’, ‘DarkGrey5’, ‘DarkGrey6’, ‘DarkGrey7’, ‘DarkPurple’, ‘DarkPurple1’, ‘DarkPurple2’, ‘DarkPurple3’, ‘DarkPurple4’, ‘DarkPurple5’, ‘DarkPurple6’, ‘DarkRed’, ‘DarkRed1’, ‘DarkRed2’, ‘DarkTanBlue’, ‘DarkTeal’, ‘DarkTeal1’, ‘DarkTeal10’, ‘DarkTeal11’, ‘DarkTeal12’, ‘DarkTeal2’, ‘DarkTeal3’, ‘DarkTeal4’, ‘DarkTeal5’, ‘DarkTeal6’, ‘DarkTeal7’, ‘DarkTeal8’, ‘DarkTeal9’, ‘Default’, ‘Default1’, ‘DefaultNoMoreNagging’, ‘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’, ‘Reddit’, ‘Reds’, ‘SandyBeach’, ‘SystemDefault’, ‘SystemDefault1’, ‘SystemDefaultForReal’, ‘Tan’, ‘TanBlue’, ‘TealMono’, ‘Topanga’]
让我们看看用于更改使用内置 PySimpleGUI 主题动态创建的 GUI 主题的Python代码。
下面是实现。
# import PySimpleGUI
import PySimpleGUI as sg
# Choose a Theme for the Layout
sg.theme('DarkTeal9')
layout = [[sg.Text('List of InBuilt Themes')],
[sg.Text('Please Choose a Theme to see Demo window')],
[sg.Listbox(values = sg.theme_list(),
size =(20, 12),
key ='-LIST-',
enable_events = True)],
[sg.Button('Exit')]]
window = sg.Window('Theme List', layout)
# This is an Event Loop
while True:
event, values = window.read()
if event in (None, 'Exit'):
break
sg.theme(values['-LIST-'][0])
sg.popup_get_text('This is {}'.format(values['-LIST-'][0]))
# Close
window.close()
输出: