Python PyQt5實(shí)戰(zhàn)項(xiàng)目之文件拷貝器的具體實(shí)現(xiàn)詳解
簡(jiǎn)介
寫了一個(gè)簡(jiǎn)單的文件夾內(nèi)容下所有文件復(fù)制到另一個(gè)文件夾內(nèi),主要邏輯代碼是來自《2小時(shí)玩轉(zhuǎn)python多線程編程》中的一個(gè)章節(jié)。
UI設(shè)置
def ui_init(self):
'''
界面的函數(shù)
'''
self.setWindowTitle('拷貝器')
self.resize(600,400)
self.setMinimumSize(600,400) # 設(shè)置窗口的最小值
'''控件'''
self.root_btn = QPushButton()
self.copy_btn = QPushButton()
self.start_btn = QPushButton()
self.root_text = QTextBrowser()
self.copy_text = QTextBrowser()
self.log = QTextBrowser()
self.h1_layout = QHBoxLayout()
self.h2_layout = QHBoxLayout()
self.h3_layout = QHBoxLayout()
self.v_layout = QVBoxLayout()
self.progerss =QProgressBar()
self.finish_sound = QSound(':resource/finish.wav') # 設(shè)置提示音
'''控件設(shè)置'''
self.root_btn.setText('選擇文件夾')
self.root_btn.setFixedSize(150,30)
self.copy_btn.setText('選擇拷貝路徑')
self.copy_btn.setFixedSize(150,30)
self.start_btn.setText('開始')
self.start_btn.setFixedSize(50,30)
self.root_text.setFixedHeight(27)
self.copy_text.setFixedHeight(27)
self.progerss.setValue(0)
'''控件擺放'''
self.h1_layout.addWidget(self.root_text)
self.h1_layout.addWidget(self.root_btn)
self.h2_layout.addWidget(self.copy_text)
self.h2_layout.addWidget(self.copy_btn)
self.h3_layout.addWidget(self.progerss)
self.h3_layout.addWidget(self.start_btn)
self.v_layout.addLayout(self.h1_layout)
self.v_layout.addLayout(self.h2_layout)
self.v_layout.addWidget(self.log)
self.v_layout.addLayout(self.h3_layout)
self.setLayout(self.v_layout)
這次加入了一個(gè)完成的音效
- QSound解析文件時(shí),可能會(huì)出現(xiàn)這問題
QSoundEffect(qaudio): Error decoding source
self.finish_sound = QSound('resource/finish.wav') # 設(shè)置提示音 原來這這樣寫的,但會(huì)出現(xiàn)上面的問題,就在寫一個(gè)qrc文件,再將qrc文件轉(zhuǎn)成py文件,再引入這個(gè)py文件,這樣就可以使用了。在使用這個(gè)音頻只需要在路徑上加一個(gè) : ,就如這樣self.finish_sound = QSound(':resource/finish.wav') # 設(shè)置提示音
- qrc文件轉(zhuǎn)py文件
先新建一個(gè)txt文件,在向里面寫入這樣的語句:
<RCC> <qresource prefix ="resource/"> <file alias="finish.wav">resource/finish.wav</file> </qresource> </RCC>
resource/是放音頻的文件夾名
finish.wav是音頻名
resource/finish.wav是完整音頻路徑
接著將文件后綴改為qrc,在利用cmd命令窗中鍵入pyrcc5 -o resource.qrc resource.py,將.qrc文件轉(zhuǎn)成.py文件。
主要邏輯
def variates_init(self):
'''
儲(chǔ)存變量的函數(shù)
'''
self.root_path = '' # 要拷貝的路徑
self.copy_path = '' # 要拷貝到的路徑
self.file_list = [] # 文件名集合
self.len = 0 # 文件夾下文件數(shù)量
def copy_file(self):
'''
拷貝文件的函數(shù)
'''
count = 0 # 臨時(shí)設(shè)置進(jìn)度條數(shù)值
self.progerss.setRange(0,self.len) # 設(shè)置進(jìn)度條的數(shù)值
self.progerss.setValue(0) # 設(shè)置進(jìn)度條初始值
'''拷貝器主邏輯'''
for file in self.file_list:
root_path = self.root_path + "/" + file
copy_path = self.copy_path + "/" + file
with open(root_path, "rb") as root_file:
with open(copy_path, "wb") as copy_file:
while True:
data = root_file.read(1024)
if data:
copy_file.write(data)
else:
count += 1
self.progerss.setValue(count)
break
def dir_file(self):
'''
遍歷目錄的函數(shù)
'''
filelist = os.listdir(self.root_path)
self.file_list = filelist
def len_file(self):
'''
文件數(shù)量的函數(shù)
'''
self.len=len(self.file_list)
拷貝器的邏輯:
- 從文件名集合中獲取文件名
- 合并出原始文件路徑和拷貝到的路徑
- 根據(jù)原始文件路徑打開文件模式為只讀,根據(jù)拷貝到的路徑新建一個(gè)文件寫入
- 拷貝的文件每次寫入1024字節(jié),當(dāng)沒有數(shù)據(jù)后,就結(jié)束寫入并保存文件,進(jìn)度條數(shù)值加1
信號(hào)與槽
def connect_init(self):
'''
信號(hào)與槽連接的函數(shù)
'''
self.root_btn.clicked.connect(lambda:self.btn_slot())
self.copy_btn.clicked.connect(lambda:self.btn_slot())
self.start_btn.clicked.connect(self.start_slot)
def start_slot(self):
'''
開始按鍵的槽函數(shù)
'''
self.root_btn.setEnabled(False)
self.copy_btn.setEnabled(False)
self.start_btn.setEnabled(False)
self.dir_file() # 遍歷指定文件夾下的文件并添加到self.file_list集合中
self.len_file() # 獲取文件夾下文件數(shù)量
self.copy_file() # 開始拷貝文件
self.log.append('拷貝成功!')
self.finish_sound.play() # 播放完成后的提示音
def btn_slot(self):
'''
上面兩個(gè)按鍵的槽函數(shù)
'''
btn = self.sender()
if btn == self.root_btn:
directory = QFileDialog.getExistingDirectory(None,"選取文件夾","C:/")
if directory:
self.root_text.setText(directory)
self.root_path = directory
self.log.append('選擇文件成功!')
elif btn == self.copy_btn:
directory = QFileDialog.getExistingDirectory(None,"選取拷貝位置","C:/")
if directory:
self.copy_text.setText(directory)
self.copy_path = directory
self.log.append('選取拷貝位置成功!')
成果展示



到此這篇關(guān)于Python PyQt5實(shí)戰(zhàn)項(xiàng)目之文件拷貝器的具體實(shí)現(xiàn)詳解的文章就介紹到這了,更多相關(guān)Python 文件拷貝器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
學(xué)python需要去培訓(xùn)機(jī)構(gòu)嗎
在本篇文章里小編給大家整理的是關(guān)于學(xué)python是否需要去培訓(xùn)機(jī)構(gòu)的相關(guān)內(nèi)容,有需要的朋友們可以閱讀下。2020-07-07
pycharm中使用request和Pytest進(jìn)行接口測(cè)試的方法
這篇文章主要介紹了pycharm中使用request和Pytest進(jìn)行接口測(cè)試的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
python實(shí)現(xiàn)大學(xué)人員管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)大學(xué)人員管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
Python+pyaudio實(shí)現(xiàn)音頻控制示例詳解
PyAudio?是語音處理的?Python?庫,提供了比較豐富的功能。本文將利用pyaudio控制指定設(shè)備,實(shí)現(xiàn)錄制音頻、采集音頻流、播放音頻,感興趣的可以了解一下2022-07-07
Python3和PyCharm安裝與環(huán)境配置【圖文教程】
這篇文章主要介紹了Python3和PyCharm安裝與環(huán)境配置,結(jié)合圖文形式詳細(xì)分析了Python3和PyCharm的安裝、環(huán)境配置、測(cè)試命令及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-02-02
python實(shí)戰(zhàn)游戲之史上最難最虐的掃雷游戲沒有之一
這篇文章主要介紹了使用 python 實(shí)現(xiàn)掃雷游戲,不同于傳統(tǒng)過時(shí)的掃雷,今天我們用 Python 增加了新花樣,文中給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
python中matplotlib的用法及繪制簡(jiǎn)單圖形詳解
這篇文章主要給大家介紹了關(guān)于python中matplotlib的用法及繪制簡(jiǎn)單圖形的相關(guān)資料,matplotlib是python中用于繪制各種圖像的模塊,功能十分強(qiáng)大,通常與pandas模塊搭配使用,可以生成各種樣視的圖片,用于數(shù)據(jù)的分析和展示,需要的朋友可以參考下2024-03-03

