Python Forum
[PyQt] call a function with parametrs from another class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] call a function with parametrs from another class
#1
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()
Reply
#2
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()
Reply
#3
thanks for the code
Reply
#4
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  GUI Problem / call another function / fill QListwidget flash77 5 836 Jul-30-2023, 04:29 PM
Last Post: flash77
  simple tkinter question function call not opening image gr3yali3n 5 3,304 Aug-02-2022, 09:13 PM
Last Post: woooee
  [PyQt] Call a function in FormA from FormB panoss 3 1,861 Jan-30-2022, 07:45 PM
Last Post: panoss
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 4,359 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  Class function does not create command button Heyjoe 2 2,227 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [Tkinter] Use function from other class (Tkinter) zarize 8 4,692 Aug-17-2020, 09:47 AM
Last Post: zarize
  [Tkinter] Call a class method from another (Tk GUI) class? Marbelous 3 6,136 Jan-15-2020, 06:55 PM
Last Post: Marbelous
  [Tkinter] Call a function when switching layouts 4096 0 3,505 Sep-22-2019, 07:39 PM
Last Post: 4096
  [PyQt] call a function in another class darktitan 6 14,030 Jun-22-2019, 03:33 AM
Last Post: darktitan
  [Tkinter] Bringing function out of class into main loop zukochew 1 2,652 Jul-30-2018, 06:43 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020