第2章 文本元素+单行输入框
1. 文本元素
一般是指单行文本,也可以指多行文本,多行文本时也不会出现滚动条。
1.1 文本框(Text)属性:
Text( text="str", # str:要显示的文本。可以包含\n以实现多行。 key = "-XX-", # XX:[str,int,tuple,object],元素的唯一标识符,用于元素的定位。 size = (int,int), # 元素宽度,行高。 font = ("宋体",int), # 设定元素的字体,大小。 auto_size_text = None, # bool:元素根据文本自动调解大小。 enable_events = False, # bool:事件属性,设定为True时,点击文本发生事件。 border_width = None, # 设定relief时,用来设定边界宽度。 text_color = None, # 文本颜色。 background_color = None, # 文本背景颜色。 justification = None, # 对齐方式:left,right,center。 pad = None, # 元素间隔设定,记住左右上下对应。 right_click_menu = None, # 右击调出菜单,List[List[Union[List[str],str]]],设定后,右击此元素可以调出菜单。 grab = False, # bool:如果是真,点击此元素可以移动拖拽窗口。 tooltip = None, # str:悬浮文本,当光标置于该元素上方,会显示设定的文本。 visible = True, # bool:元素可见状态。 )
1.2 Update方法,用于元素的更新。
window[key].update()
window[key].Update()
元素更新的属性只有如下几种:
update( value = None, # str:更新文本。 background_color = None, # str:更新文本背景颜色。 text_color = None, # str:更新文本文本。 font = None, # 更新字体的名称/大小。 visible = None, # 更新元素的可见状态。 )
1.3 案例演示:
# 1)导入模块 import PySimpleGUI as sg # 2)定义布局,确定行数 layout = [ [sg.Button("中文"),sg.Button("English")], [sg.Text("基本信息!",key="-XX-")], [sg.Text("姓名",key="-XM-"),sg.InputText()], [sg.Text("性别",key="-XB-"),sg.InputText()], ] # 3)创建窗口 window = sg.Window("测试窗口",layout) # 4)事件循环 while True: event,values = window.read() print(event,values) if event == None: break if event == "English": window["-XX-"].update("Basic information!") window["-XM-"].update("Name") window["-XB-"].update("Sex") if event == "中文": window["-XX-"].update("基本信息!") window["-XM-"].update("姓名") window["-XB-"].update("性别") # 5)关闭窗口 window.close()
2. 单行输入框
2.1 单行输入框(InputText)属性:
InputText( default_text="str", # str:默认值设定,可以为空字符串。 key = "-XX-", # 元素的唯一标识符,用于元素的定位,规范 ke="-INPUT-"。 size = (int,int), # 元素宽度,行高。 font = None, # 设输入框字体的名称或者大小设定。 enable_events = False, # bool:事件属性,设定为True时,点击文本发生事件。 border_width = None, # 设定relief时,用来设定边界宽度。 text_color = None, # 输入框文本颜色。 background_color = None, # 输入框背景颜色。 justification = None, # 对齐方式:left,right,center。 pad = None, # 元素间隔设定,记住左右上下对应。 right_click_menu = None, # 右击调出菜单,List[List[Union[List[str],str]]],设定后,右击此元素可以调出菜单。 grab = False, # bool:如果是真,点击此元素可以移动拖拽窗口。 visible = True, # bool:元素可见状态。 disabled = False, # bool:元素禁用,如果为True则禁用,无法输入任何值。 password_char = "", # 密码字符,一般设置为 * 。 do_not_clear = True, # bool:输入框内容不被清除,如果为False,一发生事件,该输入框内的值会被清除。 focus = False, # bool:设置焦点,如果为真,则光标显示在此输入框。 disabled_readonly_backgroud_color = None, # str:元素禁用时的背景颜色设定。 disabled_readonly_text_color = None, # str:元素禁用时的文本颜色设定。 right_click_menu = None, # 右击按钮菜单 )
2.2 Update方法,用于元素更新。
window[key].update()
window[key].Update()
元素可以更新的属性只有如下几种:
update( value = None, # str:更新输入框内的文本。 background_color = None, # str:更新输入框背景颜色。 text_color = None, # str:更新文本颜色。 visible = False, # 更新元素的可见状态。 disabled = False, # bool:更新元素的禁用状态,如果为True,输入框变成只读状态,无法写入 select = False, # bool:元素选中,如果为True,输入框内文本被全选中。 move_cursor_to = "end", # 光标移动文本的最后,和value,focus一起试着使用吧。 )
2.3 SetFocus方法,用于元素设置焦点。
window[key].SetFocus()
window[key].set_focus()
2.4 SetTooltip方法,用于元素悬浮文本的设定。
window[key].SetTooltip("") window[key].set_tooltip("")