python - PyQt5 - Why is progress bar not showing a file copy progress? -


i trying copy file progress bar included in pyqt5. can see in console print("copied: {}".format(percent)) 100% copied.

however, progress bar not update copy process. how can check signal sent? did make mistake?

import os, sys pyqt5 import qtwidgets, qtcore  class extended(qtcore.qthread):     def __init__(self):         super().__init__()         self.src_file = 'test.txt'         self.dst_file = 'test_copy.txt'         self.copyfileobj(self.src_file, self.dst_file, self.cb)      copied_percent = qtcore.pyqtsignal(int)      def cb(self, temp_file_size):         self.file_size = os.stat(self.src_file).st_size         percent = int(temp_file_size/self.file_size*100)         print("copied: {}".format(percent))         self.copied_percent.emit(percent)      def copyfileobj(self, fsrc, fdst, callback, length=16*1024):         copied = 0         open(fsrc, "rb") fr, open(fdst, "wb") fw:             while true:                 buff = fr.read(length)                 if not buff:                     break                 fw.write(buff)                 copied += len(buff)                 callback(copied)   class myapp(qtwidgets.qwidget):      def __init__(self, parent=none):         super().__init__(parent)          self.w = qtwidgets.qwidget()         self.w.setwindowtitle("progress bar copy test")          # add widgets on window         self.copy_button = qtwidgets.qpushbutton("copy", self)         self.copy_button.sizehint()         self.progress_bar = qtwidgets.qprogressbar(self)         self.progress_bar.setgeometry(0, 40, 300, 25)         self.progress_bar.setmaximum(100)          self.copy_button.clicked.connect(self.on_button_click)      def on_button_click(self):         self.copy_button.setdisabled(true)         self.ext = extended()         self.ext.copied_percent.connect(self.on_count_change)         self.ext.start()      def on_count_change(self, value):         self.progress_bar.setvalue(value)   if __name__ == '__main__':     app = qtwidgets.qapplication(sys.argv)     prog = myapp()     prog.show()     sys.exit(app.exec_()) 

ok, figured out. there has run method in thread-supported class executed when call start() method on instance object self.ext. added self.ext.join() method not end program before thread executed , finished.

import os, sys pyqt5 import qtwidgets, qtcore   class extended(qtcore.qthread):     """     running copy operation     """     copied_percent_signal= qtcore.pyqtsignal(int)      def __init__(self):         super().__init__()         self.src_file = 'test.txt'         self.dst_file = 'test_copy.txt'         self.file_size = os.stat(self.src_file).st_size      def run(self):         self.copyfileobj(self.src_file, self.dst_file, self.my_callback)      def my_callback(self, temp_file_size):         percent = int(temp_file_size/self.file_size*100)         print("copiedd: {}".format(percent))         self.copied_percent_signal.emit(percent)      def copyfileobj(self, fsrc, fdst, callback, length=16*1024):         copied = 0         open(fsrc, "rb") fr, open(fdst, "wb") fw:             while true:                 buff = fr.read(length)                 if not buff:                     break                 fw.write(buff)                 copied += len(buff)                 callback(copied)   class myapp(qtwidgets.qwidget):      def __init__(self, parent=none):         super().__init__(parent)          # instance attribute defined later in on_button_click()         self.ext = none          self.w = qtwidgets.qwidget()         self.w.setwindowtitle("progress bar copy test")          # add widgets on window         self.copy_button = qtwidgets.qpushbutton("copy", self)         self.copy_button.sizehint()         self.progress_bar = qtwidgets.qprogressbar(self)         self.progress_bar.setgeometry(0, 40, 300, 25)         self.progress_bar.setmaximum(100)          self.copy_button.clicked.connect(self.on_button_click)      def on_button_click(self):         self.copy_button.setdisabled(true)         self.ext = extended()         self.ext.copied_percent_signal.connect(self.on_count_change)         self.ext.start()         self.ext.join()      def on_count_change(self, value):         self.progress_bar.setvalue(value)   if __name__ == '__main__':     app = qtwidgets.qapplication(sys.argv)     prog = myapp()     prog.show()     sys.exit(app.exec_()) 

my file being copied , progress bar working.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -