我需要将数组中的值加载到组合框,使用此代码,它仅加载数组的最后一个值?谁能帮我。
from tkinter import *
class Window():
# Define User Inputs:
def __init__(self, master):
master.title('Production Analysis Tool')
# EQUIPMENT PARAMETERS
self.L0 = Label(root, text="Equipment Parameters:").grid(row=0,column=0, sticky=W)
def callback(*args):
self.system_size = int(self.system_size_raw.get())
# Modules per String
self.L4 = Label(root, text = "Number of Modules Per String").grid(row=20, column=1, sticky=E)
self.modules_string_raw = IntVar(root)
global modules_per_string
modules_per_string = []
for i in range(self.system_size):
self.label = Label(root, text = "System {}".format(i+1)).grid(row=21+i, column=1, sticky=E)
self.widget = Entry(root)
self.widget.grid(row=21+i, column=2, sticky=W)
modules_per_string.append(self.widget)
# Number of Systems
self.L2 = Label(root, text = "Number of Systems").grid(row=2, column=1, sticky=E)
self.system_size_raw = IntVar(root)
choices = [1,2,3,4,5,6,7,8,9,10]
self.popupMenu2 = OptionMenu(root, self.system_size_raw, *choices).grid(row=2, column=2, sticky=W)
self.system_size_raw.trace("w", callback)
# GEOGRAPHIC LOCATION
self.L8 = Label(root, text="Geographic Location:").grid(row=52, column=0, sticky=W)
self.L9 = Label(root, text = "Latitude (decimal degrees)").grid(row=53, column=1, sticky=E)
self.lat_raw = Entry(master, bd=5, width=11)
self.lat_raw.grid(row=53, column=2, sticky=W)
self.L10 = Label(root, text = "Longitude (decimal degrees)").grid(row=54, column=1, sticky=E)
self.lon_raw = Entry(master, bd=5, width=11)
self.lon_raw.grid(row=54, column=2, sticky=W)
self.L10 = Label(root, text = "Altitude (m)").grid(row=55, column=1, sticky=E)
self.alt_raw = Entry(master, bd=5, width=11)
self.alt_raw.grid(row=55, column=2, sticky=W)
scroll = Scrollbar(root, orietn="vertical")
scroll.grid(row-0, column=3, sticky=NS)
window.config(scrollregion=window.bbox("all"))
root = Tk()
root.configure()
window = Window(root)
root.mainloop()
答案 0 :(得分:3)
在循环中,您将在每次迭代中创建一个全新的List
。因此,当您在setItems()
上调用ComboBox
时,option
列表中只有一个项目。
循环还有其他几个问题,但是一旦有了有效的数组,就可以使用ComboBox
方法很简单地填充Arrays.asList()
:
ObservableList<String> option = FXCollections.observableList(Arrays.asList(resultArray));
cbx1.setItems(option);
resultArray[i] = lines.get(i).split("\t");
不知道代码中实际上lines
是什么,很难专门解决这个问题,但是看起来您可以完全跳过整个resultArray
,而只使用lines
。
无论如何,调用split()
方法都会返回一个字符串数组。也许您可以完全删除循环,然后尝试:
ObservableList<String> options =
FXCollections.observableArrayList(Arrays.asList(lines.split("\t"));