选择组合框值后,获取字典的值 [英] Obtain the value of a dictionary after select on of the combobox value

查看:49
本文介绍了选择组合框值后,获取字典的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为组合框的值创建了一个字典。我试图使用 .get(keys)来获取我为组合框设置的值。
例如,如果我选择A,它应该打印出 Haha What ,B应该打印出 Lala Sorry ,这两个都是我字典中的值,所以我该如何更正我的代码?

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


推荐答案

仅显示键( A B C ),则需要更改 self.box_values = mydict.keys()到 self.box_values = list(self.mydict.keys())和以下行:

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(将键列表传递给 values 选项,而不是字典本身):

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

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

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.

程序:

以下是上述情况的实现:

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

这篇关于选择组合框值后,获取字典的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆