GreenSaber GreenSaber - 9 months ago 41
Python Question

How can I make a Tkinter OptionMenu not change the text when an option is selected?

I would like to make a file button on my Tkinter application that displays three of four options to the user. I have this code:

self.file_button_text = StringVar(master)
self.file_buton = OptionMenu(self, self.file_button_text, "Com Ports", "Bottle Information", "Reset Graphs")
self.file_buton.grid(row=0, column=0)
self.file_button_text.trace("w", self.file_option)

def file_option(self, *args):

However, once an option is selected, the text of the button changes to that option. Is there a way I can get the value of the selection without changing the text of the button itself? I tried using
to see the option selected and then change the text back to
but it takes too long. Is there a better/another way to do this?

Answer Source

An option menu is nothing but a Menubutton with a Menu, and some special code specifically to change the text of the button. If you don't need that feature, just create your own with a Menubutton and Menu.


import tkinter as tk

root = tk.Tk()
var = tk.StringVar()
label = tk.Label(root, textvariable=var)
menubutton = tk.Menubutton(root, text="Select an option",
                           borderwidth=1, relief="raised",
menu = tk.Menu(menubutton, tearoff=False)
menu.add_radiobutton(label="One", variable=var, value="One")
menu.add_radiobutton(label="Two", variable=var, value="Two")
menu.add_radiobutton(label="Three", variable=var, value="Three")

label.pack(side="bottom", fill="x")

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download