举起一个tkMessageBox

时间:2012-09-19 09:15:15

标签: python tkinter tkmessagebox

我正在使用tkMessageBox.showinfo(info at tutorialspoint)在我的程序中弹出警告。

只有在屏幕上使用第二个TopLevel窗口(除了主窗口)调用警告时才会出现问题:在这种情况下,警告仍隐藏在第二个TL窗口后面。

我试着这么称呼它:

tkMessageBox.showinfo(title='Warning',message=s).lift()

但它不起作用。有什么想法吗?

2 个答案:

答案 0 :(得分:6)

我认为消息框只能保证在其父节目之上。如果您创建第二个顶层并且您希望消息框位于该第二个窗口之上,请将该第二个窗口设置为消息框的父级。

tl2 = tk.Toplevel(...)
...
tkMessageBox.showinfo("Say Hello", "Hello World", parent=tl2)

答案 1 :(得分:1)

我没有看到您描述的问题。我在下面写的代码只是创建一个创建第二个窗口的窗口所需的最小代码。第二个窗口使用showinfo方法创建信息框。我想知道你是否还有其他东西。 (注意,为了掩盖信息窗口,我使窗口有点大。)

from Tkinter import Tk, Button, Toplevel
import tkMessageBox

top = Tk()
def make_window():
    t = Toplevel(top)
    t.title("I'm Window 2. Look at me too!")
    B2 = Button(t, text = "Click me", command = hello)
    B2.pack()
    t.geometry('500x500+50+50')

def hello():
    tkMessageBox.showinfo("Say Hello", "Hello World")

B1 = Button(top, text = "New Window", command = make_window)
B1.pack()

top.title("I'm Window 1. Look at me!")
top.geometry('500x500+100+100')
top.mainloop()

这是在Windows 7(64位)上使用Python 2.7(32位)测试的。它产生这样的东西:

enter image description here