pythonfilevariablesreturn-valuepyside6

Using variable from another file that needs "import" using a button click


In file 1, I have 2 buttons.

from file2 import Candy

class MyInterface (QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setWindowTitle("Candy")

        self.candy_instance = Candy(self)
        self.candy_import_button.clicked.connect(self.candy_instance.import_candy)
        self.candy_show_button.clicked.connect(self.candy_instance.show_candy)

In File 2, I have the method to import and show an ODS File

class Candy():
    def __init__(self, main_window):
        self.main_window = main_window

    def import_candy(self):
        path = QFileDialog.getOpenFileName(None, 'Open ODS', os.getenv('HOME'), 'ODS(*.ods)')[0]
        all_data = pd.read_excel(path, skiprows=5)
        if all_data.size == 0:
            return
        return all_data

    def show_candy(self):
        self.main_window.tableWidget.setRowCount(all_data.shape[0])
        self.main_window.tableWidget.setColumnCount(all_data.shape[1])
        self.main_window.tableWidget.setHorizontalHeaderLabels(all_data.columns)
    
        for row in all_data.iterrows():
            values = row[1]
            for col_index, value in enumerate(values):
                tableItem = QTableWidgetItem(str(value))
                self.main_window.tableWidget.setItem(row[0], col_index, tableItem)

How to pass the variable of all_data from import_candy to show_candy? The import_candy can only run after the button is clicked. And after import, I want to show the table with the other button.

I read other questions, and then I tried,

def show_candy(self):
    all_data = import_candy()
    ~

and of course it needs me to import again, so it's not what I want. after that, I tried to declare Class level variable,

class Candy():
    def __init__(self, main_window):
        self.main_window = main_window
    all_data = None

But I don't know how, If I declared

all_data = Candy.all_data

I have not import anything yet, so isn't it ambiguous?

I just need to pass variable from import_candy to show_candy. Any ideas?


Solution

  • I accidentally got the answer. What makes it bad is I used all_data instead of self.all_data. When I globally change all_data with alt+f2. The code works fine as i intended.

    This is the result.

    class Candy():
    def __init__(self, main_window):
        self.main_window = main_window
        self.all_data = None #I declared the Variable
    
    
    def import_candy(self):
        path = QFileDialog.getOpenFileName(None, 'Open ODS', os.getenv('HOME'), 'ODS(*.ods)')[0]
        self.all_data = pd.read_excel(path, skiprows=5)
        if self.all_data.size == 0:
            return
        return self.all_data
    
    def show_candy(self):
        self.main_window.tableWidget.setRowCount(self.all_data.shape[0])
        self.main_window.tableWidget.setColumnCount(self.all_data.shape[1])
        self.main_window.tableWidget.setHorizontalHeaderLabels(self.all_data.columns)
    
        for row in self.all_data.iterrows():
            values = row[1]
            for col_index, value in enumerate(values):
                tableItem = QTableWidgetItem(str(value))
                self.main_window.tableWidget.setItem(row[0], col_index, tableItem)