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. Calling python function from GUI

Calling python function from GUI

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 1.4k Views
  • 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.
  • P Offline
    P Offline
    poordev123
    wrote on 3 Sept 2021, 15:03 last edited by poordev123 9 Mar 2021, 15:49
    #1

    Hi, I'm relatively new to using QT. I have created my GUI and I have added a button onto it. This is a python qwidgets project. The button is named

    pb_button
    

    and then in my code all I'm trying to do for now is just say

    pb_button.clicked.connect(say_hello)
    

    where 'say_hello' is just a small python function

    def say_hello():
        print("This is a test")
    

    but QT keeps telling me that pb_button is not defined, I know that is because I am not saying anywhere in my code that I am creating a button, but I am doing that in the GUI/ui file so I was assuming that it would be able to see that. I am using pyside2 and qt creator. I would appreciate any help on how to link these.

    I have a QT UI file named 'form.ui', a PYPROJECT file, a main.py file, and a form.py file

    E 1 Reply Last reply 3 Sept 2021, 16:14
    0
    • P poordev123
      3 Sept 2021, 16:43

      @eyllanesc I'm trying to call it anywhere, I would assume in the main, but I do not have access to the button for some reason.

      connect_btn = widget.__getattribute__("pushButtonTest")
      

      or

      connect_btn = widget.pushButtonTest
      

      or

      connect_btn = form.widget.pushButtonTest
      

      And how is form.py irrelevant? It is needed in order to generate widget.py? Without it, widget.py crashes

      E Offline
      E Offline
      eyllanesc
      wrote on 3 Sept 2021, 16:50 last edited by
      #8

      @poordev123 Demo:

      import os
      from pathlib import Path
      import sys
      
      from PySide2.QtWidgets import QApplication, QWidget
      from PySide2.QtCore import QFile
      from PySide2.QtUiTools import QUiLoader
      
      
      class Widget(QWidget):
          def __init__(self):
              super(Widget, self).__init__()
              self.ui = None
              self.load_ui()
      
              self.ui.pushButtonTest.clicked.connect(self.handle_clicked)
      
          def load_ui(self):
              loader = QUiLoader()
              path = os.fspath(Path(__file__).resolve().parent / "form.ui")
              ui_file = QFile(path)
              if ui_file.open(QFile.ReadOnly):
                  self.ui = loader.load(ui_file, self)
                  ui_file.close()
      
          def handle_clicked(self):
              print("TEST")
      
      
      if __name__ == "__main__":
          app = QApplication([])
          widget = Widget()
          widget.show()
          sys.exit(app.exec_())
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      P 1 Reply Last reply 3 Sept 2021, 16:58
      2
      • P poordev123
        3 Sept 2021, 15:03

        Hi, I'm relatively new to using QT. I have created my GUI and I have added a button onto it. This is a python qwidgets project. The button is named

        pb_button
        

        and then in my code all I'm trying to do for now is just say

        pb_button.clicked.connect(say_hello)
        

        where 'say_hello' is just a small python function

        def say_hello():
            print("This is a test")
        

        but QT keeps telling me that pb_button is not defined, I know that is because I am not saying anywhere in my code that I am creating a button, but I am doing that in the GUI/ui file so I was assuming that it would be able to see that. I am using pyside2 and qt creator. I would appreciate any help on how to link these.

        I have a QT UI file named 'form.ui', a PYPROJECT file, a main.py file, and a form.py file

        E Offline
        E Offline
        eyllanesc
        wrote on 3 Sept 2021, 16:14 last edited by
        #2

        @poordev123 please provide a minimal and reproducible example.

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        P 1 Reply Last reply 3 Sept 2021, 16:23
        1
        • E eyllanesc
          3 Sept 2021, 16:14

          @poordev123 please provide a minimal and reproducible example.

          P Offline
          P Offline
          poordev123
          wrote on 3 Sept 2021, 16:23 last edited by
          #3

          @eyllanesc I don't know how I would do that aside from uploading my entire xml qt file and python programs. That would be a few hundred lines of code

          E 1 Reply Last reply 3 Sept 2021, 16:25
          0
          • P poordev123
            3 Sept 2021, 16:23

            @eyllanesc I don't know how I would do that aside from uploading my entire xml qt file and python programs. That would be a few hundred lines of code

            E Offline
            E Offline
            eyllanesc
            wrote on 3 Sept 2021, 16:25 last edited by eyllanesc 9 Mar 2021, 16:25
            #4

            @poordev123 That is exactly your job. You must do the following:

            1. Eliminate the irrelevant code.
            2. Verify that the code is functional and that it continues to reproduce the error.
            3. If the code is small then the process ends and shares the code, otherwise execute step 1.

            If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

            P 1 Reply Last reply 3 Sept 2021, 16:33
            1
            • E eyllanesc
              3 Sept 2021, 16:25

              @poordev123 That is exactly your job. You must do the following:

              1. Eliminate the irrelevant code.
              2. Verify that the code is functional and that it continues to reproduce the error.
              3. If the code is small then the process ends and shares the code, otherwise execute step 1.
              P Offline
              P Offline
              poordev123
              wrote on 3 Sept 2021, 16:33 last edited by
              #5

              @eyllanesc

              QT ui file

              <?xml version="1.0" encoding="UTF-8"?>
              <ui version="4.0">
               <class>Widget</class>
               <widget class="QWidget" name="Widget">
                <property name="geometry">
                 <rect>
                  <x>0</x>
                  <y>0</y>
                  <width>800</width>
                  <height>600</height>
                 </rect>
                </property>
                <property name="windowTitle">
                 <string>Widget</string>
                </property>
                <widget class="QPushButton" name="pushButtonTest">
                 <property name="geometry">
                  <rect>
                   <x>180</x>
                   <y>130</y>
                   <width>80</width>
                   <height>22</height>
                  </rect>
                 </property>
                 <property name="text">
                  <string>PushButton</string>
                 </property>
                </widget>
               </widget>
               <resources/>
               <connections/>
              </ui>
              

              form.py

              # -*- coding: utf-8 -*-
              
              ################################################################################
              ## Form generated from reading UI file 'form.ui'
              ##
              ## Created by: Qt User Interface Compiler version 5.15.2
              ##
              ## WARNING! All changes made in this file will be lost when recompiling UI file!
              ################################################################################
              
              from PySide2.QtCore import *
              from PySide2.QtGui import *
              from PySide2.QtWidgets import *
              
              
              class Ui_Widget(object):
                  def setupUi(self, Widget):
                      if not Widget.objectName():
                          Widget.setObjectName(u"Widget")
                      Widget.resize(800, 600)
              
                      self.retranslateUi(Widget)
              
                      QMetaObject.connectSlotsByName(Widget)
                  # setupUi
              
                  def retranslateUi(self, Widget):
                      Widget.setWindowTitle(QCoreApplication.translate("Widget", u"Widget", None))
                  # retranslateUi
              

              widget.py

              # This Python file uses the following encoding: utf-8
              import os
              from pathlib import Path
              import sys
              
              from PySide2.QtWidgets import QApplication, QWidget
              from PySide2.QtCore import QFile
              from PySide2.QtUiTools import QUiLoader
              
              
              class Widget(QWidget):
                  def __init__(self):
                      super(Widget, self).__init__()
                      self.load_ui()
              
                  def load_ui(self):
                      loader = QUiLoader()
                      path = os.fspath(Path(__file__).resolve().parent / "form.ui")
                      ui_file = QFile(path)
                      ui_file.open(QFile.ReadOnly)
                      loader.load(ui_file, self)
                      ui_file.close()
              
              
              if __name__ == "__main__":
                  app = QApplication([])
                  widget = Widget()
                  widget.show()
                  sys.exit(app.exec_())
              

              I'm trying to call 'pushButtonTest' in my python files

              E 1 Reply Last reply 3 Sept 2021, 16:37
              0
              • P poordev123
                3 Sept 2021, 16:33

                @eyllanesc

                QT ui file

                <?xml version="1.0" encoding="UTF-8"?>
                <ui version="4.0">
                 <class>Widget</class>
                 <widget class="QWidget" name="Widget">
                  <property name="geometry">
                   <rect>
                    <x>0</x>
                    <y>0</y>
                    <width>800</width>
                    <height>600</height>
                   </rect>
                  </property>
                  <property name="windowTitle">
                   <string>Widget</string>
                  </property>
                  <widget class="QPushButton" name="pushButtonTest">
                   <property name="geometry">
                    <rect>
                     <x>180</x>
                     <y>130</y>
                     <width>80</width>
                     <height>22</height>
                    </rect>
                   </property>
                   <property name="text">
                    <string>PushButton</string>
                   </property>
                  </widget>
                 </widget>
                 <resources/>
                 <connections/>
                </ui>
                

                form.py

                # -*- coding: utf-8 -*-
                
                ################################################################################
                ## Form generated from reading UI file 'form.ui'
                ##
                ## Created by: Qt User Interface Compiler version 5.15.2
                ##
                ## WARNING! All changes made in this file will be lost when recompiling UI file!
                ################################################################################
                
                from PySide2.QtCore import *
                from PySide2.QtGui import *
                from PySide2.QtWidgets import *
                
                
                class Ui_Widget(object):
                    def setupUi(self, Widget):
                        if not Widget.objectName():
                            Widget.setObjectName(u"Widget")
                        Widget.resize(800, 600)
                
                        self.retranslateUi(Widget)
                
                        QMetaObject.connectSlotsByName(Widget)
                    # setupUi
                
                    def retranslateUi(self, Widget):
                        Widget.setWindowTitle(QCoreApplication.translate("Widget", u"Widget", None))
                    # retranslateUi
                

                widget.py

                # This Python file uses the following encoding: utf-8
                import os
                from pathlib import Path
                import sys
                
                from PySide2.QtWidgets import QApplication, QWidget
                from PySide2.QtCore import QFile
                from PySide2.QtUiTools import QUiLoader
                
                
                class Widget(QWidget):
                    def __init__(self):
                        super(Widget, self).__init__()
                        self.load_ui()
                
                    def load_ui(self):
                        loader = QUiLoader()
                        path = os.fspath(Path(__file__).resolve().parent / "form.ui")
                        ui_file = QFile(path)
                        ui_file.open(QFile.ReadOnly)
                        loader.load(ui_file, self)
                        ui_file.close()
                
                
                if __name__ == "__main__":
                    app = QApplication([])
                    widget = Widget()
                    widget.show()
                    sys.exit(app.exec_())
                

                I'm trying to call 'pushButtonTest' in my python files

                E Offline
                E Offline
                eyllanesc
                wrote on 3 Sept 2021, 16:37 last edited by
                #6

                @poordev123 1) You say: I'm trying to call 'pushButtonTest' in my python files. Where do you use it? You could the code that generates the error. 2) form.py is irrelevant as it is not used so you must remove it.

                If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                P 1 Reply Last reply 3 Sept 2021, 16:43
                1
                • E eyllanesc
                  3 Sept 2021, 16:37

                  @poordev123 1) You say: I'm trying to call 'pushButtonTest' in my python files. Where do you use it? You could the code that generates the error. 2) form.py is irrelevant as it is not used so you must remove it.

                  P Offline
                  P Offline
                  poordev123
                  wrote on 3 Sept 2021, 16:43 last edited by
                  #7

                  @eyllanesc I'm trying to call it anywhere, I would assume in the main, but I do not have access to the button for some reason.

                  connect_btn = widget.__getattribute__("pushButtonTest")
                  

                  or

                  connect_btn = widget.pushButtonTest
                  

                  or

                  connect_btn = form.widget.pushButtonTest
                  

                  And how is form.py irrelevant? It is needed in order to generate widget.py? Without it, widget.py crashes

                  E 1 Reply Last reply 3 Sept 2021, 16:50
                  0
                  • P poordev123
                    3 Sept 2021, 16:43

                    @eyllanesc I'm trying to call it anywhere, I would assume in the main, but I do not have access to the button for some reason.

                    connect_btn = widget.__getattribute__("pushButtonTest")
                    

                    or

                    connect_btn = widget.pushButtonTest
                    

                    or

                    connect_btn = form.widget.pushButtonTest
                    

                    And how is form.py irrelevant? It is needed in order to generate widget.py? Without it, widget.py crashes

                    E Offline
                    E Offline
                    eyllanesc
                    wrote on 3 Sept 2021, 16:50 last edited by
                    #8

                    @poordev123 Demo:

                    import os
                    from pathlib import Path
                    import sys
                    
                    from PySide2.QtWidgets import QApplication, QWidget
                    from PySide2.QtCore import QFile
                    from PySide2.QtUiTools import QUiLoader
                    
                    
                    class Widget(QWidget):
                        def __init__(self):
                            super(Widget, self).__init__()
                            self.ui = None
                            self.load_ui()
                    
                            self.ui.pushButtonTest.clicked.connect(self.handle_clicked)
                    
                        def load_ui(self):
                            loader = QUiLoader()
                            path = os.fspath(Path(__file__).resolve().parent / "form.ui")
                            ui_file = QFile(path)
                            if ui_file.open(QFile.ReadOnly):
                                self.ui = loader.load(ui_file, self)
                                ui_file.close()
                    
                        def handle_clicked(self):
                            print("TEST")
                    
                    
                    if __name__ == "__main__":
                        app = QApplication([])
                        widget = Widget()
                        widget.show()
                        sys.exit(app.exec_())
                    

                    If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                    P 1 Reply Last reply 3 Sept 2021, 16:58
                    2
                    • E eyllanesc
                      3 Sept 2021, 16:50

                      @poordev123 Demo:

                      import os
                      from pathlib import Path
                      import sys
                      
                      from PySide2.QtWidgets import QApplication, QWidget
                      from PySide2.QtCore import QFile
                      from PySide2.QtUiTools import QUiLoader
                      
                      
                      class Widget(QWidget):
                          def __init__(self):
                              super(Widget, self).__init__()
                              self.ui = None
                              self.load_ui()
                      
                              self.ui.pushButtonTest.clicked.connect(self.handle_clicked)
                      
                          def load_ui(self):
                              loader = QUiLoader()
                              path = os.fspath(Path(__file__).resolve().parent / "form.ui")
                              ui_file = QFile(path)
                              if ui_file.open(QFile.ReadOnly):
                                  self.ui = loader.load(ui_file, self)
                                  ui_file.close()
                      
                          def handle_clicked(self):
                              print("TEST")
                      
                      
                      if __name__ == "__main__":
                          app = QApplication([])
                          widget = Widget()
                          widget.show()
                          sys.exit(app.exec_())
                      
                      P Offline
                      P Offline
                      poordev123
                      wrote on 3 Sept 2021, 16:58 last edited by
                      #9

                      @eyllanesc You are my hero, thank you very much!

                      1 Reply Last reply
                      0

                      1/9

                      3 Sept 2021, 15:03

                      • Login

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