Python 3 Tkinter - 以顶层为主机的消息框? [英] Python 3 Tkinter - Messagebox with a toplevel as master?

查看:17
本文介绍了Python 3 Tkinter - 以顶层为主机的消息框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've found that when a toplevel widget calls a messagebox dialog (like "showinfo"), the root window is showed up, over the toplevel. Is there a way to set the Toplevel window as the master of the messagebox dialog ?

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

Here is a script to reproduce this :

# -*- 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
Then, 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
do a "focus_force()" thing on the root window').grid()
Button(topWindow, text = '[Push me !]', command = lambda: messagebox.showinfo('foo', 'bar!')).grid()

# --

root.mainloop()

推荐答案

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

You can set the parent argument to topWindow for the showInfo command:

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

另见:

这篇关于Python 3 Tkinter - 以顶层为主机的消息框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆