【问题标题】:Display toplevel window on another fullscreen window tkinter python在另一个全屏窗口tkinter python上显示顶层窗口
【发布时间】:2021-03-11 14:52:30
【问题描述】:

我对 tkinter 窗口的全屏属性有疑问。主窗口具有全屏属性,第二个窗口必须显示在主窗口的顶部,而在外部单击时不会丢失。有什么帮助吗?

# add the necessairy librairy
import tkinter as tk

def Region_windows2():
    #master is the second window with toplevel option
    master = tk.Toplevel(Mafenetre) #removed your mainframe class

    w = 300 # width for the Tk root
    h = 200 # height for the Tk root
    # get screen width and height
    ws = master.winfo_screenwidth() # width of the screen
    hs = master.winfo_screenheight() # height of the screen
    # calculate x and y coordinates for the Tk master window
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    # set the dimensions of the screen 
    # and where it is placed
    master.geometry('%dx%d+%d+%d' % (w, h, x, y))
    # when open the second window i want it to be on toplevel; it means when i click outside the frame it won't get hide
    master.attributes("-topmost", True)
    master.title("password")

# here's the main window
Mafenetre = tk.Tk()
#set main window title
Mafenetre.title("GUI")
Mafenetre['bg']='white' # couleur de fond
# get screen width and height
wf1= Mafenetre.winfo_screenwidth()
hf1= Mafenetre.winfo_screenheight()
A = str(wf1)
B = str(hf1)
# set the dimensions of the screen 
# and where it is placed
Mafenetre.geometry(A+"x"+B)
Mafenetre.attributes('-fullscreen', True)
# add menubar to the main window
menubar = tk.Menu(Mafenetre, bg='#1f1b13', fg='white')
# menubar button to test the second window
menubar.add_command(label="tester", command=Region_windows2)
# add menubr to the main window
Mafenetre.config(menu=menubar)
Mafenetre.mainloop()

【问题讨论】:

  • 刚刚运行您的代码,Toplevel 在其他地方单击时不会关闭,此master.attributes("-topmost", True) 负责将其保持在顶部。
  • @AST 它必须是特定于操作系统的,因为我可以重现 OP 的问题,当我点击主窗口时,顶层消失了。
  • @j_4321 就是这样。
  • @meth 打开顶层时主窗口“禁用”是否有问题?如果没有,可以通过绑定到 FocusOut 来防止它失去焦点,从而保持顶层的顶层。但这显然会阻止在打开顶层时在主窗口的 Text 或 Entry 小部件中写入任何内容。
  • 不,没有问题。我要做的就是在顶层打开时保持主窗口显示处于禁用状态。

标签: python tkinter


【解决方案1】:

这个问题取决于窗口管理器如何处理全屏窗口,因此在某些情况下,只需使用master.attributes("-topmost", True) 就足以将顶层保持在主窗口的顶部。但是,就我而言(Linux、XFCE 桌面环境),它不起作用。我发现的解决方法是在顶层失去焦点时重新调整焦点并绑定到<FocusOut>

master.bind('<FocusOut>', lambda ev: master.focus_force())

Region_windows2().

请注意,此解决方法显然会阻止在顶层打开时在主窗口中的 TextEntry 小部件中写入任何内容,但如果主窗口被“禁用”(例如,使用 @987654327 @ 在顶层)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多