GreenSaber
GreenSaber

Reputation: 1148

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_button_text.set('File')
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):
    print(self.file_button_text.get())
    self.file_button_text.set('File')

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 trace to see the option selected and then change the text back to File but it takes too long. Is there a better/another way to do this?

Upvotes: 0

Views: 1623

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385870

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.

Example:

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",
                           indicatoron=True)
menu = tk.Menu(menubutton, tearoff=False)
menubutton.configure(menu=menu)
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")
menubutton.pack(side="top")

root.mainloop()

Upvotes: 1

Related Questions