1

I have created a dictionary for my combobox's value. I am trying to use .get(keys) to obtain the value that I set for the combobox. For example if I select A, it should print out Haha What, B should print out Lala Sorry, both of them are the values in my dictionary so how can I correct my code?

from tkinter import *
from tkinter import ttk
class Application:

    def __init__(self, parent):
        self.parent = parent
        self.value_of_combo='A'
        self.combo()

    def textArea(self, e):
        self.value_of_combo = self.box.get()
        # Get the content of the Text widget
        r=self.thistextArea.get('1.0','1.end')
        # If it is empty then insert the selected value directly
        if not r:
           self.thistextArea.insert(INSERT, self.box_values)
        # If not empty then delete existing text and insert the selected value
        else:
        self.thistextArea.delete('1.0','1.end')
        self.thistextArea.insert(END, self.box_values)


    def combo(self):
        self.box_value = StringVar()
        mydict={'A':'Haha What','B':'Lala Sorry','C':'Ohoh OMG'}
        self.box_values=mydict.keys()
        self.box = ttk.Combobox(self.parent, textvariable=self.box_value,values= mydict,state='readonly')
        self.box.bind('<<ComboboxSelected>>',self.textArea)
        self.box.current(0)
        self.box.grid(column=0, row=0)
        self.thistextArea=Text(self.parent,height=50,width=50)
        self.thistextArea.grid(column=0,row=1)

root = Tk()
app = Application(root)
root.mainloop()

1 Answers1

3

To display only the keys (A, B and C) in the Combobox widget, you need change self.box_values=mydict.keys() to self.box_values=list(self.mydict.keys()) and this line:

self.box = ttk.Combobox(self.parent, textvariable=self.box_value,values= mydict,state='readonly')

to (passing the list of keys to values option instead of the dictionary mydict itself):

self.box = ttk.Combobox(self.parent, textvariable=self.box_value,values= self.box_values,state='readonly')

Once this done, in textArea() you will need to use get() method to retrieve the value of the correspondent chosen key you select from the Combobo wiget.

Program:

Here is the implementation of the scenario above:

from tkinter import *
from tkinter import ttk
class Application:

    def __init__(self, parent):
        self.parent = parent
        self.value_of_combo='A'
        self.combo()

    def textArea(self, e):
        self.value_of_combo = self.box.get()
        # Get the content of the Text widget
        #print(self.mydict.get(self.value_of_combo))
        r=self.thistextArea.get('1.0','1.end')
        # If it is empty then insert the selected value directly
        if not r:
           self.thistextArea.insert(INSERT, self.mydict.get(self.value_of_combo))
        # If not empty then delete existing text and insert the selected value
        else:
           self.thistextArea.delete('1.0','1.end')
           self.thistextArea.insert(END, self.mydict.get(self.value_of_combo))


    def combo(self):
        self.box_value = StringVar()
        self.mydict={'A':'Haha What','B':'Lala Sorry','C':'Ohoh OMG'}
        self.box_values=list(self.mydict.keys())
        #print(self.box_values)
        self.box = ttk.Combobox(self.parent, textvariable=self.box_value,values= self.box_values,state='readonly')
        self.box.bind('<<ComboboxSelected>>',self.textArea)
        self.box.current(0)
        self.box.grid(column=0, row=0)
        self.thistextArea=Text(self.parent,height=50,width=50)
        self.thistextArea.grid(column=0,row=1)

root = Tk()
app = Application(root)
root.mainloop()
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • Thanks for the solution, may i know what is the option 1.0 and 1.end mean? – Noel Sebastian Lim Jenq Ning Jul 12 '16 at 13:32
  • You are welcome. The notation `1.0` means first line and first column (in a Text widget, lines start with 1 and lines start with 0), so this refers to the first character of the text existing on line 1. For the notation `1.end`, it represents the second index we pass to the corresponding method (`delete()` and `get()`). So `1.0` is the starting index while `1.end` is the ending index. I advice you to read [this page](http://effbot.org/tkinterbook/text.htm) for more clarification about those methods, notations and indexes. – Billal Begueradj Jul 12 '16 at 15:31