Python 3 Tkinter-具有顶级管理员权限的消息框?

Lyy*_*yyn 7 tk-toolkit tkinter messagebox python-3.x

我发现,当顶级小部件调用消息框对话框(例如“ 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()
Run Code Online (Sandbox Code Playgroud)

Bry*_*ley 7

您可以将命令的parent参数设置topWindowshowInfo

Button(..., command=lambda: messagebox.showInfo(parent=topWindow, ...))
Run Code Online (Sandbox Code Playgroud)

也可以看看: