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 15.2k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #11

    Hi
    Did you try something like
    from .name_of_file import *

    Also, did you see
    https://www.riverbankcomputing.com/static/Docs/PyQt5/

    S 1 Reply Last reply
    0
    • mrjjM mrjj

      Hi
      Did you try something like
      from .name_of_file import *

      Also, did you see
      https://www.riverbankcomputing.com/static/Docs/PyQt5/

      S Offline
      S Offline
      sdf1444
      wrote on last edited by
      #12

      @mrjj

      Thanks I understand that now. But how would I show all the widgets once I have used the import statement.

      Thanks

      mrjjM 1 Reply Last reply
      0
      • S sdf1444

        @mrjj

        Thanks I understand that now. But how would I show all the widgets once I have used the import statement.

        Thanks

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #13

        @sdf1444
        Hi
        There is no magic to it.
        The import merely makes the types (classes) known.
        You must create instances of the classes you wish to use

        widget = MyWidget()

        MyWidget will of cause be another name, depending on what you import.

        S 1 Reply Last reply
        0
        • mrjjM mrjj

          @sdf1444
          Hi
          There is no magic to it.
          The import merely makes the types (classes) known.
          You must create instances of the classes you wish to use

          widget = MyWidget()

          MyWidget will of cause be another name, depending on what you import.

          S Offline
          S Offline
          sdf1444
          wrote on last edited by
          #14

          @mrjj

          What happens if I want to import the classes and functions but I don't know what the classes and functions are called.

          Thanks

          mrjjM 1 Reply Last reply
          0
          • S sdf1444

            @mrjj

            What happens if I want to import the classes and functions but I don't know what the classes and functions are called.

            Thanks

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #15

            @sdf1444
            Then you open the py file and look what they are called ?
            In any case, you need to know the classnames or function names to use them.
            However, i think you can enumerate them as to list their names or similar in a list
            https://stackoverflow.com/questions/1796180/how-can-i-get-a-list-of-all-classes-within-current-module-in-python
            However, concrete names must be used to actually use them.

            S 1 Reply Last reply
            0
            • mrjjM mrjj

              @sdf1444
              Then you open the py file and look what they are called ?
              In any case, you need to know the classnames or function names to use them.
              However, i think you can enumerate them as to list their names or similar in a list
              https://stackoverflow.com/questions/1796180/how-can-i-get-a-list-of-all-classes-within-current-module-in-python
              However, concrete names must be used to actually use them.

              S Offline
              S Offline
              sdf1444
              wrote on last edited by
              #16

              @mrjj

              So I must no the class names if I want to import them? There is no way to import and show all the classes without having to know what they are called?

              Thanks

              mrjjM 1 Reply Last reply
              0
              • S sdf1444

                @mrjj

                So I must no the class names if I want to import them? There is no way to import and show all the classes without having to know what they are called?

                Thanks

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #17

                @sdf1444
                well you can use the * syntax to import all, but
                you must create instances to show them so you cant say
                "Import all and show them " as that is not how it works.
                Also if the module contains multiple widgets. Then the only option it would
                have was to show all widgets as separate windows as there would be no
                way to know in what way to combine them into a single window.
                However, the module could internally have multiple widgets defined and
                and have one widget that uses the tother so you would only need to create
                an instance of that one.

                Can you explain why you seem to hook on not using the class names?

                S 1 Reply Last reply
                0
                • mrjjM mrjj

                  @sdf1444
                  well you can use the * syntax to import all, but
                  you must create instances to show them so you cant say
                  "Import all and show them " as that is not how it works.
                  Also if the module contains multiple widgets. Then the only option it would
                  have was to show all widgets as separate windows as there would be no
                  way to know in what way to combine them into a single window.
                  However, the module could internally have multiple widgets defined and
                  and have one widget that uses the tother so you would only need to create
                  an instance of that one.

                  Can you explain why you seem to hook on not using the class names?

                  S Offline
                  S Offline
                  sdf1444
                  wrote on last edited by
                  #18

                  @mrjj

                  Hi

                  I want to be able to import and show widgets from any other file in current python file but the condition is I have no idea what that file contains but I should be able to still show the widgets from that file. Please show me how to do this. As simple as possible would be great.

                  Thanks

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #19

                    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 2 Replies Last reply
                    0
                    • mrjjM mrjj

                      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 last edited by
                      #20

                      @mrjj

                      Hi could you give me a full working code because I still dont know how to do it based on your suggestions. Also I really appreciate all your suggestions.

                      Thanks

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #21

                        @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.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        S 1 Reply Last reply
                        1
                        • SGaistS SGaist

                          @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 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
                          • mrjjM mrjj

                            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 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
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 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
                              0
                              • mrjjM mrjj

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

                                mrjjM 1 Reply Last reply
                                0
                                • S sdf1444

                                  @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.

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 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
                                  0
                                  • mrjjM mrjj

                                    @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 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
                                    • mrjjM mrjj

                                      @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 last edited by
                                      #28

                                      @mrjj

                                      And how would this work for code I already have?

                                      1 Reply Last reply
                                      0
                                      • mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 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
                                        0
                                        • mrjjM mrjj

                                          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 last edited by
                                          #30

                                          @mrjj

                                          Can I have an example please?

                                          1 Reply Last reply
                                          0

                                          • Login

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