gpt4 book ai didi

python-3.x - Python 3 Tkinter - 以顶层为主机的消息框?

转载 作者:行者123 更新时间:2023-12-04 11:08:10 25 4
gpt4 key购买 nike

我发现,当顶层小部件调用消息框对话框(如“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()

最佳答案

您可以设置 parent论据 topWindowshowInfo命令:

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

也可以看看:
  • http://effbot.org/tkinterbook/tkinter-standard-dialogs.htm
  • 关于python-3.x - Python 3 Tkinter - 以顶层为主机的消息框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17910866/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com