人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

Python PyQt5實(shí)戰(zhàn)項(xiàng)目之文件拷貝器的具體實(shí)現(xiàn)詳解

發(fā)布日期:2021-12-13 02:31 | 文章來源:源碼中國

簡介

寫了一個(gè)簡單的文件夾內(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)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部