Skip to content Skip to sidebar Skip to footer

How Can I Dynamically Update Ttk.combobox?

I am creating a GUI using Python 3.4 and Tkinter on a Windows 8 computer. The GUI has some Entry inputs at the top, then some comboboxes. I want the combobox to acquire a list of

Solution 1:

You can finish your homework with a simple lambda function.

valuetypes = ["bla1", "bla2", "bla3"]
datatypes = ttk.Combobox(..., values=valuetypes, 
                          postcommand=lambda: datatypes.configure(values=valuetypes), ...) 

valuetypes.append["another bla"] 

When you click on the down-arrow of the Combobox, the changes will appear in the drop-down menu.

Solution 2:

You are calling self.get_datatypes(...) and assigning the result to the postcommand attribute at the time you create the combobox. That is why it runs exactly once: you told it to. Just like with command attributes, you must give a reference to a function when definining the postcommand attribute.

Create a method specifically for the post command for each combobox, use a reference to that for your postcommand, and then call get_datatypes from that function after fetching the values from the other widgets.

It should look something like this:

datatypes = ttk.Combobox(..., postcommand=self.combo_post_command, ...)
...
defcombo_post_command(self):
    flnm2 = self.flnm2.get()
    hl2_text = self.hl2_text.get()
    delim2 = self.delim2.get()
    fcd2_text = self.fcd2_text.get()
    returnself.get_datatypes(datatypes, flnm2, hl2_text, delim2, fcd2_text)

I'm not exactly sure what datatypes is supposed to be. You define it as an empty list, then reset it to be the widget itself. Regardless, this shows the general concept.

It may seem like you have a lot of duplicated code by having a function for each combobox, but you have to call all the get() functions somewhere. You either try to cram that all into the configuration of the widget, or you put it in a function. Putting it in the function is more explicit, and easier to debug and maintain over time.

Post a Comment for "How Can I Dynamically Update Ttk.combobox?"