0


Hello every one. I am making a desktop GUI application using PyQt5 on Windows 7.

In my application, there is a heavy workload. It is making a hash code of all files in specified directories and downloads from ftp if the files hash code is not equal with ftps file. On my system this takes about 5-6 minutes.

In my thought...

  1. show widget first. ( of course, the 'self.show()' is prior to code of compare hash. )

  2. compare hash code and download the files.

Code works properly, but widget show after 2. ends. And this is a problem.
I want to my application runs step 1 first then step 2. What should I do?

Any help is apreciated

Code:

class showUI(QWidget):
  # make menu, toolbar, buttons, etc..
  self.show()

class compareHashAndDownload:
  # compare hash and download the files.

if __name__ == '__main__':
  app = QApplication(sys.argv)
  ex = showUI()
  mainProcess = compareHashAndDownload()
  sys.exit(app.exec_())
JDurstberger
  • 4,127
  • 8
  • 31
  • 68
passion053
  • 473
  • 3
  • 8
  • 21

2 Answers2

1

The best way to fix this is to run the compareHashAndDownload in a separate QThread. This would also allow the widget to be interacted with, even before the long running function has completed. You could also re-run the function later and not hang up the GUI in that time.

Using threads is quite complex. For example, you will need to arrange the GUI actions so they operate correctly when called before, during, or after the execution of the compareHashAndDownload function.

There are some nice examples here which will help you if you decide to go down this route.

However, a cheap and cheerful alternative would be to show a splash screen while you perform the compareHashAndDownload. Here's a simple example:

class showUI(QWidget):
  splash = QtGui.QSplashScreen(QtGui.QPixmap(splashFileName))
  splash.show()
  # make menu, toolbar, buttons, etc..
  mainProcess = compareHashAndDownload()
  self.show()
  splash.finish(self)

class compareHashAndDownload:
  # compare hash and download the files.

if __name__ == '__main__':
  app = QApplication(sys.argv)
  ex = showUI()

  sys.exit(app.exec_())
Community
  • 1
  • 1
strubbly
  • 3,347
  • 3
  • 24
  • 36
0

you can directly call compareHashAndDownload from the showUI widget itself. But if your compare method takes too long then the ui will wait from it to complete and thats not a nice experience.So keep that in mind.

class showUI(QWidget):
  # make menu, toolbar, buttons, etc..
  mainProcess = compareHashAndDownload()
  self.show()

class compareHashAndDownload:
  # compare hash and download the files.

if __name__ == '__main__':
  app = QApplication(sys.argv)
  ex = showUI()

  sys.exit(app.exec_())

this will work fine is the method doesnt take that long, if it does then use qt's own QThread to run the method on seperate thread (not the most elegant solution but will work fine) .

GIRISH RAMNANI
  • 614
  • 6
  • 18
  • 1
    As I mentioned, My code has heavy workload(5, 6minutes). So your solution makes no difference. I need the way to completely show the ui first. Your code result same as I write.... – passion053 Nov 30 '15 at 10:01