Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement file copier with Python PyQt5

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to implement the document copier in Python PyQt5. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

UI sets def ui_init (self):''function of the interface' 'self.setWindowTitle (' copier') self.resize (600400) self.setMinimumSize (600400) # sets the minimum value of the window''Control' 'self.root_btn = QPushButton () self.copy_btn = QPushButton () Self.start_btn = QPushButton () self.root_text = QTextBrowser () self.copy_text = QTextBrowser () self.log = QTextBrowser () self.h2_layout = QHBoxLayout () self.h3_layout = QHBoxLayout () self.h4_layout = QHBoxLayout () self.v_layout = QVBoxLayout () self.progerss = QProgressBar () self.finish_sound = QSound (': resource/finish.wav') # set tone''Control Settings' 'self.root_btn.setText (' Select a folder') self.root_btn.setFixedSize (150 Magazine 30) self.copy_btn.setText ('Select copy path') self.copy_btn.setFixedSize (150 no. 30) self.start_btn.setText ('start') Self.start_btn.setFixedSize (50 self.root_text 30) self.root_text.setFixedHeight (27) self.copy_text.setFixedHeight (27) self.progerss.setValue (0)''self.h2_layout.addWidget (self.root_text) self.h2_layout.addWidget (self.root_btn) self.h3_layout.addWidget (self.copy_text) Self.h3_layout.addWidget (self.copy_btn) self.h4_layout.addWidget (self.progerss) self.h4_layout.addWidget (self.start_btn) self.v_layout.addLayout (self.h2_layout) self.v_layout.addLayout (self.h3_layout) self.v_layout.addWidget (self.log) self.v_layout.addLayout (self.h4_ Layout) self.setLayout (self.v_layout)

This time a completed sound effect was added.

This problem may occur when QSound parses files QSoundEffect (qaudio): Error decoding source

Self.finish_sound = QSound ('resource/finish.wav') # setting prompt tone is originally written like this, but there will be the above problem, just write a qrc file, then convert the qrc file into a py file, and then introduce the py file, so it can be used. To use this audio, you only need to add a:, like this, self.finish_sound = QSound (': resource/finish.wav') # to set the tone.

Qrc file to py file

First create a new txt file and write a statement like this to it:

Resource/finish.wav

Resource/ is the name of the folder where audio is played.

Finish.wav is the audio name

Resource/finish.wav is the full audio path

Then change the file suffix to qrc, type pyrcc5-o resource.qrc resource.py in the cmd command window, and convert the .qrc file to a .py file.

Main logic def variates_init (self):''function to store variables' self.root_path =''# path to copy self.copy_path =''# path to copy self.file_list = [] # filename collection self.len = 0 # the number of files under the folder def Copy_file (self):''function to copy files' 'count = 0 # temporarily set the progress bar value self.progerss.setRange (0 Self.len) # set the numeric value of the progress bar self.progerss.setValue (0) # set the initial value of the progress bar''copier main logic' '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):''function of traversing directories' filelist = os.listdir (self.root_path) self.file_list = filelist def len_file (self):''function of the number of files '' self.len=len (self.file_list)

The logic of the copier:

Get the file name from the file name collection

Merge the original file path and the copied path

Open the file mode as read-only according to the original file path, and create a new file write based on the path copied to

Each time the copied file is written to 1024 bytes, when there is no data, the file is written and saved, and the progress bar value is added by 1.

Signal and slot def connect_init (self):''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): Self.root_btn.setEnabled (False) self.copy_btn.setEnabled (False) self.start_btn.setEnabled (False) self.dir_file () # iterate through the files under the specified folder and add them to the self.file_list collection self.len_file () # to get the files Clip the number of files self.copy_file () # start copying the file self.log.append ('copy successful!') Self.finish_sound.play () # tone def btn_slot (self) after playback:''slot function of the above two keys' 'btn = self.sender () if btn = = self.root_btn: directory = QFileDialog.getExistingDirectory (None, "Select folder" "if directory /") if directory: self.root_text.setText (directory) self.root_path = directory self.log.append ('file selected successfully!') Elif btn = = self.copy_btn: directory = QFileDialog.getExistingDirectory (None, "Select copy location", "None /") if directory: self.copy_text.setText (directory) self.copy_path = directory self.log.append ('copy location selected successfully!') Achievement presentation

This is the end of the article on "how to implement the document copier in Python PyQt5". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report