繁体   English   中英

在选择组合框值后获取字典的值

[英]Obtain the value of a dictionary after select on of the combobox value

我为我的组合框的值创建了一个字典。 我试图使用.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()

要仅显示Combobox小部件中的键( ABC ),您需要将self.box_values=mydict.keys( )更改为self.box_values=list(self.mydict.keys())并且此行:

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

to(将键列表传递给values选项而不是字典mydict本身):

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

完成此操作后,在textArea()您将需要使用get()方法来检索从Combobo wiget中选择的对应选择键的值。

程序:

以下是上述场景的实现:

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM