Python PyQt5實戰(zhàn)項目之文件拷貝器的具體實現(xiàn)詳解
簡介
寫了一個簡單的文件夾內(nèi)容下所有文件復制到另一個文件夾內(nèi),主要邏輯代碼是來自《2小時玩轉(zhuǎn)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文件轉(zhuǎn)成py文件,再引入這個py文件,這樣就可以使用了。在使用這個音頻只需要在路徑上加一個 : ,就如這樣self.finish_sound = QSound(':resource/finish.wav') # 設置提示音
- qrc文件轉(zhuǎn)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文件轉(zhuǎn)成.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 文件拷貝器內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持本站!
版權聲明:本站文章來源標注為YINGSOO的內(nèi)容版權均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。
關注官方微信