Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

tkinter how to update optionmenu contents

import Tkinter as tk

class App():
    def __init__(self, parent):
        self.parent = parent
        self.options = ['one', 'two', 'three']

        self.om_variable = tk.StringVar(self.parent)
        self.om_variable.set(self.options[0])
        self.om_variable.trace('w', self.option_select)

        self.om = tk.OptionMenu(self.parent, self.om_variable, *self.options)
        self.om.grid(column=0, row=0)

        self.label = tk.Label(self.parent, text='Enter new option')
        self.entry = tk.Entry(self.parent)
        self.button = tk.Button(self.parent, text='Add option to list', command=self.add_option)

        self.label.grid(column=1, row=0)
        self.entry.grid(column=1, row=1)
        self.button.grid(column=1, row=2)

        self.update_button = tk.Button(self.parent, text='Update option menu', command=self.update_option_menu)
        self.update_button.grid(column=0, row=2)

    def update_option_menu(self):
        menu = self.om["menu"]
        menu.delete(0, "end")
        for string in self.options:
            menu.add_command(label=string, 
                             command=lambda value=string: self.om_variable.set(value))

    def add_option(self):
         self.options.append(self.entry.get())
         self.entry.delete(0, 'end')
         print self.options

    def option_select(self, *args):
        print self.om_variable.get()


root = tk.Tk()
App(root)
root.mainloop()
Comment

PREVIOUS NEXT
Code Example
Python :: sns boxplot ylabelsize 
Python :: compilation terminated. In file included from plugins/python/pyloader.c:1:0: plugins/python/uwsgi_python.h:2:10: fatal error: Python.h: No such file or directory #include <Python.h 
Python :: calculate area under the curve in python 
Python :: give cell format to condition pandas dataframe 
Python :: python append to dictionary 
Python :: Python Tuples Tuples allow duplicate values 
Python :: required_fields = [] 
Python :: append two 1d arrays python 
Python :: dont truncate dataframe jupyter pd display options 
Python :: python status code to string 
Python :: full row visible in jupyter notebook 
Python :: difference between awswrangler and boto3 
Python :: replace() python 
Python :: dataframe to csv 
Python :: Failed to build wxPython 
Python :: python unittest setUpClass 
Python :: django base path on level up 
Python :: python switch case 
Python :: matplotlib ax.annotate color of the arrow 
Python :: been deprecated, please pass in a Service object 
Python :: pandas redondear un valor 
Python :: preprocessing data in python 
Python :: maya python override color rgb 
Python :: how to store .png file in variable python 
Python :: how to add to beginning of linked list python 
Python :: how to specify a key to be as a break fomction python 
Python :: merge two list of dictionaries python with string 
Python :: how to check for updates from github in python 
Python :: python website example 
Python :: streamlit format_func example 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =