Widget' object has no attribute 'pushButton'
-
i have made a Gui using the Qt Designer then i imported the Form.ui to pycharm ide anyways, when i try to call my push button that i created it shows me an error !
Widget' object has no attribute 'pushButton'
knowing that the name of the button object is valid !and this is my code
This Python file uses the following encoding: utf-8
import os
from pathlib import Path
import sys
import scapy.all as scapy
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtWidgets import QApplication, QWidget, QPushButton
from PySide2.QtCore import QFile
from PySide2.QtUiTools import QUiLoader
from PyQt5.QtWidgets import QPushButton, QTextBrowserclass Widget(QWidget):
def init(self):
super(Widget, self).init()
self.load_ui()
self.device_button()
#tab_widget = self.findChild(QtWidgets.QTabWidget, "tab1")
# devices_button = tab_widget.widget(0).findChild(QtWidgets.QPushButton, "devices_button")
#devices_button.clicked.connect(self.capture_network)
# button = self.devices_button()
self.pushButton.clicked.connect(self.hello_world)def load_ui(self): loader = QUiLoader() path = os.fspath(Path(__file__).resolve().parent / "Application.ui") ui_file = QFile(path) ui_file.open(QFile.ReadOnly) loader.load(ui_file, self) ui_file.close() def device_button(self): pass def ping_button(self): pass def latency_button(self): pass def hello_world(self): print("Hello world")
if name == "main":
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts)
app = QApplication([])
widget = Widget()
widget.show()
sys.exit(app.exec_()) -
@xavi-hachem Please place code in a code block otherwise we will be guessing the indenting and having to piece together code that is mangled. This is the </> icon on the editor tool bar or type three backticks before and after the block.
Anyway, you need to put loaded UI into a persistent member variable:
import os from pathlib import Path import sys from PySide2 import QtCore, QtGui, QtWidgets from PySide2.QtWidgets import QApplication, QWidget, QPushButton from PySide2.QtCore import QFile from PySide2.QtUiTools import QUiLoader class Widget(QWidget): def __init__(self): super(Widget, self).__init__() self.load_ui() self.ui.pushButton.clicked.connect(self.hello_world) # ^^^^^ use the UI through the member variable def load_ui(self): loader = QUiLoader() path = os.fspath(Path(__file__).resolve().parent / "Application.ui") ui_file = QFile(path) ui_file.open(QFile.ReadOnly) self.ui = loader.load(ui_file, self) # ^^^^^ put the loaded UI into a member variable ui_file.close() def hello_world(self): print("Hello world") if __name__ == "__main__": QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts) app = QApplication([]) widget = Widget() widget.show() sys.exit(app.exec_())
Alternatively use
pyside2-uic
/pyside6-uic
and get a range of advantages.BTW: Are you really mixing PySide2 and PyQt5?
-