I write a small GUI to move files from one directory to another, my idea is using QTextEdit to show the progress, when one file moved, then write this file name in QTextEdit, and so on till all files are moved. I tried like below:
from PyQt5 import QtGui
from PyQt5.QtWidgets import QStyleFactory, QApplication, QWidget, QListWidget, QLabel, QVBoxLayout, QHBoxLayout, QPushButton
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit, QFileDialog,
QTextEdit, QGridLayout, QApplication, QMessageBox, QProgressBar)
from PyQt5.QtCore import Qt, QThread, pyqtSignal
import sys, os, time
class Window(QWidget):
def __init__(self):
super().__init__()
self.title = "test"
self.top = 200
self.left = 500
self.width = 400
self.height = 300
self.InitWindow()
def InitWindow(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# main layout
self.vbox = QVBoxLayout()
# for src path
self.hbox1 = QHBoxLayout()
self.label1 = QLabel('Source ')
self.src = QLineEdit()
self.path1 = self.src.text()
self.btnSrc = QPushButton('...')
self.btnSrc.clicked.connect(lambda: self.getSrc())
self.hbox1.addWidget(self.label1)
self.hbox1.addWidget(self.src)
self.hbox1.addWidget(self.btnSrc)
# for des path
self.hbox2 = QHBoxLayout()
self.label2 = QLabel('Destination')
self.des = QLineEdit()
self.path2 = self.des.text()
self.btnDes = QPushButton('...')
self.btnDes.clicked.connect(lambda: self.getDes())
self.hbox2.addWidget(self.label2)
self.hbox2.addWidget(self.des)
self.hbox2.addWidget(self.btnDes)
self.statusBox = QTextEdit()
self.statusBox.setReadOnly(True)
self.btnStart = QPushButton('Start')
self.btnStart.clicked.connect(self.startMov)
self.vbox.addLayout(self.hbox1)
self.vbox.addLayout(self.hbox2)
self.vbox.addWidget(self.statusBox)
self.vbox.addWidget(self.btnStart)
self.setLayout(self.vbox)
self.show()
def getSrc(self):
try:
self.path1 = QFileDialog.getExistingDirectory(self)
self.src.setText(self.path1)
#all file in this path
self.filenames = os.listdir(self.path1)
self.statusBox.setPlainText(str(len(self.filenames)) + ' files found in this folder:')
self.statusBox.append('')
for i, sample in enumerate(self.filenames):
self.statusBox.append(str(i+1) + '. ' + sample)
except:
pass
def getDes(self):
try:
self.path2 = QFileDialog.getExistingDirectory(self)
self.des.setText(self.path2)
except:
pass
def startMov(self):
buttonReply = QMessageBox.question(self, 'wait...', "Do you want to move all this files?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if buttonReply == QMessageBox.Yes:
for file in self.filenames:
blueText = "<span style=\" font-size:8pt; font-weight:600; color:#0047b3;\" >moving: </span>"
greenText = "<span style=\" font-size:8pt; font-weight:600; color:#009933;\" >done! </span>"
self.statusBox.append(blueText)
os.replace(self.path1 + '\\' + file, self.path2 + '\\' + file)
self.statusBox.append(file)
self.statusBox.append(greenText)
else:
print('No clicked.')
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
but it only show text in the QTextEdit after all files are moved and I can not see the progress at all.
why is it so and how can I fix it?
Edit: I want the order as below:
# 1. before one file is moved --> in QLineEdit should be: moving
self.statusBox.append(blueText)
# 2. file should be moved from src to des
os.replace(self.path1 + '\\' + file, self.path2 + '\\' + file)
# 3. name of file stand in QLineEdit
self.statusBox.append(file)
# 4. then 'done' stand in QLineEdit
self.statusBox.append(greenText)
but the order now is: 1 2 4 3 after all text was there then the files are moved