简体   繁体   English

选择OptionMenu中的选项时,如何使Tkinter按钮不消失?

[英]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. 第二个问题是,当您将标签包装在其容器中时,没有指定诸如expandfill选项,因此它不会增长来填充主窗口。

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 . 在这种情况下, placegridpack好一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择一个选项后,如何使Tkinter OptionMenu不更改文本? - How can I make a Tkinter OptionMenu not change the text when an option is selected? 将选项添加到python tkinter optionmenu并在选择时执行功能 - add option to python tkinter optionmenu and execute a function when selected 在 OptionMenu Tkinter 中选择选项时如何将按钮状态从禁用更改为活动 - How to change a buttons state from disabled to active when an option is selected in an OptionMenu Tkinter 如何让 tkinter OptionMenu 显示所选选项的名称? - How to get tkinter OptionMenu to display the name of the selected option? 当在 OptionMenu 上选择一个选项并按下一个按钮然后为该特定选项发生某些事情时,我该如何做到这一点 - How can I make it so when an Option is chosen on OptionMenu and a button is pressed then for something to happen for that specific option 如何使按钮在 Python Tkinter 中消失 - How to make button disappear in Python Tkinter TKinter OptionMenu:如何选择? - TKinter OptionMenu: How to get the selected choice? 如何从 Tkinter OptionMenu 获取选定的值 - How to get selected value from the Tkinter OptionMenu 如何添加新选项,然后在 python tkinter optionMenu 中读取新选择的选项? - How do I add new options, and then read the new selected option in a python tkinter optionMenu? 如何使用按钮单击 tkinter 设置选项菜单 - How to set an optionmenu with a button click tkinter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM