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
#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


Messages In This Thread
RE: call a function with parametrs from another class - by Denni - Feb-20-2020, 05:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  GUI Problem / call another function / fill QListwidget flash77 5 950 Jul-30-2023, 04:29 PM
Last Post: flash77
  simple tkinter question function call not opening image gr3yali3n 5 3,534 Aug-02-2022, 09:13 PM
Last Post: woooee
  [PyQt] Call a function in FormA from FormB panoss 3 1,933 Jan-30-2022, 07:45 PM
Last Post: panoss
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 4,507 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  Class function does not create command button Heyjoe 2 2,314 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [Tkinter] Use function from other class (Tkinter) zarize 8 4,945 Aug-17-2020, 09:47 AM
Last Post: zarize
  [Tkinter] Call a class method from another (Tk GUI) class? Marbelous 3 6,278 Jan-15-2020, 06:55 PM
Last Post: Marbelous
  [Tkinter] Call a function when switching layouts 4096 0 3,567 Sep-22-2019, 07:39 PM
Last Post: 4096
  [PyQt] call a function in another class darktitan 6 14,213 Jun-22-2019, 03:33 AM
Last Post: darktitan
  [Tkinter] Bringing function out of class into main loop zukochew 1 2,698 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