【问题标题】:Python 3 Tkinter - Messagebox with a toplevel as master?Python 3 Tkinter - 以顶级为主的消息框?
【发布时间】:2013-07-28 11:10:24
【问题描述】:

我发现当顶层小部件调用消息框对话框(如“showinfo”)时,根窗口会显示在顶层之上。有没有办法将顶层窗口设置为消息框对话框的主窗口?

这是一个重现这个的脚本:

# -*- coding:utf-8 -*-
# PYTHON 3 ONLY

from tkinter import *
from tkinter import messagebox

root = Tk()
root.title('ROOT WINDOW')
Label(root, text = 'Place the toplevel window over the root window\nThen, push the button and you will see that the root window is again over the toplevel').grid()

topWindow = Toplevel(root)
topWindow.title('TOPLEVEL WINDOW')
Label(topWindow, text = 'This button will open a messagebox but will\ndo a "focus_force()" thing on the root window').grid()
Button(topWindow, text = '[Push me !]', command = lambda: messagebox.showinfo('foo', 'bar!')).grid()

# --

root.mainloop()

【问题讨论】:

    标签: python-3.x tkinter messagebox tk


    【解决方案1】:

    对于showInfo 命令,您可以将parent 参数设置为topWindow

    Button(..., command=lambda: messagebox.showInfo(parent=topWindow, ...))
    

    另见:

    【讨论】:

    • 我不明白为什么我没有看到。谢谢!
    【解决方案2】:

    这可能会解决更多当前版本。

    #TEST AREA forcommands/methods/options/attributes
    #standard set up header code 2 
    from tkinter import *
    from tkinter import messagebox
    root = Tk()
    root.attributes('-fullscreen', True)
    root.configure(background='white')
    scrW = root.winfo_screenwidth()
    scrH = root.winfo_screenheight()  
    workwindow = str(1024) + "x" + str(768)+ "+" +str(int((scrW-1024)/2)) + "+" +str(int((scrH-768)/2))
    top1 = Toplevel(root, bg="light blue")
    top1.geometry(workwindow)
    top1.title("Top 1 - Workwindow")
    top1.attributes("-topmost", 1)  # make sure top1 is on top to start
    root.update()                   # but don't leave it locked in place
    top1.attributes("-topmost", 0)  # in case you use lower or lift
    #exit button - note: uses grid
    b3=Button(root, text="Egress", command=root.destroy)
    b3.grid(row=0,column=0,ipadx=10, ipady=10, pady=5, padx=5, sticky = W+N)
    #____________________________
    root.withdraw()
    mb1=messagebox.askquestion(top1, "Pay attention: \nThis is the message?")
    messagebox.showinfo("Say Hello", "Hello World")
    root.deiconify()
    top1.lift(aboveThis=None)
    #____________________________
    root.mainloop()
    

    【讨论】:

    • 我认为这可能会解决更多当前版本。
    • 请编辑您的帖子以包含您的评论。评论是为了要求澄清和回答其他 cmets。他们是二等公民,经常被删除。您的评论应该是您开始回答的一部分。如果您忘记包含它,您应该对您的帖子进行编辑,而不是将其作为评论发布。
    • Opps,我应该评论一下,这可能会解决更新的版本:协调根、顶层和消息框 - 几种可能的解决方案之一。
    • 请重新阅读我之前的评论。要edit your answer,请单击答案左下方的单词edit(位于单词shareflag 之间)。
    【解决方案3】:
    Button(..., command=lambda: messagebox.showinfo("The Title", "A piece of text", parent=topWindow))
    

    这肯定会奏效。消息框的语法是

    messagebox.Function_Name(title, message [, options]) 
    

    设置父参数是选项之一

    【讨论】:

    • 您能否提供更多关于此解决方案为何有效并解决 OPs 问题的信息?
    • 通过将showinfo命令的parent参数设置为topWindow,可以解决上述问题。通过这样做,tkinter 消息框的主人将被设置为 topWindow。因此,根窗口不会显示在 topWindow 上方。
    【解决方案4】:

    添加topWindow.attributes("-topmost", 1) 在定义了顶层之后,这应该可以解决问题

    【讨论】:

      猜你喜欢
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      相关资源
      最近更新 更多