Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Widget' object has no attribute 'pushButton'
Forum Update on Monday, May 27th 2025

Widget' object has no attribute 'pushButton'

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xavi hachem
    wrote on 3 Nov 2023, 21:41 last edited by
    #1

    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, QTextBrowser

    class 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_())

    C 1 Reply Last reply 4 Nov 2023, 02:08
    0
    • X xavi hachem
      3 Nov 2023, 21:41

      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, QTextBrowser

      class 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_())

      C Offline
      C Offline
      ChrisW67
      wrote on 4 Nov 2023, 02:08 last edited by
      #2

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

      1 Reply Last reply
      2
      • X xavi hachem has marked this topic as solved on 4 Nov 2023, 13:31

      1/2

      3 Nov 2023, 21:41

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved