【发布时间】:2021-04-07 13:17:41
【问题描述】:
我写了一个小 GUI 来将文件从一个目录移动到另一个目录,我的想法是使用 QTextEdit 来显示进度,当一个文件移动时,然后在 QTextEdit 中写入这个文件名,依此类推,直到所有文件都被移动。我尝试如下:
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())
但它只在所有文件移动后在 QTextEdit 中显示文本,我根本看不到进度。
为什么会这样,我该如何解决?
编辑: 我想要的顺序如下:
# 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)
但现在的顺序是:1 2 4 3 在所有文本都存在之后,文件被移动
【问题讨论】:
-
我已经添加了我的代码