Home > Mobile >  tkinter two combobox always getting the same value
tkinter two combobox always getting the same value

Time:03-21

I am trying to build a GUI app, I am at an early stage.

import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):

    def __init__(self):
        super().__init__()
        self.title("Test")
        self.geometry("760x250")
        self.inter_sources = ['a', 'b', 'c', 'd']
        self.inter_destinations = ['a', 'b', 'c', 'd']
        self.source_options = ttk.Combobox(self, state="readonly", width=40,
                                           values=self.inter_sources, textvariable=self.inter_sources)
        self.destination_options = ttk.Combobox(self, state="readonly", width=40,
                                                values=self.inter_destinations, textvariable=self.inter_destinations)

    def place_options(self):
        self.source_options.place(x=10, y=40)
        self.source_options.set(self.inter_sources[0])
        self.destination_options.place(x=10, y=80)
        self.destination_options.set(self.inter_destinations[0])


x = App()
x.place_options()
x.mainloop()

For some reason whenever any of the source_options or destination_options change value, the other will take the same value, I am not sure if this is a bug or I am doing something wrong. What I want is that the normal operation happens where if one changes the other does not.

Any help is appreciated.

CodePudding user response:

textvariable= is not for assigning list with all values.

It is for assigning StringVar() (or similar objects) to get or set selected value in Combobox. And tkinter uses string with ID to recognize assigned StringVar.

When you use the same list then it convert to the same string and both Combobox get the same ID (and probably create automatically StringVarfor tis ID). And when you change selection in one Combobox then it change value inStringVar` which automatically change selection in all Combobox which use the same ID.

You should create two StringVars and assign to different Comboboxs

self.inter_sources = ['a', 'b', 'c', 'd']
self.inter_destinations = ['a', 'b', 'c', 'd']

self.selected_source = tk.StringVar(self)
self.selected_destination = tk.StringVar(self)

self.source_options = ttk.Combobox(self, state="readonly", width=40,
                                   values=self.inter_sources,
                                   textvariable=self.selected_source
                                   )

self.destination_options = ttk.Combobox(self, state="readonly", width=40,
                                        values=self.inter_destinations,
                                        textvariable=self.selected_destination
                                        )

But if you want only get value from Combobox then you don't need textvariable because you may get it directly from Combobox

Example code with button which runs code to get vlaues from all Combobox

import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):

    def __init__(self):
        super().__init__()
        self.title("Test")
        self.geometry("760x250")
        
        self.inter_sources = ['a', 'b', 'c', 'd']
        self.inter_destinations = ['a', 'b', 'c', 'd']
        
        self.selected_source = tk.StringVar(self)
        self.selected_destination = tk.StringVar(self)
        
        self.source_options = ttk.Combobox(self, state="readonly", width=40,
                                           values=self.inter_sources,
                                           textvariable=self.selected_source
                                           )
        
        self.destination_options = ttk.Combobox(self, state="readonly", width=40,
                                                values=self.inter_destinations,
                                                textvariable=self.selected_destination
                                                )
     
        tk.Button(self, text='Check', command=self.on_press).pack()
        
    def on_press(self):
        print('selected_source:', self.selected_source.get())
        print('source_options :', self.source_options.get())
        print('selected_destination:', self.selected_destination.get())
        print('destination_options :', self.destination_options.get())

    def place_options(self):
        self.source_options.place(x=10, y=40)
        self.source_options.set(self.inter_sources[0])
        self.destination_options.place(x=10, y=80)
        self.destination_options.set(self.inter_destinations[0])


x = App()
x.place_options()
x.mainloop()
  • Related