【问题标题】:Obtain the value of a dictionary after select on of the combobox value选择组合框值后获取字典的值
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 感谢您的解决方案,我可以知道选项 1.0 和 1.end 是什么意思吗?
    • 不客气。符号 1.0 表示第一行和第一列(在 Text 小部件中,行以 1 开头,行以 0 开头),因此这是指存在于第 1 行的文本的第一个字符。对于符号 1.end,它代表我们传递给相应方法的第二个索引(delete()get())。所以1.0 是起始索引,而1.end 是结束索引。我建议您阅读 this page 以进一步了解这些方法、符号和索引。
    猜你喜欢
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 2014-01-07
    • 2013-08-15
    • 1970-01-01
    • 2011-10-17
    相关资源
    最近更新 更多