2016-03-23 12 views
0

PyQt4.Myコンパイル環境でダイアログを作成したいのですが、Qt4、Python2、PyQt4です。 私は自分の仕事のために何かをしました。 1. Qt Designerとdialog.uiというuiファイルで自分のUIを完成させます。 2. "pyuic -o ui_dialog.py dialog.ui"コマンドを使用して、pythonファイルの名前をui_dialog.pyにします。 ui_dialog.pyのコードは3.Iはmain.pyファイルを書いて、それをコンパイルしようPyQt4:私の最初のコードを書くときにダイアログを開くことができません

# -*- 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)) 

です。 main.pyの私のコードはmain.pyで

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

ある 、私は「クラスChatDialog」の「setupUIを()」を書く方法を知らない

どのように私は私のコードを完了することができます?あなたが私を助けることができれば嬉しいです。ありがとう

答えて

0

Qtデザイナで作成されたpythonファイルを使用するこの方法では、通常、クラスはUi_Dialogクラスを継承します。

class ChatDialog(QtGui.QDialog, Ui_Dialog): 

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

時には、人々は継承しませんが、クラスの属性に代入されます:あなたのanswer.Iため

class ChatDialog(QtGui.QDialog): 

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

おかげであなたの答えからの継承の2つの方法が知られています。 –

関連する問題