[英]How to make Tkinter button to not disappear when option in OptionMenu is selected?
I have this code: 我有以下代码:
def type():
variable=var.get()
if variable=="Text with caps":
import caps_main.py
from Tkinter import *
t = Tk()
t.geometry("500x300")
p = PhotoImage(file="C:\Users\Indrek\Downloads\Desert.gif")
l = Label(t, image=p)
t.wm_title("School Test")
var = StringVar(l)
var.set("Select test type.") # initial value
option = OptionMenu(l, var, "Text with caps", "Text with mistakes")
option.pack(expand=Y)
b = Button(l, text="Start", command = lambda:type())
b.configure(background='PeachPuff')
b.pack(expand=Y)
l.pack_propagate(0)
l.pack()
t.mainloop()
The problem is, when i run the code and select an option from optionmenu, the button "Start" disappears. 问题是,当我运行代码并从optionmenu中选择一个选项时,“开始”按钮消失了。 How to make the button to not disappear?
如何使按钮不消失?
When I change the code to:(widgets are in main frame(t) not in label(l) in this code) 当我将代码更改为:(小部件在此代码中的主框架(t)中而不在标签(l)中)
def type():
variable=var.get()
if variable=="Text with caps":
import caps_main.py
from Tkinter import *
t = Tk()
t.geometry("500x300")
p = PhotoImage(file="C:\Users\Indrek\Downloads\Desert.gif")
l = Label(t, image=p)
t.wm_title("School Test")
var = StringVar(t)
var.set("Select test type.") # initial value
option = OptionMenu(t, var, "Text with caps", "Text with mistakes")
option.pack(expand=Y)
b = Button(t, text="Start", command = lambda:type())
b.configure(background='PeachPuff')
b.pack(expand=Y)
l.pack_propagate(0)
l.pack()
t.mainloop()
Then everything works, but I dont want buttons to have such solid stripes for background. 然后一切正常,但是我不希望按钮的背景具有这样的纯色条纹。 I want just buttons on my background image.
我只想要背景图片上的按钮。
There are a couple of things that look suspicious in your code. 在您的代码中有几件事看起来可疑。 First, you turn pack propagation off in the label.
首先,在标签中关闭包装传播。 This means that the label won't resize based on the widgets inside the label.
这意味着标签不会根据标签内的小部件调整大小。 Thus, if your label is tiny, it will hide the menu that you've packed in the label.
因此,如果标签很小,它将隐藏包装在标签中的菜单。 My guess is, that's what's happening.
我的猜测是,这就是正在发生的事情。 Without knowing the size of your image I can't say for certain.
不知道您的图片大小,我无法确定。
The second problem is that when you pack the label in its container, you don't specify options such as expand
and fill
, so it won't grow to fill the main window. 第二个问题是,当您将标签包装在其容器中时,没有指定诸如
expand
和fill
选项,因此它不会增长来填充主窗口。
If you're simply wanting to use a background image for your whole GUI, you don't have to add it to a label and then add all your widgets to the label. 如果您只是想在整个GUI中使用背景图片,则不必将其添加到标签中,然后再将所有小部件添加到标签中。 A simpler approach is to use place to have your label fill the main window, and then just pack or grid all the other widgets in the main window as normal.
一种更简单的方法是使用place来使标签充满主窗口,然后像往常一样将主窗口中的所有其他小部件打包或网格化。 This is one case where
place
is a little better than grid
or pack
. 在这种情况下,
place
比grid
或pack
好一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.