Python 3 Tkinter - 带有顶级主机的Messagebox?

时间:2013-07-28 17:29:57

标签: python-3.x tkinter messagebox tk

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

以下是重现此内容的脚本:

# -*- 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()

3 个答案:

答案 0 :(得分:4)

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

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

另见:

答案 1 :(得分:1)

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

#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()

答案 2 :(得分:0)

Button(..., command=lambda: messagebox.showinfo("The Title", "A piece of text", parent=topWindow))

这肯定可以。消息框的语法为

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

设置父参数是选项之一