Tkinter 中的 tkMessageBox 之后小部件消失

发布于 2024-09-28 09:21:15 字数 508 浏览 6 评论 0原文

每次我在应用程序中使用此代码时:

tkMessageBox.showinfo("Test", "Info goes here!")

都会弹出一个消息框(就像它应该的那样),但是在单击“确定”后,该框与窗口上的大多数其他小部件一起消失。如何防止其他小部件消失?


这是我的代码

from Tkinter import *
import tkMessageBox
root = Tk()
root.minsize(600,600)
root.maxsize(600,600)
p1 = Label(root, bg='blue')
p1.place(width=600, height=600)
b1 = Button(p1, text="Test Button")
b1.place(x="30", y="50")
tkMessageBox.showinfo("Test", Info")
root.mainloop()

Every time I use this code in my applications:

tkMessageBox.showinfo("Test", "Info goes here!")

a message box pops up (like it is supposed to), but after I click OK, the box disappears along with most of the other widgets on the window. How do I prevent the other widgets from disappearing?


Here Is My Code:

from Tkinter import *
import tkMessageBox
root = Tk()
root.minsize(600,600)
root.maxsize(600,600)
p1 = Label(root, bg='blue')
p1.place(width=600, height=600)
b1 = Button(p1, text="Test Button")
b1.place(x="30", y="50")
tkMessageBox.showinfo("Test", Info")
root.mainloop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找一个思念的角度 2024-10-05 09:21:15

好吧,这里有一些问题。首先,您的标签没有与之关联的字符串或图像。因此,它的宽度和高度会很小。因为您使用 pack,所以包含的小部件(根窗口)将“缩小以适合”此小部件以及您打包在根窗口中的任何其他小部件。

其次,您使用按钮的位置,这意味着它的大小不会影响父级的大小。不仅如此,您还可以将按钮放置在非常小的标签内。因此,控制父窗口大小的唯一因素是标签,因此主窗口最终会变得非常小。

您还有另一个问题是您在进入事件循环之前显示对话框。我有点惊讶它居然还能工作,但 Tkinter 有时会在幕后做一些不寻常的事情。您应该在调用对话框之前进入事件循环。

尝试将代码的这种变体作为起点:

from Tkinter import *
import tkMessageBox
def showInfo():
    tkMessageBox.showinfo("Test","Info")

root = Tk()
p1 = Label(root, bg='blue', text="hello")
p1.pack()
b1 = Button(root, text="Test Button", command=showInfo)
b1.pack()
root.mainloop()

Ok, there are a few things going wrong here. First, your label has no string or image associated with it. Therefore, it's width and height will be very small. Because you use pack, the containing widget (the root window) will "shrink to fit" around this widget and any other widgets you pack in the root window.

Second, you use place for the button which means its size will not affect the size of the parent. Not only that, but you place the button inside the very tiny label. Thus, the only thing controlling the size of the parent is the label so the main window ends up being very small.

You have another problem is that you're showing the dialog before entering the event loop. I'm a bit surprised that it even works, but Tkinter sometimes does unusual things under the covers. You should enter the event loop before calling the dialog.

Try this variation of your code as a starting point:

from Tkinter import *
import tkMessageBox
def showInfo():
    tkMessageBox.showinfo("Test","Info")

root = Tk()
p1 = Label(root, bg='blue', text="hello")
p1.pack()
b1 = Button(root, text="Test Button", command=showInfo)
b1.pack()
root.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文