Skip to content
Advertisement

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

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)

JavaScript

Here is My Python Code

JavaScript

Here, load_ui function load my UI file.

Advertisement

Answer

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.

JavaScript

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 :)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement