pyqt一个小小的实例

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

时间: 2024-08-28 00:53:59

pyqt一个小小的实例的相关文章

每天一个JavaScript实例-html5拖拽

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-html5拖拽</title> <style> #drop{ width:300px; height:200px; background-

通过一个WPF实例进一步理解委托和事件

在前写过"浅谈C#中的委托"和"浅谈C#中的事件"两篇博客,内容有些抽象,似乎难以说明委托和事件的关系. 今天通过一个小程序来进一步说明二者的使用及联系. 首先新建一个WPF应用程序,取名TestDelegateAndEvent. 在.xmal中加入四个按钮,并添加Window_Loaded事件. 代码如下: <Window x:Class="TestDelegateAndEvent.MainWindow" xmlns="http

django 拷贝一个 model 实例

今天做一个拷贝功能,把某个 obj 拷贝并修改部分数据,提交表单后保存为一个新实例.结果google 出来的结果不对,都是相互copy 的代码,大概如下: 1 obj = MyModel.objects.get(id=1) 2 obj.pk = None 3 obj.save() 后来好不容易找到一个正确的,特此记录: 1 if request.method == "POST": 2 form = AuthorCopyForm(request.POST,instance=author)

每天一个JavaScript实例-使用带有定时器的函数闭包

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-使用带有定时器的函数闭包</title> <style> #redbox{ position:absolute; left:100px;

多次单击快捷方式,只运行一个程序实例

在应用程序安装之后,单击一次快捷方式,就运行一个程序实例,对于资源独占型程序来说,这样是不可以的,比如该程序使用了当前系统的某个端口,当同样的程序再次运行,再次试图占用同一个端口次,会提示"端口已经被占用的"异常.如此,必须在启动应用程序时,必须判断该程序是否已经有一个实例在运行.下面这个类中先判断该程序的实例有没有在运行,使用线程同步类EventWaitHandle(Boolean, EventResetMode, String)及注册正在等待 WaitHandle 的委托方法Reg

vue.js开发环境搭建以及创建一个vue实例

Vue.js 是一套构建用户界面的渐进式框架.Vue 只关注视图层, 采用自底向上增量开发的设计.Vue 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件. 在使用 vue.js 之前首先需要搭建 vue.js 的开发环境,下面,我们就来一步一步的搭建 vue.js 的环境: 1.首先,我们需要安装 node.js: 安装 node.js 请参考  node.js安装配置 ,可以打开命令行,输入 node -v,如果输出版本号,说明我们安装 node 环境成功,输入 npm

通过一个具体实例来理解WMI脚本编程-读取BIOS信息

Option Explicit 'WMI对象模型以及参考手册在MSDN2001中的位置: 'Platform SDK '   ->Setup and System Administration '   ->Windows Management Instrumentation '   ->WMI Reference '   ->Scripting API for WMI '对于Visual Stdio 2008: 'ms-help://MS.VSCC.v90/MS.MSDNQTR.v

利用反射获取一个类实例的所有属性名称和值 拼接为字符串

需要一个功能:获取一个类实例的所有属性名称和值并拼接为字符串,然后进行处理.代码保存在此以备后用. 建立一个类文件ClassToString.cs 以下代码可以直接使用,参数为一个类的实例,返回的是一个数组,[0]为属性名的拼接,[1]为属性Value的拼接. 对于类的属性类型没有限制,但是如果没有全部赋值的话,Value会范围默认值, 比如int为0,String为空,DateTime为0001/1/1 0:00:00,0,0,0 需要有需要的话可以在拼接的时候判断进行相应处理 ClassTo

每天一个JavaScript实例-从一个div元素删除一个段落

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-从一个div元素删除一个段落</title> <style> p{ padding:20px; margin:10px 0; width: