views:

24

answers:

1

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()
+1  A: 

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()
Bryan Oakley
Ok, this code worked for me. But, I need to use the Place() manager, how can I expand the label to cover the entire window, then place the other widgets on that?
Zachary Brown
@Zachary Brown: why do you need to use place? To expand the label use the options `fill` and `expand`. You can then `pack` (or `grid`) widgets inside of the label if you wish. Of course, if you really have a need to place something at a precise x/y you can use place. It's unusual, but not completely unheard of.
Bryan Oakley
Using grid() places the widgets in relation to where everything else on the window is. pack() requires side by side, or top to bottom placing. I need place because I have several widgets that I do need in exact places.
Zachary Brown
I figured it out, but the widgets disappear still. Is it the place() manager that is causing the problems?
Zachary Brown
@Zachary Brown: no, it is not place causing the problem per se. However, widgets that are placed don't cause the container to expand or contract. So, if your container is tiny your placed widgets may not be visible. That's just a guess though, you could have other problems in your code.
Bryan Oakley
@Bryan Oakley, I'm not concerned about the widget expanding any. I have set the windows minsize() and maxsize() to the appropriate values, then expanded the label widget to the same width and height. It is the Button and Label widgets on the window that disappear when I select either Yes or No from a tkMessageBox.askquestion().
Zachary Brown
@Zachary Brown: when you say "disappear" does that mean they _were_ visible prior? I'm sorry, but all your vague comments are simply not good enough to describe your real problem, and this web site isn't really for debugging your specific code.
Bryan Oakley
@Brian Oakley, yes. Something cannot disappear unless it was visible prior. The code works just fine, until the tkMessageBox appears, after I click OK, the buttons are not there until I hover over them with the mouse.
Zachary Brown