Python Forum

Full Version: call a function with parametrs from another class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi;
instead of creating a function for each tablewidget to initialize it, I would like to create a function in a class then I call this function in another class, making it assign as parameter the name of the tablewidgte to initialize :
Here is a sample of the code :

#!/usr/bin/python3
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets  import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class prototype():
     def __init__() :
         tablewidget=QTableWidget
    #function to initialize tablewidgte :
    def initialiser_tablewidget(self,tableWidget):
        self.tableWidget.clear() # effacer contenu
        self.tableWidget.setHorizontalHeaderItem(0, QTableWidgetItem("date"))
        self.tableWidget.setHorizontalHeaderItem(1, QTableWidgetItem("hour"))
        self.tableWidget.setHorizontalHeaderItem(2, QTableWidgetItem("Monitor1"))
        self.tableWidget.setHorizontalHeaderItem(3, QTableWidgetItem("Monitor2"))


class MainApp(QMainWindow) :
    
    def __init__(self,parent=None) :
        super(MainApp,self).__init__(parent)
        QMainWindow.__init__(self)
        self.win_UI() 
        self.prototype= prototype()
    def win_UI(self):
        self.setWindowTitle("ddm corrector")
        self.setGeometry(10,40,1140,653)
        #initialize tablewidgte_1
        prototype.intialiser_tableWidget(tablewidgte_1)
        #initialize tablewidgte_2
        prototype.intialiser_tableWidget(tablewidgte_2)

def main():
    app=QApplication(sys.argv)
    win=MainApp()
    win.show()
    app.exec_()# infinite loop
if __name__=='__main__' :
    main()

Okay here is an example from which you should be able to extrapolate what you are trying to do. If you have any questions about it or how to expand upon it just ask.
#from PyQt5.QtCore    import *
from PyQt5.QtGui     import QFont
from PyQt5.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QVBoxLayout
from PyQt5.QtWidgets import QWidget, QLabel, QPushButton, QStackedWidget
 
class Prototyper(QWidget):
    def __init__(self, parent, type) :
        QWidget.__init__(self)
        self.CntrPane = parent
        self.WdgtId = type

        Font = QFont()
        Font.setFamily('Times New Roman')
        Font.setPointSize(24)
        Font.setBold(True)
        
        WdgtName = 'Proto Widget ' + str(self.WdgtId)
        self.lblWdgtNam = QLabel(WdgtName)
        self.lblWdgtNam.setFont(Font)

        self.btnChange = QPushButton()
        if self.WdgtId == 1:
            self.btnChange.setText('Goto Two')
        else:
            self.btnChange.setText('Goto One')
        self.btnChange.clicked.connect(self.Switcher)
        
        HBox = QHBoxLayout()
        HBox.addWidget(self.btnChange)
        HBox.addStretch(1)

        VBox = QVBoxLayout()
        VBox.addWidget(self.lblWdgtNam)
        VBox.addLayout(HBox)
        VBox.addStretch(1)
        
        self.setLayout(VBox)

  # Note instead of making a callback you could use a Signal/Slot
  # but only if you feel that is something your program may need
  # as there is nothing wrong with a callback either they are simply
  # two different methodologies for different purposes and I reserve
  # adding the complexity of Signals/Slots to only where appropriate
    def Switcher(self):
        print('Switching')
        self.CntrPane.SwitchWidget(self.WdgtId)

class CentralPanel(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self)
        self.MainWin = parent

        self.WidgetOne = Prototyper(self, 1)
        self.WidgetTwo = Prototyper(self, 2)

        self.Contents = QStackedWidget()
        self.Contents.addWidget(self.WidgetOne)
        self.Contents.addWidget(self.WidgetTwo)
        
        HBox = QHBoxLayout()
        HBox.addWidget(self.Contents)
        
        self.setLayout(HBox)

    def SwitchWidget(self, WdgtId):
        print(WdgtId)
        if WdgtId == 1:
            self.Contents.setCurrentIndex(1)
        else:
            self.Contents.setCurrentIndex(0)
 
class MainApp(QMainWindow) :
    def __init__(self) :
      # Do not use Super( ) unless you understand the 3 critical issues it creates that you have
      # to adjust your program for especially since the reason you might need it is very rare
        QMainWindow.__init__(self)
        self.setWindowTitle('DDM Corrector')
        Top = 200; Left = 700; Wdth = 1140; Hght = 653
        self.setGeometry(Left, Top, Wdth, Hght)

        self.CenterPane = CentralPanel(self)
        self.setCentralWidget(self.CenterPane)

if __name__ == '__main__':
    MainEvntHndlr = QApplication([])

    MainApp = MainApp()
    MainApp.show()

    MainEvntHndlr.exec()
thanks for the code
hello;
I apologize for this delay, but I changed orientation: instead of creating another class, I proceeded to create a function with arguments to replace the other functions each associated with its own widget,I proceeded like this (this is a minimal code) :
class MainApp(QMainWindow,Ui_MainWindow) :
    def __init__(self,parent=None) :
        super(MainApp,self).__init__(parent)
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.win_UI()
    ################################
        parametre=QTableWidget()
    #####################################
    def win_UI(self):
        #root = QFileInfo(__file__).absolutePath() #for  absolute path of the icons
        self.setWindowTitle("ddm")
        self.setGeometry(10,40,1140,653)
        self.radioButton_correction.toggled.connect(self.initialize_all_tableWidget)
    #initialize tableWidget  :  
    def initialize_correction_tableWidget(self,parametre):  
        parametre.clear()
        parametre.setHorizontalHeaderItem(0, QTableWidgetItem("date"))
        parametre.setHorizontalHeaderItem(1, QTableWidgetItem("hour"))
        parametre.setHorizontalHeaderItem(2, QTableWidgetItem("Monitor1"))
        parametre.setHorizontalHeaderItem(3, QTableWidgetItem("Monitor2"))
    #initialize all tableWidget  :  
    def initialize_all_tableWidget(self):
        for correction in  (self.tableWidget_correction_loc35R,self.tableWidget_correction_loc35L,self.tableWidget_correction_gp35R,self.tableWidget_correction_gp35L,self.tableWidget_rechercher_operation_cev_loc35R,self.tableWidget_rechercher_operation_cev_gp35R,self.tableWidget_rechercher_operation_cev_loc35L,self.tableWidget_rechercher_operation_cev_gp35L) :
            self.initialize_correction_tableWidget(correction)

def main():
    app=QApplication(sys.argv)
    win=MainApp()
    win.show()
    app.exec_()# infinite loop
if __name__=='__main__' :
    main()