loading screen using a gif image
-
wrote on 26 Sept 2022, 14:48 last edited by
Hi
I'm trying to show a gif loading image when the user switches between two pages but it seems unsuccessful and causes the app to exit. Below is the code I tried, please I need assistance. Thanks in advance.self.ui.leftMenuSerch_btn.clicked.connect(self.leftMenu_search_button) def leftMenu_search_button(self): self.loadingScreen() self.show() self.ui.stackedWidget.setCurrentWidget(self.ui.search_page) def loadingScreen(self): self.setFixedSize(150,150) self.setWindowFlags(Qt.WindowsStayOnTopHint| Qt.CustomizeWindowHint) self.label_animation = QLabel(self) self.movie = QMovie('file_loading_gif2') self.label_animation.setMovie(self.movie) timer = QTimer(self) self.startAnimation() timer.singleShot(3000,self.stopAnimation) self.show() def startAnimation(self): self.movie.start() def stopAnimation(self): self.movie.stop() self.close()
-
Hi
I'm trying to show a gif loading image when the user switches between two pages but it seems unsuccessful and causes the app to exit. Below is the code I tried, please I need assistance. Thanks in advance.self.ui.leftMenuSerch_btn.clicked.connect(self.leftMenu_search_button) def leftMenu_search_button(self): self.loadingScreen() self.show() self.ui.stackedWidget.setCurrentWidget(self.ui.search_page) def loadingScreen(self): self.setFixedSize(150,150) self.setWindowFlags(Qt.WindowsStayOnTopHint| Qt.CustomizeWindowHint) self.label_animation = QLabel(self) self.movie = QMovie('file_loading_gif2') self.label_animation.setMovie(self.movie) timer = QTimer(self) self.startAnimation() timer.singleShot(3000,self.stopAnimation) self.show() def startAnimation(self): self.movie.start() def stopAnimation(self): self.movie.stop() self.close()
wrote on 26 Sept 2022, 14:59 last edited by JonB@LT-K101 said in loading screen using a gif image:
causes the app to exit.
Maybe
self.close()
instopAnimation()
does that? Depends whatself
is and what top-level windows/widgets are currently open. You can also step through your own code in a Python debugger.You should also put error checking into your code, e.g. how do you/we know whether
QMovie('file_loading_gif2')
even finds the file?In terms of your own code, the following:
def loadingScreen(self):: ... timer = QTimer(self) self.startAnimation() timer.singleShot(3000,self.stopAnimation) self.show()
looks likely not to achieve anything, since
timer
is just a local variable and goes out of scope at the end of the function, thereby destroying the timer in Python. Even though you specifiedQTimer(self)
, so far as I know, you might verify. -
@LT-K101 said in loading screen using a gif image:
causes the app to exit.
Maybe
self.close()
instopAnimation()
does that? Depends whatself
is and what top-level windows/widgets are currently open. You can also step through your own code in a Python debugger.You should also put error checking into your code, e.g. how do you/we know whether
QMovie('file_loading_gif2')
even finds the file?In terms of your own code, the following:
def loadingScreen(self):: ... timer = QTimer(self) self.startAnimation() timer.singleShot(3000,self.stopAnimation) self.show()
looks likely not to achieve anything, since
timer
is just a local variable and goes out of scope at the end of the function, thereby destroying the timer in Python. Even though you specifiedQTimer(self)
, so far as I know, you might verify. -
wrote on 26 Sept 2022, 18:12 last edited by JonB
@SGaist said in loading screen using a gif image:
@JonB singleShot is a static method so it should not be influenced by the fact that the timer variable is local but it's not used in the correct way.
Damn, you're right, and this has come up before, and I forgot again. I wish neither Python nor C++ allowed calling statics from an instance, C# never allowed that!
-
@SGaist said in loading screen using a gif image:
@JonB singleShot is a static method so it should not be influenced by the fact that the timer variable is local but it's not used in the correct way.
Damn, you're right, and this has come up before, and I forgot again. I wish neither Python nor C++ allowed calling statics from an instance, C# never allowed that!
@JonB said in loading screen using a gif image:
@SGaist said in loading screen using a gif image:
@JonB singleShot is a static method so it should not be influenced by the fact that the timer variable is local but it's not used in the correct way.
Damn, you're right, and this has come up before, and I forgot again. I wish neither Python nor C++ allowed calling statics from an instance, C# never allowed that!
C# by iso approval time is not even allowed to drink yet. C++ and Python have at least a decade more behind them ;-)
1/5