文本元素的更新设定方法
import PySimpleGUI as pg
layout = [[pg.T('姓名',key='name'),pg.Button('点赞')]]
window = pg.Window('演示',layout)
while True:
event, values = window.read()
if event == None:
break
if event == '点赞':
window['name'].update(
value='谢谢'
,background_color='red'
,text_color='white'
,font=('宋体',10)
,visible=True)
window.close()
layout_ch = [[pg.Button("中文"),pg.Button('English')]
,[pg.T("请输入基本信息",key='text')]
,[pg.T("姓名",key='name'),pg.In()]
,[pg.T('性别',key='sex'),pg.In()]
,[pg.T('国籍',key='nationality'),pg.In()]
,[pg.Button('确认',key='confirm'),pg.Button('取消',key='cancel')]]
en_dic = {'text':['请输入基本信息','Please enter basic information!'],
'name':['姓名','Name'],
'sex':['性别','Sex'],
'nationality':['国籍','Nationality'],
'confirm':['确认','Confirm'],
'cancel':['取消','Cancel']}
flag=0
window = pg.Window('GUI',layout_ch)
while True:
event, values = window.read()
if event == 'None':
break
if event == pg.WIN_CLOSED:
break
if event == 'English':
flag = 1
for k,v in en_dic.items():
window[k].update(en_dic[k][1])
if event == '中文':
flag = 0
for k,v in en_dic.items():
window[k].update(en_dic[k][0])
if event == 'cancel':
break
if event == 'confirm':
if flag == 0:
pg.Popup('信息录入完成!')
if flag == 1:
pg.Popup('information completed!')
window.close()
单行输入框元素设定方法
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,
)
pg.In(
default_text='',
size=(None, None),
s=(None, None),
disabled=False,
password_char='',
justification=None,
background_color=None,
text_color=None,
font=None,
tooltip=None,
border_width=None,
change_submits=False,
enable_events=False,
do_not_clear=True,
key=None,
k=None,
focus=False,
pad=None,
p=None,
use_readonly_for_disable=True,
readonly=False,
disabled_readonly_background_color=None,
disabled_readonly_text_color=None,
expand_x=False,
expand_y=False,
right_click_menu=None,
visible=True,
metadata=None,
)
单行输入框的更新设定
window[key].update() 方法示例
layout_ch = [[pg.Button("中文"),pg.Button('English')]
,[pg.T("请输入基本信息",key='text')]
,[pg.T("姓名",key='name'),pg.In(key='name_input')]
,[pg.T('性别',key='sex'),pg.In()]
,[pg.T('国籍',key='nationality'),pg.In()]
,[pg.Button('确认',key='confirm'),pg.Button('取消',key='cancel')]]
en_dic = {'text':['请输入基本信息','Please enter basic information!'],
'name':['姓名','Name'],
'sex':['性别','Sex'],
'nationality':['国籍','Nationality'],
'confirm':['确认','Confirm'],
'cancel':['取消','Cancel']}
flag=0
window = pg.Window('GUI',layout_ch)
while True:
event, values = window.read()
if event == 'None':
break
if event == pg.WIN_CLOSED:
break
if event == 'English':
flag = 1
for k,v in en_dic.items():
window[k].update(en_dic[k][1])
if event == '中文':
flag = 0
for k,v in en_dic.items():
window[k].update(en_dic[k][0])
if event == 'cancel':
break
if event == 'confirm':
'''-----------window[key].update 的演示代码----------'''
window['name_input'].update(
value='qpn',
disabled=True,
select=True)
'''---------------------------------------------------'''
window.close()
pg.In.update(
self,
value=None,
disabled=None,
select=None,
visible=None,
text_color=None,
background_color=None,
move_cursor_to='end',
password_char=None,
paste=None,
)
小练习
def check(user_pass_dict,k,v):
if k in user_pass_dict.keys():
return user_pass_dict[k] == v
layout = [[pg.T('用户名',key='username'),pg.In('请输入您的用户名',key='input_user')]
,[pg.T('密码',key='password'),pg.In(
password_char='*'
,key='input_pass')]
,[pg.B('确认',key='confirm'),pg.B('取消',key='cancel')]]
user_pass_dict = {'qpn':'123456'}
window = pg.Window('GUI',layout)
while True:
event, values = window.read()
if event == None or event == pg.WIN_CLOSED:
break
if event == 'confirm':
key = values['input_user']
value = values['input_pass']
bool_ = check(user_pass_dict,key,value)
if bool_:
pg.Popup('输入正确')
else:
pg.Popup('输入错误')
window.close()
|