sg: window对象 常用方法
在 PySimpleGUI 中,window 对象除了 .get() 和 .update() 方法外,还提供了许多其他方法来控制窗口的行为和交互。以下是一些常用的方法:
1. 窗口生命周期管理
-
.close()
关闭窗口并释放资源。window.close() -
.finalize()
在窗口显示前强制完成布局初始化(通常用于动态更新后)。window.finalize() -
.hide()/.un_hide()
隐藏或显示窗口(不关闭)。window.hide() window.un_hide() -
.disappear()/.reappear()
类似.hide()和.un_hide(),但可能更高效。
2. 事件循环控制
-
.read(timeout=None, timeout_key='__TIMEOUT__')
读取事件,可选超时设置(毫秒)。event, values = window.read(timeout=1000) # 1秒超时 -
.perform_long_operation(func, callback)
在后台线程运行函数,完成后触发回调。
3. 元素操作
-
.find_element(key)
通过键(key)获取窗口中的元素对象。element = window.find_element('-INPUT-') -
.extend_layout(container, layout)
动态扩展布局(如向列或框架添加新元素)。window.extend_layout(window['-COLUMN-'], [[sg.Text('New text')]]) -
.bring_to_front()
将窗口置顶显示。
4. 状态与属性
-
.set_title(title)
修改窗口标题。window.set_title('新标题') -
.get_screen_size()
获取屏幕分辨率。screen_width, screen_height = window.get_screen_size() -
.current_location()
获取窗口当前坐标(左上角)。x, y = window.current_location() -
.move(x, y)
移动窗口到指定坐标。window.move(100, 100)
5. 键盘/鼠标控制
-
.bind()/.unbind()
绑定或解绑键盘快捷键。window.bind('<Control-s>', '_SAVE_') # 绑定 Ctrl+S -
.set_cursor(cursor='arrow')
设置鼠标光标样式(如'arrow','wait','hand')。
6. 其他实用方法
-
.start_thread(func, *args, **kwargs)
在后台线程运行函数(避免界面卡死)。window.start_thread(lambda: print('Running in thread')) -
.save_to_disk(filename)/.load_from_disk(filename)
保存或加载窗口布局到文件(实验性功能)。 -
.refresh()
强制重绘窗口(解决某些显示问题)。
示例代码
import PySimpleGUI as sg
layout = [[sg.Input(key='-INPUT-')], [sg.Button('清空')]]
window = sg.Window('示例', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == '清空':
window['-INPUT-'].update("") # 清空输入框
window.set_title('已清空') # 修改标题
window.move(200, 200) # 移动窗口
window.close()
注意事项
- 部分方法(如
.extend_layout())需要 PySimpleGUI 版本 ≥ 4.60.0。 - 动态更新布局时,建议结合
finalize()确保界面正确渲染。
在 PySimpleGUI 中,window["-INPUT-"](或类似元素对象)除了 .update() 方法外,还支持以下常用方法,按功能分类整理如下:
一、元素值操作
-
.get()
获取元素的当前值(如输入框内容、下拉菜单选中项等)。input_value = window["-INPUT-"].get() -
.update(value=None, disabled=None, visible=None, ...)
更新元素的多个属性(如值、禁用状态、可见性等)。window["-INPUT-"].update("新内容", disabled=False)
二、焦点与交互
-
.set_focus()
将焦点设置到该元素,使用户可以直接输入或操作。window["-INPUT-"].set_focus() -
.select_text()
选中输入框中的全部文本。window["-INPUT-"].select_text() -
.set_cursor_to_end()
将光标移动到输入框末尾。window["-INPUT-"].set_cursor_to_end()
三、样式与布局
-
.set_size(size=(width, height))
调整元素的尺寸(宽度和高度,单位为字符或像素)。window["-INPUT-"].set_size((20, 1)) -
.expand(expand_x=True, expand_y=True)
使元素在水平或垂直方向扩展以填充可用空间。window["-INPUT-"].expand(True, False) -
.set_tooltip("提示文本")
动态设置元素的工具提示文本。window["-INPUT-"].set_tooltip("请输入用户名") -
.set_cursor("ibeam")
设置元素的光标样式(如"arrow"、"ibeam"等)。window["-INPUT-"].set_cursor("ibeam")
四、可见性与禁用
-
.visible = False
控制元素是否可见(True/False)。window["-INPUT-"].visible = False -
.disabled = True
禁用或启用元素(True为禁用,False为启用)。window["-INPUT-"].disabled = True -
.hide_row()
隐藏或显示元素所在行的所有内容。window["-INPUT-"].hide_row()
五、事件绑定
-
.bind("<Return>", "ENTER")
绑定自定义事件(如键盘快捷键)。window["-INPUT-"].bind("<Return>", "ENTER") -
.unbind("<Return>")
解绑已绑定的事件。window["-INPUT-"].unbind("<Return>")
六、其他实用方法
-
.password_char = "*"
设置密码隐藏字符(如"*")。window["-INPUT-"].password_char = "*" -
.font = ("微软雅黑", 14)
设置字体(如("Arial", 12))。window["-INPUT-"].font = ("微软雅黑", 14) -
.background_color = "#f0f0f0"
设置背景色和文本颜色(支持颜色名称或十六进制值)。window["-INPUT-"].background_color = "#f0f0f0" -
.metadata = {"user_id": 123}
存储任意用户数据(可附加到元素上)。window["-INPUT-"].metadata = {"user_id": 123} -
.widget
访问底层 GUI 框架的原生控件(如 Tkinter 的Entry对象)。tk_entry = window["-INPUT-"].widget
示例代码
import PySimpleGUI as sg
layout = [
[sg.Input(key="-INPUT-", enable_events=True)],
[sg.Button("获取值"), sg.Button("清空并禁用")]
]
window = sg.Window("示例", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == "获取值":
print("当前值:", window["-INPUT-"].get())
elif event == "清空并禁用":
window["-INPUT-"].update("") # 清空内容
window["-INPUT-"].disabled = True # 禁用输入
window["-INPUT-"].set_tooltip("已禁用输入") # 更新提示
window.close()

浙公网安备 33010602011771号