首页 > 代码库 > pyqt练习x2

pyqt练习x2

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

 

# Form implementation generated from reading ui file ‘F:\qtset\files.ui‘

#

# Created: Wed May 14 07:53:04 2014

#      by: PyQt4 UI code generator 4.9.6

#

# 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_MainWindow(object):

    def setupUi(self, MainWindow):

        MainWindow.setObjectName(_fromUtf8("MainWindow"))

        MainWindow.resize(502, 262)

        self.centralwidget = QtGui.QWidget(MainWindow)

        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))

        self.label = QtGui.QLabel(self.centralwidget)

        self.label.setGeometry(QtCore.QRect(10, 30, 71, 16))

        self.label.setObjectName(_fromUtf8("label"))

        self.label_2 = QtGui.QLabel(self.centralwidget)

        self.label_2.setGeometry(QtCore.QRect(10, 100, 81, 16))

        self.label_2.setObjectName(_fromUtf8("label_2"))

        self.edit01 = QtGui.QLineEdit(self.centralwidget)

        self.edit01.setEnabled(False)

        self.edit01.setGeometry(QtCore.QRect(87, 30, 411, 20))

        self.edit01.setObjectName(_fromUtf8("edit01"))

        self.edit01_2 = QtGui.QLineEdit(self.centralwidget)

        self.edit01_2.setEnabled(False)

        self.edit01_2.setGeometry(QtCore.QRect(87, 100, 411, 20))

        self.edit01_2.setObjectName(_fromUtf8("edit01_2"))

        self.T_oggled = QtGui.QPushButton(self.centralwidget)

        self.T_oggled.setEnabled(False)

        self.T_oggled.setGeometry(QtCore.QRect(130, 160, 75, 23))

        self.T_oggled.setObjectName(_fromUtf8("T_oggled"))

        self.C_ancel = QtGui.QPushButton(self.centralwidget)

        self.C_ancel.setEnabled(False)

        self.C_ancel.setGeometry(QtCore.QRect(220, 160, 75, 23))

        self.C_ancel.setObjectName(_fromUtf8("C_ancel"))

        self.S_ysstder = QtGui.QPushButton(self.centralwidget)

        self.S_ysstder.setEnabled(True)

        self.S_ysstder.setGeometry(QtCore.QRect(330, 160, 75, 23))

        self.S_ysstder.setObjectName(_fromUtf8("S_ysstder"))

        self.label_3 = QtGui.QLabel(self.centralwidget)

        self.label_3.setGeometry(QtCore.QRect(0, 190, 54, 21))

        self.label_3.setObjectName(_fromUtf8("label_3"))

        self.S_tart = QtGui.QLabel(self.centralwidget)

        self.S_tart.setGeometry(QtCore.QRect(57, 197, 221, 16))

        self.S_tart.setText(_fromUtf8(""))

        self.S_tart.setObjectName(_fromUtf8("S_tart"))

        MainWindow.setCentralWidget(self.centralwidget)

        self.menubar = QtGui.QMenuBar(MainWindow)

        self.menubar.setGeometry(QtCore.QRect(0, 0, 502, 23))

        self.menubar.setObjectName(_fromUtf8("menubar"))

        self.menu = QtGui.QMenu(self.menubar)

        self.menu.setObjectName(_fromUtf8("menu"))

        self.menu_O = QtGui.QMenu(self.menu)

        self.menu_O.setObjectName(_fromUtf8("menu_O"))

        MainWindow.setMenuBar(self.menubar)

        self.statusbar = QtGui.QStatusBar(MainWindow)

        self.statusbar.setObjectName(_fromUtf8("statusbar"))

        MainWindow.setStatusBar(self.statusbar)

        self.C_lose = QtGui.QAction(MainWindow)

        self.C_lose.setObjectName(_fromUtf8("C_lose"))

        self.A_bout = QtGui.QAction(MainWindow)

        self.A_bout.setObjectName(_fromUtf8("A_bout"))

        self.C_File = QtGui.QAction(MainWindow)

        self.C_File.setEnabled(False)

        self.C_File.setObjectName(_fromUtf8("C_File"))

        self.Sing_File = QtGui.QAction(MainWindow)

        self.Sing_File.setObjectName(_fromUtf8("Sing_File"))

        self.Sing_Path = QtGui.QAction(MainWindow)

        self.Sing_Path.setObjectName(_fromUtf8("Sing_Path"))

        self.menu_O.addAction(self.Sing_File)

        self.menu_O.addAction(self.Sing_Path)

        self.menu.addAction(self.menu_O.menuAction())

        self.menu.addAction(self.C_lose)

        self.menu.addSeparator()

        self.menu.addAction(self.A_bout)

        self.menu.addSeparator()

        self.menu.addAction(self.C_File)

        self.menubar.addAction(self.menu.menuAction())

 

        self.retranslateUi(MainWindow)

        QtCore.QMetaObject.connectSlotsByName(MainWindow)

 

    def retranslateUi(self, MainWindow):

        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))

        self.label.setText(_translate("MainWindow", "旧文件/路径:", None))

        self.label_2.setText(_translate("MainWindow", "输 出 路 径:", None))

        self.edit01.setToolTip(_translate("MainWindow", "输出路径", None))

        self.edit01_2.setToolTip(_translate("MainWindow", "输出路径", None))

        self.T_oggled.setToolTip(_translate("MainWindow", "转换", None))

        self.T_oggled.setText(_translate("MainWindow", "转换(C)", None))

        self.C_ancel.setToolTip(_translate("MainWindow", "取消", None))

        self.C_ancel.setText(_translate("MainWindow", "取消(X)", None))

        self.S_ysstder.setToolTip(_translate("MainWindow", "输出路径", None))

        self.S_ysstder.setText(_translate("MainWindow", "输出路径", None))

        self.label_3.setText(_translate("MainWindow", "状态:", None))

        self.S_tart.setToolTip(_translate("MainWindow", "用于显示输入,输出,错误信息等", None))

        self.menu.setTitle(_translate("MainWindow", "文件", None))

        self.menu_O.setTitle(_translate("MainWindow", "打开(O)", None))

        self.C_lose.setText(_translate("MainWindow", "退出", None))

        self.A_bout.setText(_translate("MainWindow", "关于", None))

        self.C_File.setText(_translate("MainWindow", "新建", None))

        self.Sing_File.setText(_translate("MainWindow", "文件(单个文件)", None))

        self.Sing_Path.setText(_translate("MainWindow", "文件夹(目录)", None))

 

 

if __name__ == "__main__":

    import sys

    app = QtGui.QApplication(sys.argv)

    MainWindow = QtGui.QMainWindow()

    ui = Ui_MainWindow()

    ui.setupUi(MainWindow)

    MainWindow.show()

    sys.exit(app.exec_())

 

 如图:

--------

源代码学习:

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

__author__ = ‘Administrator‘

from files import Ui_MainWindow

import sys,os,random

from PyQt4 import QtCore,Qt,QtGui

class Gs(QtGui.QMainWindow,Ui_MainWindow):

    def __init__(self,parent=None):

        super(Gs,self).__init__(parent)

        self.setupUi(self)

        self.C_lose.triggered.connect(self.Quit)

        self.A_bout.triggered.connect(self.abot)

        self.S_ysstder.clicked.connect(self.File_s)

        self.Sing_File.triggered.connect(self.File_s1)

        self.Sing_Path.triggered.connect(self.File_s2)

        self.C_ancel.clicked.connect(self.C_ancels)

        self.edit01_2.returnPressed.connect(self.File_s3)

        self.T_oggled.clicked.connect(self.To_olged)

    @QtCore.pyqtSlot(str)

    def To_olged(self,path):

        path=unicode(path)

        #2种,第一种是对文件(单一的)

        #第二种是对目录操作

        #如果对目录操作,选择的文件格式必须一样,不然就出现错误

        if  self.edit01.text()==‘‘ or  not (unicode(os.path.exists(self.edit01.text()))):

            QtGui.QMessageBox.information(self,u‘警告‘,u‘self.edit01.text()为空或1号路径无效‘)

        elif  self.edit01_2.text()==‘‘ or not (unicode(os.path.exists(self.edit01_2.text()))):

            QtGui.QMessageBox.information(self,u‘警告‘,u‘self.edit01_2.text()为空或2号路径无效‘)

        else:

            self.S_tart.setText(u‘文件格式转换单机版‘)

            QtCore.QProcess.startDetached(‘explorer ‘+unicode(self.edit01_2.text()))

        import string

        path=os.listdir(unicode(self.edit01.text()))#os.walk()返回一个三元素的tuple:当前路径、子文件夹名称、文件列表。

        for dirpath, dirnames, filenames in os.walk(unicode(self.edit01.text())):#dirnames:F:\a;path3:a.txt

            for path3 in filenames:

                print dirpath,path3

                path4=os.path.join(dirpath,path3)

                print path4

                print os.path.splitext(path4)[-1]

                print os.path.split(‘.‘)

                if os.path.split(‘.‘):pass#明天继续

                    # if os.path.splitext(path4)[-1]:

                    #     print ‘ok‘

 

    def Quit(self):

        self.close()

    def abot(self):

        QtGui.QMessageBox.information(self,u‘关于本工具‘,u‘文件格式转换单机版‘)

        self.S_tart.setText(u‘文件格式转换单机版‘)

    def File_s(self):

        if self.edit01_2.text()==‘‘:

            QtGui.QMessageBox.information(self,u‘警告‘,u‘请先选择文件下面模式,然后让输出变可用‘)

            self.S_tart.setText(unicode(self.edit01_2.text()))

        else:

            QtGui.QMessageBox.information(self,u‘提示‘,u‘输入的内容为:%s‘%(self.edit01_2.text()))

            QtCore.QProcess.startDetached(‘explorer ‘+unicode(self.edit01_2.text()))

            self.S_tart.setText(unicode(self.edit01_2.text()))

    def File_s1(self):

        path1=QtGui.QFileDialog.getOpenFileName(self,‘文件选择‘,‘/‘,‘*.*‘)

        self.edit01.setText(unicode(path1))

        self.edit01.setEnabled(1)

        self.C_ancel.setEnabled(1)

        self.T_oggled.setEnabled(1)

        self.edit01_2.setEnabled(1)

        self.S_tart.setText(unicode(path1))

        self.edit01_2.setText(unicode(path1))

    def File_s2(self):

        path=QtGui.QFileDialog.getExistingDirectory(self,u‘目录选择‘,‘/‘)

        self.edit01.setText(unicode(path))

        self.edit01.setEnabled(1)

        self.C_ancel.setEnabled(1)

        self.edit01_2.setEnabled(1)

        self.T_oggled.setEnabled(1)

        self.S_tart.setText(unicode(path))

        self.edit01_2.setText(unicode(path))

    def File_s3(self):

        path=QtGui.QFileDialog.getExistingDirectory(self,‘目录选择‘,‘/‘)

        self.edit01_2.setText(unicode(path))

        self.S_tart.setText(unicode(path))

    def C_ancels(self):

        p=QtGui.QMessageBox(self)

        p.setWindowTitle(u‘提示‘)

        a=p.setText(u‘您真的要取消本次操作吗?‘)

        p.setWindowIcon(QtGui.QIcon(‘i/a.png‘))

        b=p.setIconPixmap(QtGui.QPixmap(‘i/b.png‘))

        p1=p.addButton(u‘确定‘,QtGui.QMessageBox.ActionRole)

        p2=p.addButton(u‘取消‘,QtGui.QMessageBox.ActionRole)

        p.exec_()

        buonnect=p.clickedButton()

        if buonnect==p1:

            self.close()

        elif buonnect==p2:

            pass

app=QtGui.QApplication(sys.argv)

gs=Gs()

gs.show()

sys.exit(app.exec_())

 #QtGui.QDesktopServices.openUrl(QtCore.QUrl(unicode(self.edit01_2.text()),QtCore.QUrl.TolerantMode)

 #QtGui.QDesktopServices.openUrl(QtCore.QUrl.fromLocalFile(unicode(self.edit01_2.text())))

"""

 for path1 in path:

            path2= os.path.splitext(path1)[-1]

            print path2

            newname1= path2+‘.txt‘

            newname2=path2+‘.py‘

            if path2==‘.doc‘:

                os.rename(path2,newname1)

            if path2==‘.txt‘:

                os.rename(path2,newname2)

 

  """

pyqt练习x2