我有一个使用 PyQt6 创建 GUI 的 python 应用程序。该应用程序由三个窗口组成:
我试图存储每行的值(在每个选项卡的所有表中),以便可以对这些值执行计算。因此,第一行的所有值都应该彼此相关,第二行、第三行等也是如此。
我无法找到将输入值存储在变量中的方法。因此,任何帮助将不胜感激。
这是与创建InputWindow相关的代码。
def set_tab_info(self, num_rows):
self.num_rows = num_rows
tab_info = [
{"name": "Junction", "columns": 7, "header_labels": ["Type", "X", "Shear Plane Location", "Fastener Position", "Bolt/Screw", "Under head", "Thread"],
"editor_types": ["combo_box", "combo_box", "combo_box", "combo_box", "combo_box", "combo_box", "combo_box"]},
{"name": "Screw", "columns": 10, "header_labels": ["Type of Screw", "ISO", "UNI", "Nominal Diameter", "Material of Screw", "Eurocode Check", "g",
"Is there a change in diameter (shank)?", "Shank Length", "Shank Diameter"],
]
self.create_tabs(tab_info)
def create_tabs(self, tab_info):
self.column_inputs = {}
for info in tab_info:
tab_widget = QWidget()
tab_layout = QVBoxLayout(tab_widget)
table = QTableWidget(self.num_rows, info["columns"])
table.setHorizontalHeaderLabels(info["header_labels"])
for row in range(self.num_rows):
for col in range(info["columns"]):
if info["editor_types"][col] == "line_edit":
item = QLineEdit()
table.setCellWidget(row, col, item)
self.column_inputs[(info["name"], info["header_labels"][col])] = item
elif info["editor_types"][col] == "combo_box":
item = QComboBox()
item.addItems(self.get_combo_box_items(info["header_labels"][col]))
table.setCellWidget(row, col, item)
self.column_inputs[(info["name"], info["header_labels"][col])] = item
else:
item = QTableWidgetItem()
table.setItem(row, col, QTableWidgetItem())
tab_layout.addWidget(table)
self.tabs.addTab(table, info["name"])
以下图像是其外观的示例。我希望将第 1 行(所有选项卡的所有值,如果可能)中的所有值存储在一个变量中,将第 2 行中的所有值存储在另一个变量中,依此类推。
这是与我尝试过的相关的代码:
def next_button_clicked(self):
current_tab_index = self.tabs.currentIndex()
current_tab_name = self.tabs.tabText(current_tab_index)
current_table = self.tabs.currentWidget()
for row in range(current_table.rowCount()):
row_data = []
for col in range(current_table.columnCount()):
item = current_table.item(row, col)
if item is not None:
if isinstance(item, QLineEdit):
row_data.append(item.text())
elif isinstance(item, QComboBox):
row_data.append(item.currentText())
else:
row_data.append("")
if current_tab_name == "Screw":
self.data1.append(row_data)
else:
self.data2.append(row_data)
if current_tab_index < self.tabs.count() - 1:
self.tabs.setCurrentIndex(current_tab_index + 1)
elif current_tab_index == self.tabs.count() - 1 and self.tabs.tabText(current_tab_index) == "Safety Factors":
print("Data from Tab 1: " + str(self.data1))
print("Data from Tab 2: " + str(self.data2))
self.open_output_window()
我还没有完全实现它,因为我想确保它在此之前有效。我将特定选项卡的数据存储在一个列表中,并将其余选项卡存储在单独的列表中,然后将它们都打印出来。使用当前代码,我得到空列表,如下图所示。
每个列表代表一行,在这个测试中每个表有 5 行,所以 data1 有 5 个列表,data2 有剩余的 30 个。由于代码正确执行,我知道问题是“item”为空,但我不知道明白为什么。任何帮助将不胜感激
这是工作代码:
def next_button_clicked(self):
current_tab_index = self.tabs.currentIndex()
current_tab_name = self.tabs.tabText(current_tab_index)
current_table = self.tabs.currentWidget()
for row in range(current_table.rowCount()):
row_data = []
for col in range(current_table.columnCount()):
item = current_table.cellWidget(row, col)
if item is not None:
if isinstance(item, QLineEdit):
row_data.append(item.text())
elif isinstance(item, QComboBox):
row_data.append(item.currentText())
else:
row_data.append("")
if current_tab_name == "Junction":
self.junction_data.append(row_data)
elif current_tab_name == "Screw":
self.screw_data.append(row_data)
elif current_tab_name == "Plate":
self.plate_data.append(row_data)
elif current_tab_name == "Nut/Insert":
self.nut_data.append(row_data)
elif current_tab_name == "Washer":
self.washer_data.append(row_data)
elif current_tab_name == "Loads":
self.loads_data.append(row_data)
else:
self.safety_factors_data.append(row_data)
if current_tab_index < self.tabs.count() - 1:
self.tabs.setCurrentIndex(current_tab_index + 1)
elif current_tab_index == self.tabs.count() - 1 and self.tabs.tabText(current_tab_index) == "Safety Factors":
print("Data from Junction: " + str(self.junction_data))
print("Data from Screws: " + str(self.screw_data))
print("Data from Plates: " + str(self.plate_data))
print("Data from Nuts/Inserts: " + str(self.nut_data))
print("Data from Washers: " + str(self.washer_data))
print("Data from Loads: " + str(self.loads_data))
print("Data from Safety Factors: " + str(self.safety_factors_data))
self.open_output_window()