Calling python function from GUI
-
wrote on 3 Sept 2021, 15:03 last edited by poordev123 9 Mar 2021, 15:49
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
-
@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
wrote on 3 Sept 2021, 16:50 last edited by@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_())
-
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
wrote on 3 Sept 2021, 16:14 last edited by@poordev123 please provide a minimal and reproducible example.
-
@poordev123 please provide a minimal and reproducible example.
wrote on 3 Sept 2021, 16:23 last edited by@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
-
@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
wrote on 3 Sept 2021, 16:25 last edited by eyllanesc 9 Mar 2021, 16:25@poordev123 That is exactly your job. You must do the following:
- Eliminate the irrelevant code.
- Verify that the code is functional and that it continues to reproduce the error.
- If the code is small then the process ends and shares the code, otherwise execute step 1.
-
@poordev123 That is exactly your job. You must do the following:
- Eliminate the irrelevant code.
- Verify that the code is functional and that it continues to reproduce the error.
- If the code is small then the process ends and shares the code, otherwise execute step 1.
wrote on 3 Sept 2021, 16:33 last edited byQT 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>
# -*- 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
# 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
-
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>
# -*- 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
# 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
wrote on 3 Sept 2021, 16:37 last edited by@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.
-
@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.
wrote on 3 Sept 2021, 16:43 last edited by@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
-
@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
wrote on 3 Sept 2021, 16:50 last edited by@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_())
-
@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_())
wrote on 3 Sept 2021, 16:58 last edited by@eyllanesc You are my hero, thank you very much!
1/9