[SOLVED] How to Access a QT Button (from UI) in Python

Issue

I am a beginner in QT. I want to create a simple GUI to load an image from a file. That’s Why I create a button in my GUI, named pushButton (I designed the GUI by QT Creator). Now how can I access the pushButton from my python file?

enter image description here

Here is my XML Code (from ui file)

<ui version="4.0">
 <class>OCR</class>
 <widget class="QWidget" name="OCR">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>567</width>
    <height>332</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>OCR</string>
  </property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>480</x>
     <y>300</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>Open Image</string>
   </property>
   <property name="autoDefault">
    <bool>true</bool>
   </property>
  </widget>
  <widget class="QScrollArea" name="scrollArea">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>541</width>
     <height>281</height>
    </rect>
   </property>
   <property name="verticalScrollBarPolicy">
    <enum>Qt::ScrollBarAlwaysOn</enum>
   </property>
   <property name="horizontalScrollBarPolicy">
    <enum>Qt::ScrollBarAlwaysOn</enum>
   </property>
   <property name="widgetResizable">
    <bool>true</bool>
   </property>
   <widget class="QWidget" name="scrollAreaWidgetContents">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>522</width>
      <height>262</height>
     </rect>
    </property>
    <layout class="QGridLayout" name="gridLayout">
     <item row="0" column="0">
      <widget class="QLabel" name="label">
       <property name="styleSheet">
        <string notr="true">background-color: rgb(170, 255, 255);</string>
       </property>
       <property name="text">
        <string>Photo</string>
       </property>
       <property name="alignment">
        <set>Qt::AlignCenter</set>
       </property>
      </widget>
     </item>
    </layout>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

Here is My Python Code

import sys
import os
import cv2
from PySide2.QtGui import QImage, QPixmap

from PySide2.QtWidgets import QApplication, QWidget, QFileDialog, QPushButton
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader


class OCR(QWidget):


    def __init__(self):
        super(OCR, self).__init__()
        self.load_ui()
        self.show()
        self.image = None

    def load_ui(self):
        loader = QUiLoader()
        path = os.path.join(os.path.dirname(__file__), "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 = OCR()
    widget.show()
    sys.exit(app.exec_())

Here, load_ui function load my UI file.

Solution

When you want to access button you just use this in __init__:
self.pushButton + action

In your code, your class(OCR) is based on QWidget and later you load this widget from file using load_ui(). In this solution I didn’t manage to access pushButton.

If you want to access buttons and other stuff I recommend slightly different project structure. Instead make OCR class based on actual widget.

import sys
import os
from PySide2.QtWidgets import QApplication
from PySide2.QtUiTools import loadUiType

current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = loadUiType(os.path.join(current_dir, "form.ui"))


class OCR(Base, Form):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)
        self.image = None
        self.pushButton.clicked.connect(lambda: self.function_called_when_btn_is_clicked())

    def function_called_when_btn_is_clicked(self):
        print("Button clicked!!!!!!")

if __name__ == "__main__":
    app = QApplication([])
    widget = OCR()
    widget.show()
    sys.exit(app.exec_())

In this example ui file is loaded via function build in PySide2 > loadUiType. All of elements in ui file now can be accessed in __init__function via self.name_of_element. When you run code code from above, clicking on button will result printing "Button clicked!!!!!!" on console. If you have more question, feel free to ask 🙂

Answered By – kornelHub

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *