How to adapt my current splash screen to allow other pieces of my code to run in the background?

2024/11/16 1:50:59

Currently I have a splash screen in place. However, it does not work as a real splash screen - as it halts the execution of the rest of the code (instead of allowing them to run in the background).

This is the current (reduced) arquitecture of my program, with the important bits displayed in full. How can I adapt the splash screen currently in place to actually allow the rest of the program to load in the background? Is it possible in python?

Thanks!

import ...
(many other imports)
def ...
def ...
(many other definitions)class VFrams(wxFrame):wx.Frame.__init__(self, parent, -1, _("Software"), size=(1024, 768), style=wx.DEFAULT_FRAME_STYLE)(a lot of code goes in here)class MySplashScreen(wx.SplashScreen):def __init__(self, parent=None):aBitmap = wx.Image(name=VarFiles["img_splash"]).ConvertToBitmap()splashStyle = wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUTsplashDuration = 5000 # mswx.SplashScreen.__init__(self, aBitmap, splashStyle, splashDuration, parent)self.Bind(wx.EVT_CLOSE, self.CloseSplash)wx.Yield()def CloseSplash(self, evt):self.Hide()global frameframe = VFrame(parent=None)app.SetTopWindow(frame)frame.Show(True)evt.Skip()class MyApp(wx.App):def OnInit(self):MySplash = MySplashScreen()MySplash.Show()return Trueif __name__ == '__main__':DEBUG = viz.addText('DEBUG:', viz.SCREEN)DEBUG.setPosition(0, 0)DEBUG.fontSize(16)DEBUG.color(viz.BLACK)Start_Mainvars()        Start_Config()Start_Translation()Start_DB()Start_Themes()Start_Gui()Start_Get_Isos()Start_Bars()Start_Menus()Start_Event_Handlers()app = MyApp()app.MainLoop()

Thank you for all the help, this is how I changed the code (following the provided advice):

def show_splash():# create, show and return the splash screenglobal splashbitmap = wx.Image(name=VarFiles["img_splash"]).ConvertToBitmap()splash = wx.SplashScreen(bitmap, wx.SPLASH_CENTRE_ON_SCREEN|wx.SPLASH_NO_TIMEOUT, 0, None, -1)splash.Show()return splashclass MyApp(wx.App):def OnInit(self):global frame, splashsplash = show_splash()Start_Config()Start_Translation()Start_DB()Start_Themes()Start_Gui()Start_Get_Isos()Start_Bars("GDP1POP1_20091224_gdp", "1 pork")Start_Menus()Start_Event_Handlers()frame = VFrame(parent=None)frame.Show(True)splash.Destroy()return Trueif __name__ == '__main__':DEBUG = viz.addText('DEBUG:', viz.SCREEN)DEBUG.setPosition(0, 0)DEBUG.fontSize(16)DEBUG.color(viz.BLACK)Start_Mainvars()   app = MyApp()app.MainLoop()
Answer

Your code is pretty messy/complicated. There's no need to override wx.SplashScreen and no reason your splash screen close event should be creating the main application window. Here's how I do splash screens.

import wxdef show_splash():# create, show and return the splash screenbitmap = wx.Bitmap('images/splash.png')splash = wx.SplashScreen(bitmap, wx.SPLASH_CENTRE_ON_SCREEN|wx.SPLASH_NO_TIMEOUT, 0, None, -1)splash.Show()return splashdef main():app = wx.PySimpleApp()splash = show_splash()# do processing/initialization here and create main windowframe = MyFrame(...)frame.Show()splash.Destroy()app.MainLoop()if __name__ == '__main__':main()

Just create the splash screen as soon as possible with no timeout. Continue loading and create your application's main window. Then destroy the splash screen so it goes away. Showing the splash screen doesn't stop other processing from happening.

https://en.xdnf.cn/q/71724.html

Related Q&A

Reversed array slice including the first element [duplicate]

This question already has answers here:Python reverse-stride slicing(8 answers)Closed 5 years ago.Lets say I have:>>> a = [1, 2, 3, 4]And I want to get a reversed slice. Lets say I want the 1s…

Problem using py2app with the lxml package

I am trying to use py2app to generate a standalone application from some Python scripts. The Python uses the lxml package, and Ive found that I have to specify this explicitly in the setup.py file that…

How to run TensorFlow on AMD/ATI GPU?

After reading this tutorial https://www.tensorflow.org/guide/using_gpu I checked GPU session on this simple code import numpy as np import matplotlib.pyplot as plt import tensorflow as tfa = tf.constan…

Pure virtual function call

Im using boost.python to make python-modules written in c++. I have some base class with pure virtual functions which I have exported like this:class Base {virtual int getPosition() = 0; };boost::pytho…

Expected String or Unicode when reading JSON with Pandas

I try to read an Openstreetmaps API output JSON string, which is valid.I am using following code:import pandas as pd import requests# Links unten minLat = 50.9549 minLon = 13.55232# Rechts oben maxLat …

How to convert string labels to one-hot vectors in TensorFlow?

Im new to TensorFlow and would like to read a comma separated values (csv) file, containing 2 columns, column 1 the index, and column 2 a label string. I have the following code which reads lines in th…

Pandas dataframe boolean mask on multiple columns

I have a dataframe (df) containing several columns with an actual measure and corresponding number of columns (A,B,...) with an uncertainty (dA, dB, ...) for each of these columns:A B dA dB …

Which of these scripting languages is more appropriate for pen-testing? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

Keras: Optimal epoch selection

Im trying to write some logic that selects the best epoch to run a neural network in Keras. My code saves the training loss and the test loss for a set number of epochs and then picks the best fitting …

error in loading pickle

Not able to load a pickle file. I am using python 3.5import pickle data=pickle.load(open("D:\\ud120-projects\\final_project\\final_project_dataset.pkl", "r"))TypeError: a bytes-lik…