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

Posted

技术标签:

【中文标题】PyQt5 从 pyuic5 转换文件或 uic.loadUi 调用 gui【英文标题】:PyQt5 calling gui from pyuic5 converted file or uic.loadUi 【发布时间】:2020-04-04 09:49:08 【问题描述】:

您好,我是 python 新手(直到 MIT 6001 Introduction to Computer Science and Programming in Python 的第 2 课和一半),但我开始使用 Gtk & Glade 和 PyQt5 和 Designer。

按照https://***.com/a/54081597/9877065我用pyuic5转换了我的prova.ui,Designer生成的窗口并在下面的代码中导入:

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

程序按预期工作,即显示窗口 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"))

我确定该错误与类定义有某种关系,但无法真正掌握它?有什么帮助吗?

【问题讨论】:

MyWindow的第一个版本中,ButonAButtonQself.ui的属性,而不是self本身的属性,所以你需要使用self.ui.ButtonA.clicked.connect(...)之类的东西. 嗨,Heike 是的,它与 self.ui.ButtonQ.clicked.connect 一起使用......需要回到我的书以了解有关课程和自我的信息。 【参考方案1】:

黑克解决了

在MyWindow的第一个版本中,ButtonA和ButtonQ是self.ui的属性,而不是self本身的属性,所以你需要使用self.ui.ButtonA.clicked.connect(...)之类的东西。

【讨论】:

以上是关于PyQt5 从 pyuic5 转换文件或 uic.loadUi 调用 gui的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5:pyuic5 - 未知的 Qt 小部件:QKeySequenceEdit

Pyuic5 生成带有无效导入的 python 文件

pyuic5 向后兼容性

[转]pycharm中将ui文件转换成py文件

[转]pycharm中将ui文件转换成py文件

pyqt5学习日记