python-3.xpyqt5qt-designeruic

PyQt5 calling gui from pyuic5 converted file or uic.loadUi


Hi I am new to python (up to lecture 2 and half of MIT 6001 Introduction to Computer Science and Programming in Python) nevetheless I started playing with Gtk & Glade and PyQt5 and Designer.

following https://stackoverflow.com/a/54081597/9877065 I used pyuic5 to convert my prova.ui, Designer generated window and imported in the code below:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui     import *
from PyQt5.QtCore    import *

from prova1 import Ui_MainWindow

class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
       self.ui = Ui_MainWindow()
       self.ui.setupUi(self)


       self.ButtonQ.clicked.connect(self.QPushButtonQPressed) 

       self.ButtonA.clicked.connect(self.QPushButtonAPressed) 



    def QPushButtonQPressed(self):

        # This is executed when the button is pressed

        print('pppppppp exit from QQQQQQQQQQQQQQQQQQQQQQQQQQQQQ')

        sys.exit()


    def QPushButtonAPressed(self):

        # This is executed when the button is pressed

        print('exit from AAAAAAAAAAAAAAAAAAAAAAA')

        sys.exit()

if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())

but when I run it in terminal I got a :

  File "./main.py", line 80, in <module>
  w = MyWindow()
  File "./main.py", line 38, in __init__
  self.ButtonQ.clicked.connect(self.QPushButtonQPressed) 
  AttributeError: 'MyWindow' object has no attribute 'ButtonQ'

error !!!!

while using loaduic like:

 import sys

 from PyQt5 import QtWidgets, uic


 class Ui(QtWidgets.QMainWindow):

      def __init__(self):

        super(Ui, self).__init__()

        uic.loadUi('prova1.ui', self)


        self.ButtonQ.clicked.connect(self.QPushButtonQPressed) 

        self.ButtonA.clicked.connect(self.QPushButtonAPressed)

        self.show() 


    def QPushButtonQPressed(self):

        # This is executed when the button is pressed

        print('pppppppp exit from QQQQQQQQQQQQQQQQQQQQQQQQQQQQQ')

        sys.exit()


    def QPushButtonAPressed(self):

        # This is executed when the button is pressed

        print('exit from AAAAAAAAAAAAAAAAAAAAAAA')

        sys.exit()


app = QtWidgets.QApplication(sys.argv)

window = Ui()

app.exec_()

the program works as expected, that is show the window gui and exit if A or Quit button is pressed

as per the prova1.ui file that converted with pyuic5 gives, prova1.py:

# Form implementation generated from reading ui file 'prova1.ui'
#
# Created by: PyQt5 UI code generator 5.12.3
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
     def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(256, 351)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setEnabled(True)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 0, 221, 261))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.ButtonA = QtWidgets.QPushButton(self.gridLayoutWidget)
        font = QtGui.QFont()
        font.setFamily("Abyssinica SIL")
        font.setItalic(True)
        font.setUnderline(True)
        font.setStrikeOut(False)
        font.setKerning(False)
        self.ButtonA.setFont(font)
        self.ButtonA.setCheckable(True)
        self.ButtonA.setAutoDefault(False)
        self.ButtonA.setDefault(False)
        self.ButtonA.setObjectName("ButtonA")
        self.gridLayout.addWidget(self.ButtonA, 0, 0, 1, 1)
        self.C = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.C.setObjectName("C")
        self.gridLayout.addWidget(self.C, 2, 0, 1, 1)
        self.B = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.B.setObjectName("B")
        self.gridLayout.addWidget(self.B, 1, 0, 1, 1)
        self.ButtonQ = QtWidgets.QPushButton(self.gridLayoutWidget)
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.ButtonQ.setFont(font)
        self.ButtonQ.setCheckable(True)
        self.ButtonQ.setAutoDefault(False)
        self.ButtonQ.setDefault(False)
        self.ButtonQ.setObjectName("ButtonQ")
        self.gridLayout.addWidget(self.ButtonQ, 3, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 256, 29))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.statusbar.setFont(font)
        self.statusbar.setAutoFillBackground(True)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

   def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.ButtonA.setText(_translate("MainWindow", "A"))
        self.C.setText(_translate("MainWindow", "C"))
        self.B.setText(_translate("MainWindow", "B"))
        self.ButtonQ.setText(_translate("MainWindow", "Q quit"))

I am sure the error is somehow related to the class definitions but cannot really grasp it ? Any help ?


Solution

  • Heike solved it

    In the first version of MyWindow, ButonA and ButtonQ are attributes of self.ui, not of self itself, so you need to use somthing like self.ui.ButtonA.clicked.connect(...), etc.