pySimpleGUI-练习篇
界面英汉转换
""" 版本1.0 """
import PySimpleGUI as sg
layout = [
[sg.B("中文"), sg.B("English")],
[sg.T("请输入基本信息!", key='-TIP-')],
[sg.T('姓名', key='-NAME-', size=(8, 1)), sg.In()],
[sg.T('性别', key='-GENDER-', size=(8, 1)), sg.In()],
[sg.T('国籍', key='-NATIONALITY-', size=(8, 1)), sg.In()],
[sg.B("确认", key='-Y-'), sg.B("取消", key='-N-')]
]
window = sg.Window('Python pySimpleGUI', layout)
while True:
event, values = window.read()
print(event)
if event == None:
break
if event == 'English':
window["-TIP-"].update(value='Please enter basic infomation!')
window["-NAME-"].update(value='Name')
window["-GENDER-"].update(value='Gender')
window["-NATIONALITY-"].update(value='Nationality')
window['-Y-'].update('Confirm')
window['-N-'].update('Cancel')
if event == "中文":
window["-TIP-"].update("请输入基本信息!")
window["-NAME-"].update("姓名")
window["-GENDER-"].update("性别")
window["-NATIONALITY-"].update("国籍")
window['-Y-'].update('确定')
window['-N-'].update('取消')
window.close()
下拉菜单的更新方法
import PySimpleGUI as sg
dictionary = {'小明': 'Python', '小王': 'Java', '小花': 'C', '小壮': 'c++', '小美': 'Gui'}
list1 = [name for name in dictionary]
layout = [
[sg.Combo(list1, key='-list1-', size=(30, 6), enable_events=True, default_value='小明')],
[sg.T("语言"), sg.In(key='-INPUT-', size=(22, 1))]
]
window = sg.Window('pySimpleGUI', layout)
while True:
event, values = window.read()
print(event, values)
if not event:
break
if event == '-list1-':
window['-INPUT-'].update(dictionary[values['-list1-']])
window.close()
图片格式转换base64
import base64
f = open('图片路径', 'rb')
res = f.read()
s = base64.b64encode(res)
print(s)
f.close()
窗口上调用字典的键和值
import PySimpleGUI as sg
dictionary = {'小明': 'Python', '小王': 'Java', '小花': 'C', '小壮': 'c++', '小美': 'Gui'}
list1 = [name for name in dictionary]
layout = [
[sg.LB(list1, key='-list-', size=(30, 6), enable_events=True)],
[sg.T("语言"), sg.In(key='-INPUT-', size=25)]
]
window = sg.Window('pySimpleGUI', layout)
while True:
event, values = window.read()
print(event, values)
if not event:
break
if event == "-list-":
name = values['-list-'][0]
window["-INPUT-"].update(dictionary[name])
window.close()
多窗口
import PySimpleGUI as sg
sg.theme('GreenTan')
layout0 = [
[sg.Button("window1", size=(20, 3)), sg.Button("window2", size=(20, 3))]
]
win0 = sg.Window("window0", layout0, default_element_size=(10, 1))
win1_active = False
win2_active = False
while True:
ev0, vals0 = win0.read(timeout=10000)
if ev0 == '__TIMEOUT__':
break
win0_active = True
if ev0 == sg.WIN_CLOSED or ev0 == 'Exit':
break
if ev0 == "window1" and not win1_active:
win1_active = True
win0.hide()
layout1 = [
[sg.Button("win1")],
[sg.Input("12312", key="-12-")]
]
win1 = sg.Window("window1", layout1, default_element_size=(10, 1))
while True:
ev1, vals1 = win1.read(timeout=100)
if ev1 == sg.WIN_CLOSED or ev1 == 'Exit':
win1_active = False
win1.close()
win0.UnHide()
break
if ev0 == "window2" and not win2_active:
win2_active = True
win0.hide()
layout2 = [
[sg.Button("win2")],
[sg.Input("123123", key="-13-")]
]
win2 = sg.Window("window2", layout2, default_element_size=(10, 1))
while True:
ev2, vals2 = win2.read(timeout=100)
if ev2 == sg.WIN_CLOSED or ev2 == 'Exit':
win2_active = False
win2.close()
win0.UnHide()
break
win0.close()
登录窗口练习
import PySimpleGUI as sg
layout = [
[sg.T("用户名", size=(8, 1)), sg.In("请输入您的用户名", key='-NAME-')],
[sg.T("密码", size=(8, 1)), sg.In(key='-PASS-', tooltip='密码为3位数', password_char='*')],
[sg.B('确认输入'), sg.B('清空输入'), sg.B('保存密码'), sg.B('取消保存')],
]
window = sg.Window("Python 练习窗", layout)
user1 = {'用户名': '小明', '密码': '136'}
user2 = {'用户名': '叶寻', '密码': '135'}
users = [user1, user2]
switch = 0
while True:
event, values = window.read()
def judge(n):
for user in users:
if user['用户名'] == values["-NAME-"]:
if user['密码'] == values['-PASS-']:
sg.popup("登录成功")
if n == 0:
window['-PASS-'].update(value='')
break
else:
sg.popup("密码错误")
break
else:
sg.popup("输入错误")
if not event:
break
if event == '确认输入':
judge(switch)
if event == '清空输入':
window['-NAME-'].update(value='')
window['-PASS-'].update(value='')
if event == '保存密码':
switch = 1
if event == '取消保存':
switch = 0
window.close()
实现一个红绿灯
"""
3.点击后,更新效果
"""
import PySimpleGUI as sg
layout = [
[sg.B(
key='-BA-',
button_text="绿灯",
size=(16, 8),
button_color=('black', 'green')
)], [sg.B(
key='-BB-',
button_text="黄灯",
size=(16, 8),
button_color=('black', 'yellow')
)], [sg.B(
key='-BC-',
button_text="红灯",
size=(16, 8),
button_color=('black', 'red')
)]
]
window = sg.Window('Python Light', layout)
while True:
event, values = window.read()
print(event)
if not event:
break
def one():
window['-BA-'].update(
button_color=('black', 'grey'),
)
def two():
window['-BB-'].update(
button_color=('black', 'grey'),
)
def three():
window['-BC-'].update(
button_color=('black', 'grey'),
)
def fix():
window['-BA-'].update(button_color=('black', 'green'))
window['-BB-'].update(button_color=('black', 'yellow'))
window['-BC-'].update(button_color=('black', 'red'))
if event == '-BA-':
fix()
two()
three()
if event == '-BB-':
fix()
one()
three()
if event == '-BC-':
fix()
one()
two()
window.close()
实现一个计算器
import PySimpleGUI as sg
def button(x):
return sg.Button(x, size=(4, 2), button_color='black', pad=(2, 2), font=('黑体', 18))
layout = [
[sg.Text('', key='-SHOW-')],
[sg.InputText('', key='-INPUT-', size=(19, 2), font=('黑体', 18))],
[button(i) for i in ['AC', '(', ')', '%']],
[button(i) for i in '123+'],
[button(i) for i in '456-'],
[button(i) for i in '789X'],
[button(i) for i in '0.=÷'],
]
window = sg.Window('计算器', layout)
while True:
event, values = window.read()
print(event, values)
if not event:
break
if event in list('0123456789+-().'):
window['-INPUT-'].update(values['-INPUT-'] + event)
window['-SHOW-'].update('')
if event == 'X':
window['-INPUT-'].update(values['-INPUT-'] + '*')
window['-SHOW-'].update('')
if event == '÷':
window['-INPUT-'].update(values['-INPUT-'] + '/')
window['-SHOW-'].update('')
if event == '%':
try:
window['-INPUT-'].update(eval(values['-INPUT-'] + '/100'))
window['-SHOW-'].update('')
except:
window['-INPUT-'].update('')
window['-SHOW-'].update('输入有误')
if event == '=':
try:
window['-INPUT-'].update(eval(values['-INPUT-']))
window['-SHOW-'].update('')
except:
window['-INPUT-'].update('')
window['-SHOW-'].update('输入有误')
if event == 'AC':
window['-INPUT-'].update('')
window['-SHOW-'].update('')
window.close()
带界面的PDF合并工具
import PySimpleGUI as sg
layout = [
[sg.Text("选择文件夹:", size=(18, 1)), sg.InputText(), sg.FolderBrowse('选择')],
[sg.Text("输入合并后的文件名:", size=(18, 1)), sg.InputText(), sg.Button("开始合并")],
[sg.ML(size=(80, 15))]
]
window = sg.Window('PDF 合并工具', layout)
while True:
event, values = window.read()
print(event, values)
if not event:
break
window.close()
进度条
import PySimpleGUI as sg
import time
layout = [
[sg.Text('任务完成进度')],
[sg.ProgressBar(1000, orientation='h', size=(20, 20), key='progressbar')],
[sg.Cancel()]
]
window = sg.Window('机器人执行进度', layout)
progress_bar = window['progressbar']
print(progress_bar.__dict__)
for i in range(1000):
event, values = window.read(timeout=10)
if event == 'Cancel' or event is None:
break
progress_bar.UpdateBar(i + 1)
window.close()