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