PyQt4:当我写我的第一个代码时无法打开我的对话框 [英] PyQt4:Can‘t Open My Dialog When I Write My First Code

查看:52
本文介绍了PyQt4:当我写我的第一个代码时无法打开我的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 PyQt4 建立一个对话框.我的编译环境是 Qt4、Python2 和 PyQt4.我为我的工作做了一些事情.1.我使用 Qt Designer 和名为 dialog.ui 的 ui 文件完成了我的 UI.2.我使用命令pyuic -o ui_dialog.py dialog.ui"制作名为ui_dialog.py的python文件.ui_dialog.py 的代码是

I want to build a dialog with the PyQt4.My compile envirenment is Qt4,Python2 and PyQt4. I have done something for my job. 1.I complete my UI with Qt Designer and the ui file named dialog.ui. 2.I use the command "pyuic -o ui_dialog.py dialog.ui" to make the python file named ui_dialog.py. the code of ui_dialog.py is

# -*- coding: utf-8 -*-

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

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(516, 378)
        self.verticalLayoutWidget = QtGui.QWidget(Dialog)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 13, 501, 361))
        self.verticalLayoutWidget.setObjectName(_fromUtf8("verticalLayoutWidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.label_recieve = QtGui.QLabel(self.verticalLayoutWidget)
        self.label_recieve.setObjectName(_fromUtf8("label_recieve"))
        self.verticalLayout.addWidget(self.label_recieve)
        self.textBrowser_recieve = QtGui.QTextBrowser(self.verticalLayoutWidget)
        self.textBrowser_recieve.setObjectName(_fromUtf8("textBrowser_recieve"))
        self.verticalLayout.addWidget(self.textBrowser_recieve)
        self.label_send = QtGui.QLabel(self.verticalLayoutWidget)
        self.label_send.setObjectName(_fromUtf8("label_send"))
        self.verticalLayout.addWidget(self.label_send)
        self.textEdit_send = QtGui.QTextEdit(self.verticalLayoutWidget)
        self.textEdit_send.setObjectName(_fromUtf8("textEdit_send"))
        self.verticalLayout.addWidget(self.textEdit_send)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.pushButton_send = QtGui.QPushButton(self.verticalLayoutWidget)
        self.pushButton_send.setObjectName(_fromUtf8("pushButton_send"))
        self.horizontalLayout.addWidget(self.pushButton_send)
        self.verticalLayout.addLayout(self.horizontalLayout)

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

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "chat", None))
        self.label_recieve.setText(_translate("Dialog", "recieve", None))
        self.label_send.setText(_translate("Dialog", "send", None))
        self.pushButton_send.setText(_translate("Dialog", "SEND", None))

3.我尝试编写main.py文件并编译它.我的 main.py 代码是

3.I try to write the main.py file and compile it. My code of main.py is

'''
title:chat dialog
author:CCBANG
virsion:0.1
'''
from PyQt4 import QtCore,QtGui 
import sys
from ui_dialog import *

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class ChatDialog(QtGui.QDialog):
        def __init__(self,parent=None):
                QtGui.QDialog.__init__(self,parent)                
                self=Ui_Dialog()

if __name__=='__main__':
    app = QtGui.QApplication(sys.argv)
    myqq=ChatDialog()
    myqq.show()
    sys.exit(app.exec_())

在 main.py 中,我不知道如何在Class ChatDialog"中编写setupUI()"

In the main.py ,I don't know how to write "setupUI()" in the "Class ChatDialog"

我怎样才能完成我的代码?如果你能帮助我,我会很高兴.谢谢

How can i finish my code ? I would be happy if you can help me. Thanks

推荐答案

通过这种使用 Qt Designer 创建的 python 文件的方法,通常你的类会继承 Ui_Dialog 类.

With this method of using Qt Designer created python files, generally, your class will inherit the Ui_Dialog class.

class ChatDialog(QtGui.QDialog, Ui_Dialog):

    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)                
        self.setupUi(self)

有时,人们不会继承,而是将其分配给类的一个属性:

Sometimes, people won't inherit, but will assign it to an attribute of the class:

class ChatDialog(QtGui.QDialog):

    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)                
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

这篇关于PyQt4:当我写我的第一个代码时无法打开我的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆