[英]json file is not populating my comboboxes correctly
我正在嘗試迭代一個json文件,以便我的ui-如果它在場景中找到了geos,它將把信息附加到第一列中,同時,它將在第二個中找到的每個geos附加顏色選項列(顏色選項來自json文件)
雖然我可以在第一欄中添加geos,但是在將顏色選項添加到第二欄中填充組合框時遇到了問題
例如。 我的場景中有一個pCube和一個pPlane,而不是在我的組合框中填充了選項,它似乎是在抓住找到的最后一個地理對象,並僅填充pPlane的一種顏色選項。
def get_all_mesh():
all_mesh = cmds.listRelatives(cmds.ls(type = 'mesh'), parent=True)
# Result: [u'pCube1', u'pSphere1', u'pPlane1'] #
return all_mesh
def get_color():
with open('/Desktop/colors.json') as data_file:
data = json.load(data_file)
for index, name in enumerate(data):
geo_names = get_all_mesh()
for geo in geo_names:
# Remove all the digits
geo_exclude_num = ''.join(i for i in geo if not i.isdigit())
if geo_exclude_num in name:
for item in (data[name]):
print "{0} - {1}".format(name, item)
return item
class testTableView(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle('Color Test')
self.setModal(False)
self.all_mesh = get_all_mesh()
# Build the GUI
self.init_ui()
self.populate_data()
def init_ui(self):
# Table setup
self.mesh_table = QtGui.QTableWidget()
self.mesh_table.setRowCount(len(self.all_mesh))
self.mesh_table.setColumnCount(3)
self.mesh_table.setHorizontalHeaderLabels(['Mesh Found', 'Color for Mesh'])
self.md_insert_color_btn = QtGui.QPushButton('Apply color')
# Layout
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.mesh_table)
self.layout.addWidget(self.md_insert_color_btn)
self.setLayout(self.layout)
def populate_data(self):
geo_name = self.all_mesh
for row_index, geo_item in enumerate(geo_name):
new_item = QtGui.QTableWidgetItem(geo_item)
# Add in each and every mesh found in scene and append them into rows
self.mesh_table.setItem(row_index, 0, new_item)
# Insert in the color
combobox = QtGui.QComboBox()
color_list = get_color()
combobox.addItems(color_list)
self.mesh_table.setCellWidget(row_index, 1, combobox)
# To opent the dialog window
dialog = testTableView()
dialog.show()
這是我的json文件中的內容:
{
"pCube": [
"blue",
"purple",
"yellow",
"green",
"white",
"silver",
"red"
],
"pCone": [
"black",
"yellow"
],
"pSphere": [
"silver"
],
"pPlane": [
"red",
"yellow"
],
"pPrism": [
"white"
]
}
加上,而不是看到我的組合框的每個字段都用顏色的名稱填充,我得到了每個字段一個字符。
有人可以向我提供任何見解嗎?
這一點get_color()
:
for item in (data[name]):
print "{0} - {1}".format(name, item)
return item
將在遍歷所有顏色之前從函數中返回(一旦命中return語句)。
您可能要在返回之前累積所有顏色。 就像是:
def get_color():
with open('/Desktop/colors.json') as data_file:
data = json.load(data_file)
items = set()
for index, name in enumerate(data):
geo_names = get_all_mesh()
for geo in geo_names:
# Remove all the digits
geo_exclude_num = ''.join(i for i in geo if not i.isdigit())
if geo_exclude_num in name:
for item in (data[name]):
print "{0} - {1}".format(name, item)
items.add(item)
return items
之所以向您顯示第一種顏色的字符列表,是因為以下語句:
combobox.addItems(color_list)
正在將該單一顏色視為列表,並對其進行迭代以填充選項。 修復第一部分也將解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.