简体   繁体   English

将Qt Designer 5.8与PyQt5一起使用:来自PyQt v3的有问题的示例

[英]Using Qt Designer 5.8 with PyQt5: problematic example from PyQt v3

I understand (more or less) content of the page of official documentation except the the final example , which is essencially as follows (I do not care the button): 我了解(或多或少) 官方文档页面的内容, 最后一个示例 除外 ,该示例本质上如下(我不在乎按钮):

from ui_imagedialog import ImageDialog

class MyImageDialog(ImageDialog):
    def __init__(self):
      super(MyImageDialog, self).__init__()

    # Connect up the buttons.
      self.okButton.clicked.connect(self.accept)

Problem : I'm stuck a bit trying to understand how to make this snippet work. 问题 :我有点想了解如何使此代码片段正常工作。 It rises an error: 'cannon import name ImageDialog'. 它会引发一个错误:'cannon import name ImageDialog'。

What should I add from the first example of the aforementioned documentation page to make this code to show up a Dialog window? 我应该在上述文档页面的第一个示例中添加什么,以使此代码显示“对话框”窗口?

What I've tried : I've made the file with generated Python code with the name ui_imagedialog.py as requiered. 我尝试过的工作 :我已使用所需的名称ui_imagedialog.py用生成的Python代码制作了文件。 It has the the following content, which obviously works on its own: 它具有以下内容,这些内容显然可以独立工作:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_ImageDialog(object):
    def setupUi(self, ImageDialog):
        ImageDialog.setObjectName("ImageDialog")
        ImageDialog.resize(303, 204)
        self.pushButton = QtWidgets.QPushButton(ImageDialog)
        self.pushButton.setGeometry(QtCore.QRect(200, 160, 75, 23))
        self.pushButton.setObjectName("pushButton")

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

    def retranslateUi(self, ImageDialog):
        _translate = QtCore.QCoreApplication.translate
        ImageDialog.setWindowTitle(_translate("ImageDialog", "Dialog"))
        self.pushButton.setText(_translate("ImageDialog", "PushButton"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    ImageDialog = QtWidgets.QDialog()
    ui = Ui_ImageDialog()
    ui.setupUi(ImageDialog)
    ImageDialog.show()
    sys.exit(app.exec_())

Any constructive help is appreciated. 任何建设性的帮助表示赞赏。

Qt Designer is used to create the graphic part, but not for the logic, you must create the logical part depending on the widget you used in the design. Qt Designer用于创建图形部件,但不用于逻辑部件,必须根据设计中使用的小部件创建逻辑部件。 In your case by the name I think it is QDialog . 以您的名字命名,我认为是QDialog

from ui_imagedialog import Ui_ImageDialog

from PyQt5 import QtCore, QtGui, QtWidgets

class ImageDialog(QtWidgets.QDialog, Ui_ImageDialog):
    def __init__(self, parent=None):
      super(ImageDialog, self).__init__(parent=parent)
      self.setupUi(self)

      self.pushButton.clicked.connect(self.accept)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = ImageDialog()
    w.show()
    sys.exit(app.exec_())

Observations: In the ui_imagedialog.py file there is no ImageDialog class, only the Ui_ImageDialog class, so I generated the error. 观察结果:ui_imagedialog.py文件中,没有ImageDialog类,只有Ui_ImageDialog类,所以我生成了错误。 Also in the design the button is called self.pushButton, so you can not call it self.okButton . 同样在设计中,该按钮称为self.pushButton,因此您不能将其self.okButton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM