python 初学03 Eric+PyQt+python IDE与界面程序

近期一直在学习python和批处理,来将工作中的手工操作的低效环节用脚本自动实现。

已经实现了几个脚本。但是命令行窗口,总是不太友好,对执行结果的反馈也不清楚,就想实现可视化。

在网上找到Python可视化的编程的一个方法,周末专心实现了一下,效果还行,算是有头绪了。

http://blog.sina.com.cn/s/blog_514104fc0101c8yi.html

主要是按照上面这篇博客的方法实现的。感谢作者。

一、环境与软件版本

Eric特别挑软件版本,与PyQt 和 python的版本都要对应。

在网上搜索,以及自己安装尝试,验证Win7 64可用的软件版本配合为:

[Python3.3.2] + [PyQt4-4.10.2-gpl-Py3.3-Qt4.8.4-x64] + [Eric5.4.5]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2014/10/29 更新:Eric版本更新到5.5.0

上面的组合中,搭配 [Eric5.4.6]或[Eric5.5.0]都是可以的。

Eeric 中文实现:

1、下载对应版本的中文语言包。如 eric5-i18n-zh_CN.GB2312-5.5.0.zip

2、解压,将其中的内容替换到Eric安装目录下

3、到 eric5-5.5.0erici18n 目录中,将文件名中的 .GB2312 字段去掉。即可实现中文界面。

   eric5_zh_CN.GB2312.qm  ==> eric5_zh_CN.qm

 eric5_zh_CN.GB2312.ts    ==> eric5_zh_CN.ts

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

二、实现的效果

输入路径,点击“文件列表”,会在文本框中显示文件列表。这是为下一步进行文件处理打基础。

点击“清空”,清空路径与文本框。

点击“退出”,关闭程序。

三、源码

 1 # -*- coding: utf-8 -*-
 2 
 3 # Form implementation generated from reading ui file 'E:SVNranchesPythonHWhelloworld.ui'
 4 #
 5 # Created: Mon Jul 28 00:06:20 2014
 6 #      by: PyQt4 UI code generator 4.10.2
 7 #
 8 # WARNING! All changes made in this file will be lost!
 9 
10 from PyQt4 import QtCore, QtGui
11 
12 try:
13     _fromUtf8 = QtCore.QString.fromUtf8
14 except AttributeError:
15     def _fromUtf8(s):
16         return s
17 
18 try:
19     _encoding = QtGui.QApplication.UnicodeUTF8
20     def _translate(context, text, disambig):
21         return QtGui.QApplication.translate(context, text, disambig, _encoding)
22 except AttributeError:
23     def _translate(context, text, disambig):
24         return QtGui.QApplication.translate(context, text, disambig)
25 
26 class Ui_Dialog(object):
27     def setupUi(self, Dialog):
28         Dialog.setObjectName(_fromUtf8("Dialog"))
29         Dialog.resize(541, 300)
30         Dialog.setSizeGripEnabled(True)
31         self.LText = QtGui.QLineEdit(Dialog)
32         self.LText.setGeometry(QtCore.QRect(110, 20, 371, 21))
33         self.LText.setObjectName(_fromUtf8("LText"))
34         self.btn3 = QtGui.QPushButton(Dialog)
35         self.btn3.setGeometry(QtCore.QRect(20, 210, 75, 23))
36         self.btn3.setObjectName(_fromUtf8("btn3"))
37         self.BText = QtGui.QTextBrowser(Dialog)
38         self.BText.setGeometry(QtCore.QRect(110, 60, 371, 211))
39         self.BText.setObjectName(_fromUtf8("BText"))
40         self.btn1 = QtGui.QPushButton(Dialog)
41         self.btn1.setGeometry(QtCore.QRect(20, 90, 75, 23))
42         self.btn1.setObjectName(_fromUtf8("btn1"))
43         self.btn2 = QtGui.QPushButton(Dialog)
44         self.btn2.setGeometry(QtCore.QRect(20, 150, 75, 23))
45         self.btn2.setObjectName(_fromUtf8("btn2"))
46         self.label = QtGui.QLabel(Dialog)
47         self.label.setGeometry(QtCore.QRect(40, 20, 51, 16))
48         font = QtGui.QFont()
49         font.setFamily(_fromUtf8("微软雅黑"))
50         font.setPointSize(14)
51         self.label.setFont(font)
52         self.label.setObjectName(_fromUtf8("label"))
53 
54         self.retranslateUi(Dialog)
55         QtCore.QObject.connect(self.btn3, QtCore.SIGNAL(_fromUtf8("clicked()")), Dialog.close)
56         QtCore.QMetaObject.connectSlotsByName(Dialog)
57 
58     def retranslateUi(self, Dialog):
59         Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
60         self.btn3.setText(_translate("Dialog", "退出", None))
61         self.btn1.setText(_translate("Dialog", "文件列表", None))
62         self.btn2.setText(_translate("Dialog", "清空", None))
63         self.label.setText(_translate("Dialog", "路径:", None))
64 
65 
66 if __name__ == "__main__":
67     import sys
68     app = QtGui.QApplication(sys.argv)
69     Dialog = QtGui.QDialog()
70     ui = Ui_Dialog()
71     ui.setupUi(Dialog)
72     Dialog.show()
73     sys.exit(app.exec_())
UI Code
 1 # -*- coding: utf-8 -*-
 2 
 3 """
 4 Module implementing Dialog.
 5 """
 6 
 7 from PyQt4.QtCore import pyqtSlot
 8 from PyQt4.QtGui import QDialog
 9 
10 from Ui_helloworld import Ui_Dialog
11 #import the module
12 import PyQt4,  PyQt4.QtGui,  sys,  os
13 
14 class Dialog(QDialog, Ui_Dialog):
15     """
16     Class documentation goes here.
17     """
18     def __init__(self, parent=None):
19         """
20         Constructor
21         
22         @param parent reference to the parent widget (QWidget)
23         """
24         super().__init__(parent)
25         self.setupUi(self)
26                 
27     @pyqtSlot()
28     def on_btn1_clicked(self):
29         """
30         Slot documentation goes here.
31         """
32         destdir = self.LText.text()
33         #fix the bug
34         buglist = ['C:',  'D:',  'E:',  'F:',  'G:',  'H:',  'I:',  'J:',  'K:',  'X:',  'Y:',  'Z:']
35         if destdir.upper() in buglist:
36             destdir = destdir + '/'
37             
38         #if the path does not exists or is null 
39         if not os.path.exists(destdir) or destdir == "":
40            destdir = os.getcwd()
41            filestr = "请输入有效路径!"
42            self.BText.setText(filestr)
43         #else
44         else:
45             filestr = "文件列表:"
46             filelist= os.listdir(destdir)
47             for file in filelist:
48                 filestr = filestr + '
' + str(file)
49             self.BText.setText(filestr)
50         
51     @pyqtSlot()
52     def on_btn2_clicked(self):
53         """
54         Slot documentation goes here.
55         """
56         # TODO: not implemented yet
57 
58         #clean the LineEdit and the TextBrowser
59         textempty = ""
60         self.BText.setText(textempty)
61         self.LText.setText(textempty)
62 if __name__ == "__main__":
63     app = PyQt4.QtGui.QApplication(sys.argv)
64     dlg = Dialog()
65     dlg.show()
66     sys.exit(app.exec())
67     
Func Code


四、学到的

1.对于界面上对象属性的处理,要对过界面对象实现。比对,我先将LineEdit中输入的值,赋给一个变量,然后在TextBrowser中显示,尝试失败。

五、待改进

程序运行时,tab顺序正好位于“退出”上,敲回车直接退出程序了,没有找到好的方法来使tab顺序位于“文件列表”上,这样输入路径后敲回车即可。

六、BUG

1.使用Generage Dialog Code生成代码,其中有一行需要修改。from .Ui_helloworld import Ui_Dialog 多了一个点,要去掉。

2.当路径是程序所在盘的盘符时(如e: 或 E: 不带斜杠),python判断路径不存在。而路径带上斜杠(为e:或E: 时),一切正常。其他盘不受影响,带不带斜杠均可。目前用一个判断,统一给加上“/”

原文地址:https://www.cnblogs.com/itit/p/3872281.html