# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'E:\MyPro\code\PyQT\word_to_txt.ui' # # Created: Tue Sep 19 19:04:14 2017 # by: PyQt4 UI code generator 4.10.2 # # WARNING! All changes made in this file will be lost! import sys import os import time from PyQt4 import QtCore, QtGui reload(sys) sys.setdefaultencoding('utf-8') 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(QtGui.QDialog): def __init__(self): super(Ui_Dialog, self).__init__() self.setupUi(self) # self.retranslateUi(self) def setupUi(self, Dialog): Dialog.setObjectName(_fromUtf8("Dialog")) Dialog.resize(600, 600) self.word_label = QtGui.QLabel(Dialog) self.word_label.setGeometry(QtCore.QRect(20, 40, 54, 12)) self.word_label.setObjectName(_fromUtf8("word_label")) self.txt_label = QtGui.QLabel(Dialog) self.txt_label.setGeometry(QtCore.QRect(20, 150, 54, 12)) self.txt_label.setObjectName(_fromUtf8("txt_label")) self.word_path = QtGui.QLineEdit(Dialog) self.word_path.setGeometry(QtCore.QRect(90, 40, 281, 20)) self.word_path.setObjectName(_fromUtf8("word_path")) self.txt_path = QtGui.QLineEdit(Dialog) self.txt_path.setGeometry(QtCore.QRect(90, 150, 281, 20)) self.txt_path.setObjectName(_fromUtf8("txt_path")) self.exchange_btn = QtGui.QPushButton(Dialog) self.exchange_btn.setGeometry(QtCore.QRect(140, 100, 75, 23)) self.exchange_btn.setObjectName(_fromUtf8("exchange_btn")) self.text_edit = QtGui.QTextEdit(Dialog) self.text_edit.setGeometry(QtCore.QRect(20, 180, 550, 400)) # self.text_edit.setObjectName(_fromUtf8("rizhi")) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): Dialog.setWindowTitle(_translate("Dialog", "批量轉換工具", None)) Dialog.setWindowIcon(QtGui.QIcon("710.png")) self.word_label.setText(_translate("Dialog", "word路徑", None)) self.txt_label.setText(_translate("Dialog", "txt路徑", None)) self.exchange_btn.setText(_translate("Dialog", "轉換", None)) QtCore.QObject.connect(self.exchange_btn, QtCore.SIGNAL("clicked()"), self.exchange) def no_such_path_warning(self): msg_box = QtGui.QMessageBox(QtGui.QMessageBox.Warning, u"警告", u"\n請輸入正確地址!") msg_box.show() msg_box.exec_() def exchange(self): word_pat = self.word_path.text() txt_pat = self.txt_path.text() if not os.path.exists(word_pat): self.no_such_path_warning() else: self.text_edit.append(word_pat) self.text_edit.append(txt_pat) if __name__ == '__main__': app = QtGui.QApplication(sys.argv) ex = Ui_Dialog() ex.show() sys.exit(app.exec_())
pyqt4轉ui文件爲py:python
在cmd控制檯下,進入uic目錄:app
cd C:\Cocos\tools\Python27\Lib\site-packages\PyQt4\uic
輸入命令ide
python pyuic.py ui文件路徑 -o py文件路徑
由於Qt生成的代碼得要有其它東西調用才能出來界面
所以若是想運行出界面還需另外添加以下代碼函數
if __name__ == "__main__": app = QtGui.QApplication(sys.argv) myapp = MyForm() #MyForm是本身的窗體類名 myapp.show() sys.exit(app.exec_())
在類中添加初始化:工具
class Ui_Dialog(QtGui.QDialog):
def __init__(self): super(Ui_Dialog, self).__init__() self.setupUi(self)
注意,自動生成的類可能須要繼承(QtGui.QDialog)ui
爲了實現界面與底層功能代碼分離建議另外新建一個文件用來編寫底層功能代碼,如新建一個main.py:this
from PyQt4 import QtGui import sys from GammaSpecPro3 import Ui_MainWindow #GammaSpecPro3爲***.ui轉換的py文件名,也就至關於模塊。 #Ui_MainWindow爲GammaSpecPro3界面的類,打開該py文件便可看到 class MyWindow(QtGui.QMainWindow): def __init__(self): super(MyWindow, self).__init__() self.ui = Ui_MainWindow() #這裏須要注意self.ui已經將Ui_MainWindow類實例 #化,所以繼承了該類的全部屬性,後面更改設置屬性都用self.ui「冠名」 self.ui.setupUi(self) =======================我是註釋======================== 這個類的下面就能夠編寫(def)本身的函數,如按鈕的功能等 def。。。 =======================註釋完畢======================== if __name__ == "__main__": app = QtGui.QApplication(sys.argv) w = MyWindow() w.show() sys.exit(app.exec_())
以後直接運行main.pyspa