【发布时间】:2016-11-14 16:01:44
【问题描述】:
我为我的组合框的值创建了一个字典。我正在尝试使用.get(keys) 来获取我为组合框设置的值。
例如,如果我选择A,它应该打印出Haha What,B应该打印出Lala Sorry,它们都是我字典中的值,那么我该如何更正我的代码?
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()
【问题讨论】:
标签: python dictionary combobox tkinter interface