在不重新加载组合框的情况下将值附加到 TTK Combobox['values']
Appending values to TTK Combobox['values'] without reloading combobox
我需要在不重新加载整个内容的情况下向 ttk.combobox 添加值。当向其添加值时,不应重新加载 GUI 上的当前选择。
我需要这样的东西:
for string in listofstrings:
if string not in self.combobox1['values']:
self.combobox1['values'].append(string)
当我这样尝试时,出现了这个错误:
AttributeError: 'tuple' object has no attribute 'append'
(如预期)。
在此先感谢您的帮助。
怎么样:
if string not in self.combobox1['values']:
self.combobox1['values'] = (*self.combobox1['values'], string)
或者:
if string not in self.combobox1['values']:
self.combobox1['values'] += (string,)
我 运行 在寻找将项目附加到组合框的方法时进入此 post。我正在从电子表格中加载值。我创建了一个列表,然后循环添加项目。循环结束后,所有值都分配给组合框。我希望这对某人有所帮助。
Combobox1.delete(0, END)
wb = load_workbook("types.xlsx")
ws = wb.active
r = 1
string=['']
for row in ws.iter_rows(min_row=1, min_col=1):
val=str(ws.cell(row=r, column=1).value)
string.append(val)
r = r + 1
Combobox1['values'] = string
wb.close()
我需要在不重新加载整个内容的情况下向 ttk.combobox 添加值。当向其添加值时,不应重新加载 GUI 上的当前选择。
我需要这样的东西:
for string in listofstrings:
if string not in self.combobox1['values']:
self.combobox1['values'].append(string)
当我这样尝试时,出现了这个错误:
AttributeError: 'tuple' object has no attribute 'append'
(如预期)。
在此先感谢您的帮助。
怎么样:
if string not in self.combobox1['values']:
self.combobox1['values'] = (*self.combobox1['values'], string)
或者:
if string not in self.combobox1['values']:
self.combobox1['values'] += (string,)
我 运行 在寻找将项目附加到组合框的方法时进入此 post。我正在从电子表格中加载值。我创建了一个列表,然后循环添加项目。循环结束后,所有值都分配给组合框。我希望这对某人有所帮助。
Combobox1.delete(0, END)
wb = load_workbook("types.xlsx")
ws = wb.active
r = 1
string=['']
for row in ws.iter_rows(min_row=1, min_col=1):
val=str(ws.cell(row=r, column=1).value)
string.append(val)
r = r + 1
Combobox1['values'] = string
wb.close()