Issue
I have a python application that uses PyQt6 to create the GUI. The application consists of three windows:
- The HomeWindow is used to recieve a user input to determine the number of rows in the tables present in the second and third windows.
- The InputWindow is a window consisting of several tabs, each tab with a table with a different number of columns, but equal number of rows.
- The OutputWindow has a similar structure as the InputWindow and will be used to display the results of calculations performed on the inputs of the previous window.
I am trying to store the values of each row (throughout all the tables of every tab), so that calculations can be performed on these values. So, all values of the first row should be related to one another, same for the second, third, and so on.
I have not been able to find a way to store the input values in a variable. So, any help will be appreciated.
Here is the code relevant to the creation of the 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"])
The following images are an example of what it looks like. I want all values in row 1 (of all tabs, if possible) to be stored in one variable and all values in row 2 in another variable, and so on.
This is the piece of code relating to what I tried:
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()
I have not implemented it fully, as I want to make sure that it works prior to this. I store the data of a specific tab in one list and the rest on a separate one, and then print them both. With the current code I get empty lists, as shown in the image below.
Each list represents a row, in this test each table has 5 rows, so data1 has 5 lists and data2 has the remaining 30. As the code executes properly I know that the issue is that "item" is empty, but I do not understand why. Any help would be appreciated
Solution
Here is the working code:
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()
Answered By - Gerardo Lopez
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.