I have 2 tabs in PyQT5 created as separate classes. 1 class (tab) is to load excel file, second to set rules for removing unneeded rows. When I load file I need to post all headers to QComboBox in other tab so I can create rules. Can't make that to work.
import sys
from PyQt5.QtWidgets import *
import pandas as pd
class BOMAppWindow(QDialog):
def __init__(self):
super().__init__()
self.setGeometry(250, 150, 1400, 600)
mainTabWidget = QTabWidget() # --------- TABS UI ----------
mainTabWidget.addTab(FileOpenTab(), "File Open")
mainTabWidget.addTab(RowRemoveRulesTab(), "Row Delete Rules")
mainVbox = QVBoxLayout()
mainVbox.addWidget(mainTabWidget)
self.setLayout(mainVbox)
df = pd.DataFrame() # pandas dataframe as container for excel spreadsheet
header_list = []
class FileOpenTab(QDialog):
def __init__(self):
super().__init__()
vbox = QVBoxLayout()
h_top_box = QHBoxLayout()
excel_load_button = QPushButton("Open Excel File")
h_top_box.addWidget(excel_load_button) # ---- Adding Widgets to HBox
# ---------------- Adding Table Widget -----------
self.excel_table = QTableWidget()
vbox.addLayout(h_top_box) #-----Create Vertical Box Layout
vbox.addWidget(self.excel_table)
self.setLayout(vbox)
excel_load_button.clicked.connect(self.set_file_data)
# ----------- Open Excel Button Clicked Event
self.show()
def file_path(self): # ----------- file dialog box
filter_excel_only = "Excel Files (*.xlsx)"
filename = QFileDialog.getOpenFileName(self, 'Open Excel File', "", filter_excel_only)
return filename[0]
def set_file_data(self):
path_text = self.file_path()
if path_text:
global df
df = pd.read_excel(path_text, na_filter=False)
self.post_dataframe(df)
RowRemoveRulesTab().update_select_list()
def post_dataframe(self, dataframe):
column_count = dataframe.shape[1]
row_count = dataframe.shape[0]
self.excel_table.setColumnCount(column_count)
self.excel_table.setRowCount(row_count)
self.excel_table.setHorizontalHeaderLabels(list(dataframe.columns))
def header_list(self):
return list(df.columns)
class RowRemoveRulesTab(QDialog):
def __init__(self):
super().__init__()
hbox = QHBoxLayout()
groupBox1 = QGroupBox("Rule 1:")
vbox1 = QVBoxLayout()
self.rule1select = QComboBox()
self.rule1select.addItems(FileOpenTab().header_list())
vbox1.addWidget(self.rule1select)
groupBox1.setLayout(vbox1)
hbox.addWidget(groupBox1)
self.setLayout(hbox)
self.show()
def update_select_list(self):
self.rule1select.clear()
self.rule1select.addItems(FileOpenTab().header_list())
print(FileOpenTab().header_list())
app = QApplication(sys.argv)
bomAppWindow = BOMAppWindow()
bomAppWindow.show()
app.exec()
I need last function in second tab (or any other way to handle it) update_select_list(self): to update QComboBox with header list from excel file loaded in first FileOpenTab class. Now QComboBox remains blank after file load.