python - Print combobox value in label -


i started use python. have combobox , want print selected value on label. me difficult because use dictionary.

can me print combobox value on label?

from tkinter import * tkinter import ttk   class newcbox(ttk.combobox):      def __init__(self, master, dictionary, *args, **kw):         ttk.combobox.__init__(self, master, values=sorted(list(dictionary.keys())),                               state='readonly', *args, **kw)         self.dictionary = dictionary          # purely testing purposes         self.bind('<<comboboxselected>>', self.selected)      def value(self):         return self.dictionary[self.get()]      def selected(self, event):  # test         print((self.value()))   class newcbox1(ttk.combobox):      def __init__(self, master, dictionary, *args, **kw):         ttk.combobox.__init__(self, master, values=sorted(list(dictionary.keys())),                               state='readonly', *args, **kw)         self.dictionary = dictionary         # purely testing purposes         self.bind('<<comboboxselected>>', self.selected)      def value(self):         return self.dictionary[self.get()]      def selected(self, event):  # test         print((self.value()))   lookup = {'arkitekt': 'a', 'geoteknik': 'b',           'ingeniør anlæg': 'c', 'procesanlæg': 'd'}  documentcode = {'aftaler': 'agr', 'analyse': 'ana',                 'myndigheder': 'aut', 'sagsbasis': 'bas'}  root = tk()  newcb = newcbox(root, lookup) newcb.pack()  newcb1 = newcbox1(root, documentcode) newcb1.pack()  root.mainloop() 

you don't need 2 classes, one. if observe code, notice both classes pretty same, different dictionaries. so, can instantiate 2 objects same class passing __init__ value 2 different dictionaries.

if want label text changes whenever select comboboxes, not inherite ttk.combobox frame, can customize class ttk.combobox , label.

to set text of label can use method config(...) common widgets.

see example below (and if have further questions ask comment):

import tkinter tk tkinter import ttk   class labeledcombobox(tk.frame):      def __init__(self, master, dictionary, *args, **kw):         tk.frame.__init__(self, master, *args, **kw)         self.dictionary = dictionary          self.combo = ttk.combobox(self, values=sorted(list(dictionary.keys())),                                   state='readonly')         self.combo.current(0)         self.combo.pack(fill="both")         self.combo.bind('<<comboboxselected>>', self.on_selection)          self.label = tk.label(self, text=self.value())         self.label.pack(fill="both", expand=true)       def value(self):         return self.dictionary[self.combo.get()]      def on_selection(self, event=none):  # test         self.label.config(text=self.value())   lookup = {'arkitekt': 'a', 'geoteknik': 'b',           'ingeniør anlæg': 'c', 'procesanlæg': 'd'}  documentcode = {'aftaler': 'agr', 'analyse': 'ana',                 'myndigheder': 'aut', 'sagsbasis': 'bas'}   if __name__ == "__main__":     root = tk.tk()     root.title("labeled comboboxes")      combo1 = labeledcombobox(root, lookup, bd=1, relief="groove")     combo1.pack(side="left", padx=(2, 2), pady=5)     combo2 = labeledcombobox(root, documentcode, bd=1, relief="groove")     combo2.pack(side="right", padx=(2, 2), pady=5)      root.mainloop() 

Comments