我学Java的时候也用Swing做了一个文件搜索的小程序,但界面真的挺丑的,现在学了点python,感觉python是最简单的语言,没有之一。 (大家没事都可以来学的,真的很简单有趣哦)
我采用的是pyqt5,所以需要先安装Pyqt5模块
直接cmd命令输入
1
|
pip install pyqt5 |
闲言少叙,上代码!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# -*- coding: utf-8 -*- # @Time : 2018\9\15 20:39 # @Author : Tang weiyang # @File : FileSearch02.py from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtGui import QIcon import sys import os import threading class fileSearchThread(QThread): sinOut = pyqtSignal( str ) # 自定义信号,执行run()函数时,从相关线程发射此信号 def __init__( self ,key): super ().__init__() self .key = key def run( self ): threads = [] path = [r "c:\\", r" d:\\ ", r" e:\\ ", r" f:\\"] #通过多线程对windows下的多个盘符进行文件的遍历查找 for each in path: t = threading.Thread(target = self .search, args = ( self .key,each,)) threads.append(t) t.start() for i in range ( len (threads)): #将主线程阻塞 threads[i].join() print ( "搜索结束" ) def search( self ,keyword, path): for dirpath, dirnames, filenames in os.walk(path): for filename in filenames: if filename.__contains__(keyword): print (os.path.join(dirpath, filename)) self .sinOut.emit(os.path.join(dirpath, filename)) for folder in dirnames: if folder.__contains__(keyword): print (os.path.join(dirpath,folder)) self .sinOut.emit(os.path.join(dirpath,folder)) class fileSearch(QListWidget): def __init__( self ): super ().__init__() self .Ui() def Ui( self ): self .key = QLineEdit() self .bt = QPushButton( "搜索" ) self .result = QListWidget() self .bt.clicked.connect( self .ButtonClicked) #按钮单击信号绑定到槽 # self.line.editingFinished.connect(self.Action) self .key.editingFinished.connect( self .ButtonClicked) grid = QGridLayout() grid.setSpacing( 10 ) # 创建标签之间的空间 grid.addWidget( self .key, 1 , 0 ) # (1,0)表示显示的位置 grid.addWidget( self .bt, 1 , 1 ) grid.addWidget( self .result, 2 , 0 , 5 , 2 ) # 指定组件的跨行和跨列的大小,指定这个元素跨5行显示 self .setLayout(grid) for i in range ( 1 , 100 ): self .result.addItem( "搜索" + str (i) + "个项目" ) self .result.itemClicked.connect( self .Clicked) self .setGeometry( 300 , 300 , 500 , 500 ) self .setWindowTitle( '文件搜索' ) self .setWindowIcon(QIcon( 'icon.jpg' )) self .show() def Clicked( self , item): QMessageBox.information( self , "ListWidget" , "You clicked: " + item.text()) os.startfile(item.text()) #打开文件 def ButtonClicked( self ): # 创建新线程,将自定义信号sinOut连接到slotAdd()槽函数 keyword = self .key.text() self .result.clear() self .thread = fileSearchThread(keyword) self .thread.sinOut.connect( self .slotAdd) self .thread.start() def slotAdd( self ,filename): self .result.addItem( str (filename)) if __name__ = = '__main__' : app = QApplication(sys.argv) ex = fileSearch() sys.exit(app.exec_()) |
这个小程序可以搜索本地所有的文件,时间大概在10秒左右,点击文件的地址,就可以打开这个文件.多线程这个有点卡壳,然后写的很变扭.
这个小程序还有一个小感悟:
涉及到GUI的程序最好要将UI界面和数据处理(还包括大批量文件的读取,循环计算),否则会导致UI界面无响应,这一点可以参考我的代码,通过一个多线程很好解决这个问题
线程之间的信息传递,可以通过信号和槽完成,
1
2
3
|
sinOut = pyqtSignal( str ) #自定义一个信号槽 self .thread.sinOut.connect( self .slotAdd) #信号绑定到槽 self .sinOut.emit(os.path.join(dirpath,folder)) #发射信号 |
pyqt的布局真的很好用,GridLayout真的超级好用
效果图如下
补充:pyqt5多线程-简单例子
一、主要代码逻辑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
from PyQt5 import QtWidgets, QtCore from testqt.TEST_QT_FROM import Ui_Dialog import sys from PyQt5.QtCore import * import time # 继承QThread class Runthread(QtCore.QThread): # python3,pyqt5与之前的版本有些不一样 # 通过类成员对象定义信号对象 _signal = pyqtSignal( str ) def __init__( self ): super (Runthread, self ).__init__() def __del__( self ): self .wait() def run( self ): print ( "run 666" ) self ._signal.emit( "run 666" ); # 信号发送 class TestQtFromC(QtWidgets.QWidget, Ui_Dialog): text = "" def __init__( self ): super (TestQtFromC, self ).__init__() self .setupUi( self ) #click def timer_click( self ): self .thread = Runthread() # 创建线程 self .thread._signal.connect( self .callbacklog) # 连接信号 self .thread.start() # 开始线程 # callback def callbacklog( self , msg): self .text = self .text + time.strftime( "%Y-%m-%d %H:%M:%S " , time.localtime()) + msg + "\n" print ( self .text) # 回调数据输出到文本框 self .textEdit.setText( self .text); if __name__ = = "__main__" : app = QtWidgets.QApplication(sys.argv) mTestQtFromC = TestQtFromC() mTestQtFromC.show() sys.exit(app.exec_()) |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/weiyang_tang/article/details/82748111