标签:
from __future__ import division
# -*- coding: utf-8 -*-
__author__ = ‘Administrator‘
try:
from PyQt4.Qt import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys,os
from ok import Ui_Form
import xlwt,xlrd,win32com.client
import datetime,time
from Start import Ui_Start12 as From1
from OKNO import Ui_Form as From2
except:
pass
from PyQt4 import QtCore, QtGui
class Ui_FormMian(QDialog,Ui_Form):
def __init__(self,parnet=None):
super(Ui_FormMian,self).__init__(parnet)
self.setupUi(self)
self.Button.setEnabled(True)
self.OpenDirectory.setEnabled(True)
self.select.clicked.connect(self.FileOk)
self.scanning.clicked.connect(self.FilePath)
self.clearList.clicked.connect(self._clearList)
self.Button1.clicked.connect(self.saveAs)
self.Button.clicked.connect(self.OpenAs)
self.looger.toggled.connect(self.Looger)
self.SS1=S1()#子窗口
self.createContextMenu()
self.OKWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.OpenDirectory.clicked.connect(self.currentPath)
self.OKWidget.currentCellChanged.connect(self.Nones)
self.OKWidget.customContextMenuRequested.connect(self.conmenx)
self.select_dicselect.clicked.connect(self.selectitems)
self.SS1.start1.clicked.connect(self.Begin)
#写入txt地区
def Looger(self):
if self.looger.isChecked():
with open(‘text/txt.txt‘,‘w‘)as f:
f.writelines(list(‘在‘+datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)+‘记录‘))
button=QMessageBox(self)
button.setText(u‘内容已经保存了,是否需要打开?‘)
button.setWindowTitle(‘Tip‘)
yes=button.addButton(‘Yes‘,QMessageBox.ActionRole)
no=button.addButton(‘No‘,QMessageBox.ActionRole)
button.exec_()
button=button.clickedButton()
if button==yes:
os.system(r‘text\txt.txt‘)
else:
return
#打开
def FilePath(self):
path=self.PathEdit.text()
path1=QDir(path)
if path.isEmpty():#判断路径是否为空
QMessageBox.information(self,‘Tip‘,u‘当前路径未选择无法进行扫描‘)
else:
if not path1.exists():#判断路径是否不合法(也叫不存在)
QMessageBox.information(self,‘Tip‘,u‘当前路径不合法,请检查‘)
else:
self.statusbar.setText(path)#此处开始进行查询和消息的显示
self.SS1.PathEdit.setText(path)
self.SS1.show()
#self.Begin()#不需要
def OpenAs(self):#导入
filename=unicode(QFileDialog.getOpenFileName(self,‘Save File‘,‘‘,‘*.xls(*.xls)‘))
xls1=xlrd.open_workbook(filename)
name=xls1.sheet_names()
t1=xls1.sheets()[0]
t2=xls1.sheet_by_index(0)
t3=xls1.sheet_by_name(u‘sheet‘)
row=t3.nrows
cow=t3.ncols
for i in range(row):
for j in range(cow):
rowcow=t3.cell(i,j).value
table=QTableWidgetItem(unicode(rowcow))
self.OKWidget.setItem(i,j,table)
self.OKWidget.setRowCount(row+1)
rowx=self.OKWidget.rowCount()
self.OKWidget.insertRow(rowx)
def Nones(self,i=None,j=None):#判断是否为空
item=self.OKWidget.item(i,j)
if item==None or (item and item==‘‘):
self.Button1.setEnabled(False)
else:
self.Button1.setEnabled(True)
def saveAs(self):#导出
filename=unicode(QFileDialog.getSaveFileName(self,‘Save File‘,‘‘,‘*.xls(*.xls)‘))
wbk=xlwt.Workbook()
sheet=wbk.add_sheet(‘sheet‘,cell_overwrite_ok=True)
self.add2(sheet)
wbk.save(filename)
def add2(self,sheet):
for currentColumn in range(self.OKWidget.columnCount()):
for currentRow in range(self.OKWidget.rowCount()):
try:
teext =unicode(self.OKWidget.item(currentRow, currentColumn).text())
sheet.write(currentRow, currentColumn, teext)
except AttributeError:
pass
def FileOk(self):
f=QFileDialog.getExistingDirectory(self,u‘选择‘,‘/‘)
self.PathEdit.setText(unicode(f))
return f
def Fipath(self,path):
self.OKWidget.setItem(self.OKWidget.rowCount()-1,2,QTableWidgetItem(u‘文件夹‘))
def Begin(self):#开始扫描
row=self.OKWidget.rowCount()
cow=self.OKWidget.columnCount()
paths=unicode(self.SS1.PathEdit.text())
size=QTableWidgetItem(unicode(get_size(unicode(paths))))
name=os.path.splitext(unicode(paths))[0][3:].split(‘\\‘)[-1]
type=QTableWidgetItem(u‘文件夹‘)
vimo=QTableWidgetItem(u‘无法支持‘)
path1=os.path.split(unicode(paths))[0]
time1=time.ctime(os.path.getmtime(unicode(paths)))
self.OKWidget.setItem(self.OKWidget.rowCount()-1,0,QTableWidgetItem(name))
self.OKWidget.setItem(self.OKWidget.rowCount()-1,1,size)
self.OKWidget.setItem(self.OKWidget.rowCount()-1,2,type)
self.OKWidget.setItem(self.OKWidget.rowCount()-1,3,QTableWidgetItem(vimo))
self.OKWidget.setItem(self.OKWidget.rowCount()-1,4,QTableWidgetItem(path1))
self.OKWidget.setItem(self.OKWidget.rowCount()-1,5,QTableWidgetItem(time1))
self.OKWidget.setRowCount(row+1)
self.OKWidget.currentItemChanged.connect(self.activation)
def conmenx(self,point):
self.menu.exec_(self.OKWidget.mapToGlobal(point))
def createContextMenu(self):
self.OKWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.menu=QtGui.QMenu(self)
menu1=self.menu.addAction(u‘删除空白行‘)
menu1.triggered.connect(self.M1clear)
def M1clear(self):
self.About()
def About(self):
button=QMessageBox(self)
button.setText(u‘您真的要删除此行吗?‘)
button.setWindowTitle(‘Tip‘)
yes=button.addButton(‘Yes‘,QMessageBox.ActionRole)
no=button.addButton(‘No‘,QMessageBox.ActionRole)
button.exec_()
button=button.clickedButton()
if button==yes:
row=self.OKWidget.currentRow()
if row!=-1:
self.OKWidget.removeRow(row)
elif row==-1:
QMessageBox.information(self,‘Tip‘,u‘当前一行都没有,无法删除‘)
self.about()
def currentPath(self):
path=self.PathEdit.text()
path1=QDir(path)
if path.isEmpty():#判断路径是否为空
QMessageBox.information(self,‘Tip‘,u‘当前路径未选择无法进行扫描‘)
else:
if not path1.exists():#判断路径是否不合法(也叫不存在)
QMessageBox.information(self,‘Tip‘,u‘当前路径不合法,请检查‘)
else:
path=QDir.setCurrent(unicode(self.PathEdit.text()))
path1=QDir.currentPath()
path1.replace("/","\\")
QProcess.startDetached("explorer "+path1)
def about(self):
button=QMessageBox(self)
button.setText(u‘当前表格处是空白行,是否要增加一空白行?‘)
button.setWindowTitle(‘Tip‘)
yes=button.addButton(u‘是的‘,QMessageBox.ActionRole)
no=button.addButton(u‘不需要‘,QMessageBox.ActionRole)
button.exec_()
button=button.clickedButton()
if button==yes:
row=self.OKWidget.rowCount()
self.OKWidget.setRowCount(row+1)
else:
pass
def activation(self):
row=self.OKWidget.rowCount()
cow=self.OKWidget.columnCount()
for row in range(row):
for cow in range(cow):
if self.OKWidget.item(row,cow)!=None:
text=self.OKWidget.item(row,cow)
self.select_dicselect.setEnabled(True)
self.clearList.setEnabled(True)
self.Button.setEnabled(True)
self.Button1.setEnabled(True)
def _clearList(self):
self.OKWidget.clear()
def selectitems(self):#有问题
row=self.OKWidget.rowCount()
cow=self.OKWidget.columnCount()
for i in range(row):
for j in range(cow):
item=self.OKWidget.item(i,j)
if self.select_dicselect.click():
self.OKWidget.setEnabled(False)
else:
self.OKWidget.setEnabled(1)
def get_size(src):
‘‘‘Get the size of a directory or a file‘‘‘
size=0L
if os.path.isfile(src):
size=os.stat(src)[6]
elif os.path.isdir(src):
for item in os.listdir(src):
itemsrc=os.path.join(src,item)
#iterate to caculate the directory size
size+=get_size(itemsrc)
return size
class S1(QWidget,From1):
def __init__(self,panret=None):
super(S1,self).__init__(panret)
self.setupUi(self)
self.countlen.setText(u‘0个‘)
self.countlen.setReadOnly(True)
self.PathEdit.setReadOnly(True)
self.s2=S2()
self.startok1.clicked.connect(self.jilu)
self.start1.clicked.connect(self.BarJD)
self.step = 0
self.step1 = 0
self.start1.clicked.connect(self.clicked1sd)
self.Thread1=Thread1()
self.connect(self.Thread1, SIGNAL("timeout()"), self.updateTime)
self.counting1 = False
self.msInLastLaps = 0
self.countlen_2.setSegmentStyle(QLCDNumber.Flat)
self.timer = QtCore.QTimer(self)
self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.slotTimerEvent)
self.displayTime(0, False)
self._tim1= QBasicTimer()
self.PathEdit.returnPressed.connect(self.retus)
def displayTime(self, msecs, exact):
ms = msecs % 1000
msecs /= 1000
sec = msecs % 60
msecs /= 60
min = msecs % 60
msecs /= 60
hours = msecs
if exact:
timestring = ‘%02d:%02d:%02d.%03d‘ % (hours, min, sec, ms)
self.countlen_2.setNumDigits(12 if hours > 0 else 9)
self.countlen_2.display(timestring)
else:
timestring = ‘%02d:%02d:%02d‘ % (hours, min, sec)
self.countlen_2.setNumDigits(8 if hours > 0 else 5)
self.countlen_2.display(timestring)
def clicked1sd(self):
if ( self.counting1 ) :
print "stop ", str(QtCore.QTime.currentTime().toString())
self.timer.stop()
self.msInLastLaps += self.startTime.msecsTo(QtCore.QTime.currentTime());
self.displayTime(self.msInLastLaps, True)
self.start1.setText(u"开始")
self.start()
else:
self.startTime = QtCore.QTime.currentTime()
print "start ", str(self.startTime.toString())
self.timer.start(500)
self.start1.setText(u"暂停")
self.stop()
self.slotTimerEvent()
self.counting1 = not self.counting1
def slotBnClearClicked(self):
self.msInLastLaps = 0
self.startTime = QTime.currentTime()
self.displayTime(0, not self.counting1)
def slotTimerEvent(self):
self.displayTime(self.msInLastLaps + self.startTime.msecsTo(QTime.currentTime()), False)
def Sivshow(self):
self.s2.extEdit.appendPlainText(u‘搜索路径:‘+unicode(self.PathEdit.text()))
self.s2.extEdit.appendPlainText(u‘搜索路径:‘+unicode(self.countlen.text()))
self.s2.extEdit.appendPlainText(u‘扫描时间:‘+unicode(self.countlen_2.text()))
def jilu(self):
self.s2.show()
text=self.PathEdit.text()
count=self.countlen.text()
if unicode(text.isEmpty()) and unicode(count.isEmpty()):
self.s2.extEdit.setPlainText(u‘亲,未有信息存在‘)
else:
self.Sivshow()
def updateTime(self):
self.slotTimerEvent()
self.num+=1
def stop(self):
path=self.PathEdit.text()
self.Thread1.stop()
self.walk()
self.PathEdit.setText(path)
self.label_4.setText(u‘正在扫描‘)
def start(self):
path=self.PathEdit.text()
self.num=0
self.walk()
self.PathEdit.setText(path)
self.label_4.setText(u‘暂停扫描‘)
self.Thread1.start()
def timerEvent(self,event):
if self.step>=100:
self._tim1.stop()
self.label_jinting.setText(u‘扫描完成‘)
self.label_4.setText(u‘扫描成功‘)
self.timer.stop()
return
self.step+=1
self.Barjindu.setValue(self.step)
self.label_jinting.setText(u‘当前进度为:%s‘%self.step+str(‘%‘))
def retus(self):
f=QFileDialog.getExistingDirectory(self,‘/‘,‘/‘)
self.PathEdit.setText(unicode(f))
def BarJD(self):
if self._tim1.isActive():
self._tim1.stop()
self.label_jinting.setText(u‘扫描进度继续‘)
else:
self._tim1.start(100,self)
self.label_jinting.setText(u‘进度暂停‘)
def walk(self):
count=0
path=unicode(self.PathEdit.text())
for root,dirs,files in os.walk(path):
flen=len(files)
if flen!=0:
count+=flen
self.countlen.setText(unicode(count)+u‘个‘)
class Thread1(QThread):
def __init__(self, parent=None):
super(Thread1,self).__init__(parent)
self.stoped=False
self.mutex=QMutex()#提供的是线程之间的访问顺序化
def run(self):
with QMutexLocker(self.mutex):#QMutexLocker ( QMutex * mutex )
self.stoped=False
while True:
if self.stoped:
return
self.emit(SIGNAL(‘updateTime()‘))#发送信号
time.sleep(1)
def stop(self):
with QMutexLocker(self.mutex):
self.stoped=True
def isStoped(self):
with QMutexLocker(self.mutex):
return self.stoped
class S2(QWidget,From2):
def __init__(self,parnet=None):
super(S2,self).__init__(parnet)
self.setupUi(self)
self.extEdit.setReadOnly(True)
self.close1.clicked.connect(self.quit1)
def quit1(self):
self.close()
def main():
app=QApplication(sys.argv)
Ui=Ui_FormMian()
Ui.setWindowTitle(u‘小工具‘)
Ui.setFixedSize(618,465)
Ui.show()
sys.exit(app.exec_())
main()
标签:
原文地址:http://www.cnblogs.com/mhxy13867806343/p/4300020.html