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 Offline
    S Offline
    sdf1444
    wrote on 16 Aug 2019, 11:52 last edited by
    #1

    Hi

    I want to know how to show all UI from another python file in current file when button is clicked.

    Thanks
    Spencer

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 16 Aug 2019, 21:11 last edited by
      #2

      Hi,

      Import the class(es) from that file, instantiate this and then call show on them for example. Or put them in a layout, etc.

      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 17 Aug 2019, 15:05
      1
      • S SGaist
        16 Aug 2019, 21:11

        Hi,

        Import the class(es) from that file, instantiate this and then call show on them for example. Or put them in a layout, etc.

        S Offline
        S Offline
        sdf1444
        wrote on 17 Aug 2019, 15:05 last edited by
        #3

        @sgaist

        How do I do this? Can you give me some examples please.

        Thanks

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 17 Aug 2019, 18:49 last edited by
          #4

          How proficient in python are you ?

          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 17 Aug 2019, 21:07
          0
          • S SGaist
            17 Aug 2019, 18:49

            How proficient in python are you ?

            S Offline
            S Offline
            sdf1444
            wrote on 17 Aug 2019, 21:07 last edited by
            #5

            @sgaist

            Above average but not expert level.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 17 Aug 2019, 21:53 last edited by
              #6

              So you know how to import a class from another file. The same applies to the widgets you implement in a different file.

              mywidget.py:

              from PySide2.QtWidgets import QWidget
              
              class MyWidget(QWidget):
                  """My Cool Widget"""
              
              def doSomething(self):
                  """Do something cool"""
              
                  print("Done something cool")
              

              main.py:

              import sys
              
              from PySide2.QtWidgets import QApplication
              
              from .mywidget import MyWidget
              
              if __name__ == "__main__":
                  app = QApplication(sys.argv)
                  widget = MyWidget()
                  widget.show()
                  sys.exit(app.exec_())
              

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

              S 2 Replies Last reply 17 Aug 2019, 21:59
              2
              • S SGaist
                17 Aug 2019, 21:53

                So you know how to import a class from another file. The same applies to the widgets you implement in a different file.

                mywidget.py:

                from PySide2.QtWidgets import QWidget
                
                class MyWidget(QWidget):
                    """My Cool Widget"""
                
                def doSomething(self):
                    """Do something cool"""
                
                    print("Done something cool")
                

                main.py:

                import sys
                
                from PySide2.QtWidgets import QApplication
                
                from .mywidget import MyWidget
                
                if __name__ == "__main__":
                    app = QApplication(sys.argv)
                    widget = MyWidget()
                    widget.show()
                    sys.exit(app.exec_())
                
                S Offline
                S Offline
                sdf1444
                wrote on 17 Aug 2019, 21:59 last edited by
                #7

                @sgaist

                Hi

                How would I import all classes and functions at the same time and show the widgets?

                1 Reply Last reply
                0
                • S SGaist
                  17 Aug 2019, 21:53

                  So you know how to import a class from another file. The same applies to the widgets you implement in a different file.

                  mywidget.py:

                  from PySide2.QtWidgets import QWidget
                  
                  class MyWidget(QWidget):
                      """My Cool Widget"""
                  
                  def doSomething(self):
                      """Do something cool"""
                  
                      print("Done something cool")
                  

                  main.py:

                  import sys
                  
                  from PySide2.QtWidgets import QApplication
                  
                  from .mywidget import MyWidget
                  
                  if __name__ == "__main__":
                      app = QApplication(sys.argv)
                      widget = MyWidget()
                      widget.show()
                      sys.exit(app.exec_())
                  
                  S Offline
                  S Offline
                  sdf1444
                  wrote on 17 Aug 2019, 22:15 last edited by
                  #8

                  @sgaist

                  Hi how would I import all classes and functions at the same time when I have more than one widget I want to show from another file using pyqt2 instead of pyside2.

                  Thanks

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 17 Aug 2019, 22:21 last edited by
                    #9

                    What exactly are you trying to achieve ? It looks as if you want to use something you know nothing about so please explain your use case.

                    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 17 Aug 2019, 22:40
                    1
                    • S SGaist
                      17 Aug 2019, 22:21

                      What exactly are you trying to achieve ? It looks as if you want to use something you know nothing about so please explain your use case.

                      S Offline
                      S Offline
                      sdf1444
                      wrote on 17 Aug 2019, 22:40 last edited by
                      #10

                      @sgaist

                      Hi

                      I have three different types of widgets in a python file in the form of classes and functions and I want to know how I can import them all at once to another python file so that I can show them in the python file. I need to use pyqt5. Please give me examples. The types of widgets you use does not matter to me.

                      Thanks

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 17 Aug 2019, 22:54 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 17 Aug 2019, 22:59
                        0
                        • M mrjj
                          17 Aug 2019, 22:54

                          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 17 Aug 2019, 22:59 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

                          M 1 Reply Last reply 17 Aug 2019, 23:07
                          0
                          • S sdf1444
                            17 Aug 2019, 22:59

                            @mrjj

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

                            Thanks

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 17 Aug 2019, 23:07 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 17 Aug 2019, 23:12
                            0
                            • M mrjj
                              17 Aug 2019, 23:07

                              @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 17 Aug 2019, 23:12 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

                              M 1 Reply Last reply 18 Aug 2019, 08:18
                              0
                              • S sdf1444
                                17 Aug 2019, 23:12

                                @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

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 18 Aug 2019, 08:18 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 18 Aug 2019, 08:28
                                0
                                • M mrjj
                                  18 Aug 2019, 08:18

                                  @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 18 Aug 2019, 08:28 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

                                  M 1 Reply Last reply 18 Aug 2019, 08:33
                                  0
                                  • S sdf1444
                                    18 Aug 2019, 08:28

                                    @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

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 18 Aug 2019, 08:33 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 18 Aug 2019, 10:06
                                    0
                                    • M mrjj
                                      18 Aug 2019, 08:33

                                      @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 18 Aug 2019, 10:06 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
                                      • M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 18 Aug 2019, 10:23 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 18 Aug 2019, 12:17
                                        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 18 Aug 2019, 12:17 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

                                          8/47

                                          17 Aug 2019, 22:15

                                          topic:navigator.unread, 39
                                          • Login

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