【问题标题】:Root window jump to front when opening file dialog in child window tkinter在子窗口 tkinter 中打开文件对话框时,根窗口跳转到前面
【发布时间】:2021-08-10 20:21:19
【问题描述】:

我在 python 中使用tkinter 在 2 个不同的类中创建 2 个窗口,它们的根窗口打开一个新窗口并使子窗口位于顶部我正在使用 lift(),但是当我m 按下打开文件对话框窗口的按钮,根窗口再次跳到前面,我也尝试让子窗口始终使用attributes('-topmost', True),但随后它也出现在对话框窗口上方。
如何使根窗口始终保持在较低的位置并且不会重新出现?

【问题讨论】:

  • 弹出窗口通常应该高于该进程的所有其他窗口。您能否提供一个显示您的问题的最小工作示例?
  • 您是否尝试过将弹出窗口设为儿子的孩子?如果没有看到minimal reproducible example,我们很难知道问题出在哪里。
  • 您可以使子窗口成为根窗口的transient窗口:例如child_win.transient(root).

标签: python python-3.x tkinter


【解决方案1】:

以下代码对我有用。 GUI 是我的顶层窗口,root 是我的主窗口。

    GUI.attributes('-topmost', 0)
    filename = filedialog.askopenfilename()
    GUI.attributes('-topmost', 1)

一旦文件对话框打开文件,我将把我的 GUI 窗口带回到前面。

【讨论】:

    【解决方案2】:

    您可以保证子窗口将具有filedialog 的焦点,就像这样。

    image = fido.askopenfilename( parent = child, title = "Pick an image" )

    Transient 应用于子窗口将使子窗口坐在父窗口的前面,但不会在加载数据后阻止父窗口成为焦点。

    我已经设置了程序,所以你可以测试它,只需将条件语句设置为 True 并从文件对话框中删除 parent = child

    已扩展代码以全面测试 tkinter 的行为

    Tkinter 实验:

    测试TkToplevel 窗口的焦点控制和图像显示时 使用tk.filedialog.askopenfilename

    结论:

    除非显式设置 filedialog 关闭后,最终焦点将始终转到 filedialog 中定义的小部件(默认 = 父级)。

    将子级设置为瞬态不会影响最终焦点。

    另外,如果 child 是 tk.Tk(),那么 tk.PhotoImage 需要 file = imagemaster = parent|child,否则会抛出 _tkinter.TclError: image "pyimage1" doesn't exist 错误。

    如果孩子是tk.Toplevel(),那么tk.PhotoImage 只需要file = image 就可以成功显示照片!

    import os
    import tkinter as tk
    from tkinter import filedialog as fido
    
    parent = tk.Tk()
    parent.title("Parent")
    
    # Give tkinter time to compute parent size and position
    parent.update()
    
    # Extract window dimensions from parent
    x, y, w = parent.winfo_x(), parent.winfo_y(), parent.winfo_width()
    
    if False:
    
        # Create child as `Toplevel`
        child = tk.Toplevel(parent)
        child.title("Child")
    
        # Test usefulness of transient in controlling focus
        if True:
            child.transient(parent)
    
    else:
    
        # Create child as `Tk
        child = tk.Tk()
        child.title("Child")
    
    #  Place both windows side by side
    child.geometry( f"+{x+w+10}+{y}" )
    
    if False:
    
        # TEST 1: Focus will ulitmately go to parent
        child.focus_set()
        image = fido.askopenfilename( title = "Pick an image" )
    
    else:
    
        # TEST 2: Focus will ulitmately go to child
        parent.focus_set()
        image = fido.askopenfilename( parent = child, title = "Pick an image" )
    
    # Demonstrate that placing data (image) in child will not alter the focus
    if image:
        if True:
    
            # Try loading image method #1: Will show image
            photo = tk.PhotoImage(master = child, file = image)
    
        else:
    
            # Try loading image method #2: Will throw error
            photo = tk.PhotoImage(file = image)
    
        # Display photo in `label`
        label = tk.Label(
            child, image = photo, foreground = "white", font = "Consolas 20 normal",
            compound = tk.CENTER, text = os.path.split( image )[1])
        label.pack(fill=tk.BOTH, expand=True)
    
    parent.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 2014-08-01
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多