аватар question@mail.ru · 01.01.1970 03:00

How to properly convey an object from one class to another?

there are two class: mainwindow and setofbuttontons ,
first class - the main class,
and the second class must build buttons on the skeleton on the skeleton Windows.

So, how can you properly convey the object of the window of the buttons to, so that you can completely control this object from the class of buttons?

, I already tried to create an object of class setbuttTons when initializing the class of setttons and transmit it to the window, transmit it to the window, transmit it to the window It tued out to be built buttons, but if it came to check for pressing and performing any action, then everything broke.

help please. Below I give an approximate code of two classes:

 # File Projectpaint.main from Projectpaint.buttons Import Sys   Class Mainwindow:  DEF __init __ (Self):  Super () .__ Init __ ()   __name__ == '__main __':  app = qpplication (sys.argv)  exemplar = mainwindow ()  exemplar.show () sys.exit (app.exec _ ())  ---------------------------------------------  # File Project.buttons from Pyqtwidges Import * from Pyqt5.qtgui import *   class setofbuttons (qwidget):  def __init __ (self):  super () .__ Init __ ()   de Build_main_buttons (Self): # This method should build buttons Pass 
аватар answer@mail.ru · 01.01.1970 03:00

sorry, I do not quite understand what you want to do. Look at the example below, if something is wrong-specify what is wrong.

   import  sys  from   Pyqt5.qt  Import  * #from Projectpaint.buttons Import *   class   setofbuttts  ( def   __ init __  self, self, num, parent =  none  ):   super  (setofbuttts, self) .__ Init __ (Parent)  Self.num = num Self.layaut = QHBOXLAYOUT (SELF)  Self.Build_Main_buttons ()    def   bad_main_buttons  ( self ): # This method should build buttons    for  i  in   range  ( 5 ):  btn = qpushbutton ( f "" btn  {self.num} . " )   btn.clicked.connect ( def   on_clicked  ( self, btn ):   print  ( f'btn:  {btn.text ()}  ')     class   mainwindow  ( qmainwindow ):   def   __ init __  ( Super  () .__ Init __ ()  Self.Centralwidget = qwidget (SELF)  Self.Setcentralwidget (Self.Centralwidget)  self.num =  1    Buttonadd = Qpushbutton ()  tubonadd.clicked.connect (self.add_widget)   self.layut = Qvboxlayout (self.Centralwidget)  self.layout.addwidget (buttonadd, alignment = Qt.alignbottom)   def   ad_widget  ( self ):  setofbuttons = setofbuttons (self.num, self)   Self.LAYOUT.INSERTWIDGET (SELF.LAYOUT.count ()- 1 , setofbuttons)  self.num +=  1     if  __name__ ==  '' __ main __ ':  app = qpplication (sys.argv)  exemplar = mainwindow ()  exemplar.show () sys.exit (app.exec _ ())     

Latest

Similar