Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to show all UI from another python file in current file when button is clicked.
Forum Updated to NodeBB v4.3 + New Features

How to show all UI from another python file in current file when button is clicked.

Scheduled Pinned Locked Moved Unsolved General and Desktop
47 Posts 4 Posters 14.0k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S SGaist
    18 Aug 2019, 19:03

    @sdf1444 you do realise that you are basically asking us to do your work for you ? You were already given several good starting points but you didn't even show anything you tried to get things working.

    S Offline
    S Offline
    sdf1444
    wrote on 19 Aug 2019, 08:24 last edited by sdf1444
    #22

    @sgaist

    Hi

    Here is some code to display widgets in pyqt5. But how do I allow the code so that it displays the imported widget from another file in the same python file/window without creating a new window for the new widget.

    pyqt.py

    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from pyqt2 import *    
    
    class App(QWidget):
        def __init__(self):
            QMainWindow.__init__(self)
    
            pybutton = QPushButton('Click me', self)
            pybutton.resize(200,70)
            pybutton.move(400, 50)
            self.show()
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        mainWin = App()
        mainWin2 = App2() 
        sys.exit(app.exec_())
    

    Here is also the code for the imported python file:

    pyqt2.py

    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *    
    
    class App2(QWidget):
        def __init__(self):
            QMainWindow.__init__(self)
    
            pybutton = QPushButton('hi', self)
            pybutton.resize(200,70)
            pybutton.move(900, 50)
            self.show()
    
    1 Reply Last reply
    0
    • M mrjj
      18 Aug 2019, 10:23

      Hi
      It seems you can use a string as class name and in that way maybe do it
      with
      https://stackoverflow.com/questions/553784/can-you-use-a-string-to-instantiate-a-class
      combined with
      https://stackoverflow.com/questions/1796180/how-can-i-get-a-list-of-all-classes-within-current-module-in-python
      so you import module , build stringlist and
      then use the

       def construct(self, builderName):
              targetClass = getattr(idClasses, builderName)
              instance = targetClass()
              self.allClasses.append(instance)
      

      to actually construct a list of instances.

      Then you can insert into your main window to show.

      S Offline
      S Offline
      sdf1444
      wrote on 19 Aug 2019, 08:34 last edited by sdf1444
      #23

      @mrjj

      Hi

      Here is some code to display widgets in pyqt5. But how do I allow the code so that it displays the imported widget from another file in the same python file/window without creating a new window for the new widget.

      pyqt.py

      import sys
      from PyQt5.QtWidgets import *
      from PyQt5.QtCore import *
      from pyqt2 import *
      class App(QWidget):
      def init(self):
      QMainWindow.init(self)
          pybutton = QPushButton('Click me', self)
          pybutton.resize(200,70)
          pybutton.move(400, 50)
          self.show()
      if name == "main":
      app = QApplication(sys.argv)
      mainWin = App()
      mainWin2 = App2()
      sys.exit(app.exec_())
      

      Here is also the code for the imported python file:

      pyqt2.py

      import sys
      from PyQt5.QtWidgets import *
      from PyQt5.QtCore import *
      
      class App2(QWidget):
      def init(self):
      QMainWindow.init(self)
          pybutton = QPushButton('hi', self)
          pybutton.resize(200,70)
          pybutton.move(900, 50)
          self.show()
      
      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 19 Aug 2019, 08:38 last edited by
        #24

        Hi
        Widgets that do not get a parent assigned, will become windows.
        So normally you would insert the imported widgets into a
        layout on the mainwindow.

        S 1 Reply Last reply 19 Aug 2019, 08:46
        0
        • M mrjj
          19 Aug 2019, 08:38

          Hi
          Widgets that do not get a parent assigned, will become windows.
          So normally you would insert the imported widgets into a
          layout on the mainwindow.

          S Offline
          S Offline
          sdf1444
          wrote on 19 Aug 2019, 08:46 last edited by
          #25

          @mrjj

          Hi

          How do I do this? What is the code for this because I really don't understand how to do it.

          Thanks very much much appreciated with also your help.

          M 1 Reply Last reply 19 Aug 2019, 08:51
          0
          • S sdf1444
            19 Aug 2019, 08:46

            @mrjj

            Hi

            How do I do this? What is the code for this because I really don't understand how to do it.

            Thanks very much much appreciated with also your help.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 19 Aug 2019, 08:51 last edited by
            #26

            @sdf1444
            well something like
            layout = QHBoxLayout()
            layout.addWidget(button1) // button1 would be your imported widget instance

            window.setLayout(layout)
            window.show()

            window most like being mainwindow

            its a good idea to read about layouts
            https://doc.qt.io/qtforpython/overviews/layout.html
            so you know what you are using :)

            S 2 Replies Last reply 19 Aug 2019, 09:02
            0
            • M mrjj
              19 Aug 2019, 08:51

              @sdf1444
              well something like
              layout = QHBoxLayout()
              layout.addWidget(button1) // button1 would be your imported widget instance

              window.setLayout(layout)
              window.show()

              window most like being mainwindow

              its a good idea to read about layouts
              https://doc.qt.io/qtforpython/overviews/layout.html
              so you know what you are using :)

              S Offline
              S Offline
              sdf1444
              wrote on 19 Aug 2019, 09:02 last edited by
              #27

              @mrjj

              But the imported widget is defined in a class so how do I use the class in the layout of the main window?

              1 Reply Last reply
              0
              • M mrjj
                19 Aug 2019, 08:51

                @sdf1444
                well something like
                layout = QHBoxLayout()
                layout.addWidget(button1) // button1 would be your imported widget instance

                window.setLayout(layout)
                window.show()

                window most like being mainwindow

                its a good idea to read about layouts
                https://doc.qt.io/qtforpython/overviews/layout.html
                so you know what you are using :)

                S Offline
                S Offline
                sdf1444
                wrote on 19 Aug 2019, 09:17 last edited by
                #28

                @mrjj

                And how would this work for code I already have?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 19 Aug 2019, 09:20 last edited by
                  #29

                  Hi
                  but a widget is a class. ?
                  so you import, you create an instance and insert into layout.

                  It should not be an issue to integrate it with the code you have.
                  Maybe you already have a layout. else now would be a good time to use one :)

                  S 2 Replies Last reply 19 Aug 2019, 09:32
                  0
                  • M mrjj
                    19 Aug 2019, 09:20

                    Hi
                    but a widget is a class. ?
                    so you import, you create an instance and insert into layout.

                    It should not be an issue to integrate it with the code you have.
                    Maybe you already have a layout. else now would be a good time to use one :)

                    S Offline
                    S Offline
                    sdf1444
                    wrote on 19 Aug 2019, 09:32 last edited by
                    #30

                    @mrjj

                    Can I have an example please?

                    1 Reply Last reply
                    0
                    • M mrjj
                      19 Aug 2019, 09:20

                      Hi
                      but a widget is a class. ?
                      so you import, you create an instance and insert into layout.

                      It should not be an issue to integrate it with the code you have.
                      Maybe you already have a layout. else now would be a good time to use one :)

                      S Offline
                      S Offline
                      sdf1444
                      wrote on 19 Aug 2019, 10:03 last edited by
                      #31

                      @mrjj

                      So how would I create instances of the classes which have multiple widgets and then insert into the layout?

                      Thanks

                      M 1 Reply Last reply 19 Aug 2019, 10:12
                      0
                      • S sdf1444
                        19 Aug 2019, 10:03

                        @mrjj

                        So how would I create instances of the classes which have multiple widgets and then insert into the layout?

                        Thanks

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 19 Aug 2019, 10:12 last edited by
                        #32

                        @sdf1444
                        Hi
                        what you say, classes, you mean normal python classes that use widgets or what do you mean?
                        I can't answer that as it would be either you create the master class (composite) then (if its a widget) or
                        you would create each widget by itself if the "class" if not a widget and you don't want to show it.
                        However, it really depends on what you import.

                        S 1 Reply Last reply 19 Aug 2019, 10:19
                        0
                        • M mrjj
                          19 Aug 2019, 10:12

                          @sdf1444
                          Hi
                          what you say, classes, you mean normal python classes that use widgets or what do you mean?
                          I can't answer that as it would be either you create the master class (composite) then (if its a widget) or
                          you would create each widget by itself if the "class" if not a widget and you don't want to show it.
                          However, it really depends on what you import.

                          S Offline
                          S Offline
                          sdf1444
                          wrote on 19 Aug 2019, 10:19 last edited by
                          #33

                          @mrjj

                          Basically this is an widget created inside a class. So I am importing widgets which are inside a master class. How do I instantiate this, and then insert into a layout?

                          class App2(QWidget):
                          def init(self):
                          QMainWindow.init(self)
                              pybutton = QPushButton('hi', self)
                              pybutton.resize(200,70)
                              pybutton.move(900, 50)
                              self.show()
                          
                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 19 Aug 2019, 10:27 last edited by mrjj
                            #34

                            Well normally one would use App2 as that is the
                            composite widget.
                            So often that makes the most sense to use as it will then init the
                            widget it uses in a proper way.

                            S 1 Reply Last reply 19 Aug 2019, 10:29
                            0
                            • M mrjj
                              19 Aug 2019, 10:27

                              Well normally one would use App2 as that is the
                              composite widget.
                              So often that makes the most sense to use as it will then init the
                              widget it uses in a proper way.

                              S Offline
                              S Offline
                              sdf1444
                              wrote on 19 Aug 2019, 10:29 last edited by
                              #35

                              @mrjj

                              So I how do I show this in the main window. What is the overall code?

                              M 1 Reply Last reply 19 Aug 2019, 10:47
                              0
                              • S sdf1444
                                19 Aug 2019, 10:29

                                @mrjj

                                So I how do I show this in the main window. What is the overall code?

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 19 Aug 2019, 10:47 last edited by
                                #36

                                @sdf1444
                                something like
                                (import it )

                                layout = QHBoxLayout()
                                layout.addWidget(App2)
                                yourwindow.setLayout(layout)
                                yourwindow.show()

                                S 1 Reply Last reply 19 Aug 2019, 11:25
                                0
                                • M mrjj
                                  19 Aug 2019, 10:47

                                  @sdf1444
                                  something like
                                  (import it )

                                  layout = QHBoxLayout()
                                  layout.addWidget(App2)
                                  yourwindow.setLayout(layout)
                                  yourwindow.show()

                                  S Offline
                                  S Offline
                                  sdf1444
                                  wrote on 19 Aug 2019, 11:25 last edited by
                                  #37

                                  @mrjj

                                  Hi

                                  I am getting this error when I add the layout code into pyqt.py: TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'sip.wrappertype'

                                  import sys
                                  from PyQt5.QtWidgets import *
                                  from PyQt5.QtCore import *
                                  from pyqt2 import *

                                  class App(QWidget):
                                  def init(self):
                                  QMainWindow.init(self)

                                      layout = QHBoxLayout()
                                      layout.addWidget(App2)
                                      yourwindow.setLayout(layout)
                                      yourwindow.show()
                                  
                                      pybutton = QPushButton('Click me', self)
                                      pybutton.resize(200,70)
                                      pybutton.move(400, 50)
                                      self.show()
                                  

                                  if name == "main":
                                  app = QApplication(sys.argv)
                                  mainWin = App()
                                  sys.exit(app.exec_())

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 19 Aug 2019, 12:11 last edited by
                                    #38

                                    Hi
                                    So
                                    app2 is NOT a QWidget ?
                                    i have no idea what "sip.wrappertype" is :)

                                    S 1 Reply Last reply 19 Aug 2019, 12:12
                                    0
                                    • M mrjj
                                      19 Aug 2019, 12:11

                                      Hi
                                      So
                                      app2 is NOT a QWidget ?
                                      i have no idea what "sip.wrappertype" is :)

                                      S Offline
                                      S Offline
                                      sdf1444
                                      wrote on 19 Aug 2019, 12:12 last edited by
                                      #39

                                      @mrjj

                                      "app2" is a widget created from inside class.

                                      M 1 Reply Last reply 19 Aug 2019, 12:14
                                      0
                                      • S sdf1444
                                        19 Aug 2019, 12:12

                                        @mrjj

                                        "app2" is a widget created from inside class.

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 19 Aug 2019, 12:14 last edited by
                                        #40

                                        @sdf1444
                                        it has to inherit QWidget or be a QWidget to be used in a layout.
                                        Else its plain impossible and you must create the inner Widgets yourself. :)

                                        S 1 Reply Last reply 19 Aug 2019, 12:24
                                        0
                                        • M mrjj
                                          19 Aug 2019, 12:14

                                          @sdf1444
                                          it has to inherit QWidget or be a QWidget to be used in a layout.
                                          Else its plain impossible and you must create the inner Widgets yourself. :)

                                          S Offline
                                          S Offline
                                          sdf1444
                                          wrote on 19 Aug 2019, 12:24 last edited by
                                          #41

                                          @mrjj

                                          Hi "app2" is a QWidget.

                                          1 Reply Last reply
                                          0

                                          31/47

                                          19 Aug 2019, 10:03

                                          • Login

                                          • Login or register to search.
                                          31 out of 47
                                          • First post
                                            31/47
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved