PyQt5 从 pyuic5 转换文件调用 gui 或 uic.loadUi

PyQt5 calling gui from pyuic5 converted file or uic.loadUi

嗨,我是 python 的新手(直到 Python 中麻省理工学院 6001 计算机科学与编程导论的第 2 讲和一半)尽管如此,我开始使用 Gtk & Glade 和 PyQt5 以及 Designer .

根据 我使用 pyuic5 转换我的 prova.ui,Designer 生成 window 并在下面的代码中导入:

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

但是当我在终端中 运行 它时,我得到了一个 :

  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'

错误 !!!!

像这样使用 loaduic 时:

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

程序按预期工作,即显示 window gui 并在按下 A 或退出按钮时退出

根据用 pyuic5 转换的 prova1.ui 文件,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"))

我确定错误与 class 定义有某种关系,但无法真正理解它?有帮助吗?

黑克解决了

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.