Python GUI builder(页面)设置组合框选项

时间:2018-04-19 04:42:37

标签: python ttk

您好我尝试使用ttk库在python中配置Combobox,但遗憾的是它不允许我添加值

self.qType = ttk.Combobox(top)
    self.qType.place(relx=0.69, rely=0.09, relheight=0.04, relwidth=0.22)
    self.qType.configure(textvariable=dnsGui_support.combobox)
    self.qType.configure(width=137)
    self.qType.configure(takefocus="")

dnsGui_support.combobox位于sepearte文件

global combobox
combobox = StringVar("")
combobox.set("AA")

我尝试过combobox ['values'] =('AA','MX') 但这给了我错误stringvar实例没有属性 setitem 如果我在没有设置值的情况下运行我的程序就可以正常运行

2 个答案:

答案 0 :(得分:0)

问题是你在错误的对象上调用它。

您将StringVar对象comboboxCombobox对象qtype命名为有点令人困惑。但是Combobox对象有一个可以设置的字符串列表。 StringVar只有一个字符串 - 在这种情况下,是Combobox的当前选择。

错误消息可能有点不透明。它说StringVar instance has no attribute __setitem__的原因是x[key] = value实际上调用了x.__setitem__(key, value),所以如果x(在您的情况下为StringVar)的类型没有这样的方法,那就是错误说的是什么。

我不知道你是否可以使用dict风格的语法看到Combobox的值(对此的支持是一个很大的偶然......)。如果它不起作用,您可能需要configure,或者甚至可能configure基础Listbox。但是,无论哪种方式,都需要操作Combobox

答案 1 :(得分:0)

self.qType = ttk.Combobox(top)
self.qType.place(relx=0.69, rely=0.09, relheight=0.04, relwidth=0.22)
self.value_list = ['MX', 'AA', 'CNAME']
self.qType.configure(values=self.value_list)
self.qType.configure(textvariable=dnsGui_support.combobox)
self.qType.configure(width=137)
self.qType.configure(takefocus="")