Tkinter 中的 tkMessageBox 之后小部件消失
每次我在应用程序中使用此代码时:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这里有一些问题。首先,您的标签没有与之关联的字符串或图像。因此,它的宽度和高度会很小。因为您使用 pack,所以包含的小部件(根窗口)将“缩小以适合”此小部件以及您打包在根窗口中的任何其他小部件。
其次,您使用按钮的位置,这意味着它的大小不会影响父级的大小。不仅如此,您还可以将按钮放置在非常小的标签内。因此,控制父窗口大小的唯一因素是标签,因此主窗口最终会变得非常小。
您还有另一个问题是您在进入事件循环之前显示对话框。我有点惊讶它居然还能工作,但 Tkinter 有时会在幕后做一些不寻常的事情。您应该在调用对话框之前进入事件循环。
尝试将代码的这种变体作为起点:
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: