python - Obtain the value of a dictionary after select on of the combobox value -
i have created dictionary combobox's value. trying use .get(keys)
obtain value set combobox. example if select a, should print out haha what
, b should print out lala sorry
, both of them values in dictionary how can correct code?
from tkinter import * 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() # content of text widget r=self.thistextarea.get('1.0','1.end') # if empty insert selected value directly if not r: self.thistextarea.insert(insert, self.box_values) # if not empty delete existing text , insert 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()
to display keys (a
, b
, c
) in combobox widget, need change self.box_values=mydict.keys(
) self.box_values=list(self.mydict.keys())
, line:
self.box = ttk.combobox(self.parent, textvariable=self.box_value,values= mydict,state='readonly')
to (passing list of keys values
option instead of dictionary mydict
itself):
self.box = ttk.combobox(self.parent, textvariable=self.box_value,values= self.box_values,state='readonly')
once done, in textarea()
need use get()
method retrieve value of correspondent chosen key select combobo wiget.
program:
here implementation of scenario above:
from tkinter import * 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() # content of text widget #print(self.mydict.get(self.value_of_combo)) r=self.thistextarea.get('1.0','1.end') # if empty insert selected value directly if not r: self.thistextarea.insert(insert, self.mydict.get(self.value_of_combo)) # if not empty delete existing text , insert 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()
Comments
Post a Comment