Python PyQt5實戰(zhàn)項目之文件拷貝器的具體實現(xiàn)詳解
簡介
寫了一個簡單的文件夾內容下所有文件復制到另一個文件夾內,主要邏輯代碼是來自《2小時玩轉python多線程編程》中的一個章節(jié)。
UI設置
def ui_init(self):
'''
界面的函數(shù)
'''
self.setWindowTitle('拷貝器')
self.resize(600,400)
self.setMinimumSize(600,400) # 設置窗口的最小值
'''控件'''
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') # 設置提示音
'''控件設置'''
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)
這次加入了一個完成的音效
- QSound解析文件時,可能會出現(xiàn)這問題
QSoundEffect(qaudio): Error decoding source
self.finish_sound = QSound('resource/finish.wav') # 設置提示音 原來這這樣寫的,但會出現(xiàn)上面的問題,就在寫一個qrc文件,再將qrc文件轉成py文件,再引入這個py文件,這樣就可以使用了。在使用這個音頻只需要在路徑上加一個 : ,就如這樣self.finish_sound = QSound(':resource/finish.wav') # 設置提示音
- qrc文件轉py文件
先新建一個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文件轉成.py文件。
主要邏輯
def variates_init(self):
'''
儲存變量的函數(shù)
'''
self.root_path = '' # 要拷貝的路徑
self.copy_path = '' # 要拷貝到的路徑
self.file_list = [] # 文件名集合
self.len = 0 # 文件夾下文件數(shù)量
def copy_file(self):
'''
拷貝文件的函數(shù)
'''
count = 0 # 臨時設置進度條數(shù)值
self.progerss.setRange(0,self.len) # 設置進度條的數(shù)值
self.progerss.setValue(0) # 設置進度條初始值
'''拷貝器主邏輯'''
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ù)拷貝到的路徑新建一個文件寫入
- 拷貝的文件每次寫入1024字節(jié),當沒有數(shù)據(jù)后,就結束寫入并保存文件,進度條數(shù)值加1
信號與槽
def connect_init(self):
'''
信號與槽連接的函數(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):
'''
上面兩個按鍵的槽函數(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('選取拷貝位置成功!')
成果展示



到此這篇關于Python PyQt5實戰(zhàn)項目之文件拷貝器的具體實現(xiàn)詳解的文章就介紹到這了,更多相關Python 文件拷貝器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
pycharm中使用request和Pytest進行接口測試的方法
這篇文章主要介紹了pycharm中使用request和Pytest進行接口測試的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Python+pyaudio實現(xiàn)音頻控制示例詳解
PyAudio?是語音處理的?Python?庫,提供了比較豐富的功能。本文將利用pyaudio控制指定設備,實現(xiàn)錄制音頻、采集音頻流、播放音頻,感興趣的可以了解一下2022-07-07
Python3和PyCharm安裝與環(huán)境配置【圖文教程】
這篇文章主要介紹了Python3和PyCharm安裝與環(huán)境配置,結合圖文形式詳細分析了Python3和PyCharm的安裝、環(huán)境配置、測試命令及相關操作注意事項,需要的朋友可以參考下2020-02-02
python實戰(zhàn)游戲之史上最難最虐的掃雷游戲沒有之一
這篇文章主要介紹了使用 python 實現(xiàn)掃雷游戲,不同于傳統(tǒng)過時的掃雷,今天我們用 Python 增加了新花樣,文中給大家介紹的非常詳細,需要的朋友可以參考下2021-09-09

