PySimpleGUI弹窗keep_on_top=True在Linux(树莓派)上失效求助
解决Linux(树莓派)上PySimpleGUI弹窗被主窗口遮挡的问题
问题概述
我开发了一款跨Windows和树莓派Linux的PySimpleGUI应用,包含主窗口和多个设置弹窗。需求是弹窗绝对不能被主窗口遮挡,当前弹窗创建时已设置keep_on_top=True和modal=True,Windows上正常生效,但树莓派Linux环境下点击主窗口后,弹窗会被主窗口遮挡,且不能禁用主窗口功能。
现有代码
弹窗创建代码
def createPopup(options,name): layouts = {'Options':getConfigLayout, 'Printer settings':getPrinterLayout, 'Exit Options':getClosingLayout} return sg.Window(name, layouts[name](options), keep_on_top = True, modal=True, finalize=True)
主窗口代码
popupWnd = sg.Window('Example' + getVersion(), createLayout(info, size=(1024,600)), margins=(0,0), background_color=getBorderColor(), no_titlebar=False, resizable=True, icon=getImgById('APP_ICON'), finalize=True)
解决方案
方案1:给弹窗指定主窗口作为父窗口
这是PySimpleGUI跨平台弹窗层级控制的推荐做法,在Linux窗口管理器中,指定父窗口后子窗口会强制保持在父窗口上层。
修改弹窗创建函数,新增main_window参数并传入主窗口实例:
def createPopup(options,name, main_window): layouts = {'Options':getConfigLayout, 'Printer settings':getPrinterLayout, 'Exit Options':getClosingLayout} # 添加parent参数关联主窗口 return sg.Window(name, layouts[name](options), keep_on_top=True, modal=True, parent=main_window, finalize=True)
调用弹窗时传入主窗口实例:
# 假设主窗口实例为popupWnd(建议后续修改变量名避免混淆) settings_popup = createPopup(options, "Options", popupWnd)
方案2:直接操作Tkinter底层强制置顶
如果方案1无效,可直接调用PySimpleGUI窗口的Tkinter底层对象,强制设置窗口置顶属性:
def createPopup(options,name): layouts = {'Options':getConfigLayout, 'Printer settings':getPrinterLayout, 'Exit Options':getClosingLayout} window = sg.Window(name, layouts[name](options), keep_on_top=True, modal=True, finalize=True) # 针对Linux环境强制窗口始终置顶 window.TKroot.attributes('-topmost', True) window.TKroot.lift() return window
方案3:设置窗口类型为对话框
告诉Linux窗口管理器当前窗口是对话框类型,这类窗口通常会自动保持在父窗口上层:
def createPopup(options,name): layouts = {'Options':getConfigLayout, 'Printer settings':getPrinterLayout, 'Exit Options':getClosingLayout} # 添加window_type='dialog'属性 return sg.Window(name, layouts[name](options), keep_on_top=True, modal=True, window_type='dialog', finalize=True)
推荐顺序
优先尝试方案1,这是最符合PySimpleGUI设计逻辑的跨平台解决方案;若无效,再依次尝试方案2、方案3适配树莓派的Linux窗口管理器特性。
内容的提问来源于stack exchange,提问作者Sofiia Kozoriz
相关产品推荐
相关产品推荐

