6

Attualmente ho una schermata iniziale in posizione. Tuttavia, non funziona come una vera schermata iniziale, in quanto interrompe l'esecuzione del resto del codice (anziché consentirne l'esecuzione in background).Come adattare la mia schermata iniziale attuale per consentire l'esecuzione di altre parti del mio codice in background?

Questa è l'arquitecture corrente (ridotta) del mio programma, con i bit importanti visualizzati per intero. Come posso adattare lo splash screen attualmente presente per consentire effettivamente il resto del programma di caricare in background? È possibile in Python?

Grazie!

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_TIMEOUT 
     splashDuration = 5000 # ms 
     wx.SplashScreen.__init__(self, aBitmap, splashStyle, splashDuration, parent) 
     self.Bind(wx.EVT_CLOSE, self.CloseSplash) 
     wx.Yield() 
    def CloseSplash(self, evt): 
     self.Hide() 
     global frame 
     frame = VFrame(parent=None) 
     app.SetTopWindow(frame) 
     frame.Show(True) 
     evt.Skip() 

class MyApp(wx.App): 
    def OnInit(self): 
     MySplash = MySplashScreen() 
     MySplash.Show() 
     return True 

if __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() 

Grazie per tutto l'aiuto, questo è come ho cambiato il codice (in seguito alla consulenza fornita):

def show_splash(): 
    # create, show and return the splash screen 
    global splash 
    bitmap = 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 splash 

class MyApp(wx.App): 
    def OnInit(self): 
     global frame, splash 
     splash = 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 True 

if __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() 

risposta

12

Il tuo codice è piuttosto complicato/complicato. Non è necessario eseguire l'override di wx.SplashScreen e nessun motivo per cui l'evento di chiusura della schermata iniziale dovrebbe creare la finestra principale dell'applicazione. Ecco come faccio gli splash screen.

import wx 

def show_splash(): 
    # create, show and return the splash screen 
    bitmap = wx.Bitmap('images/splash.png') 
    splash = wx.SplashScreen(bitmap, wx.SPLASH_CENTRE_ON_SCREEN|wx.SPLASH_NO_TIMEOUT, 0, None, -1) 
    splash.Show() 
    return splash 

def main(): 
    app = wx.PySimpleApp() 
    splash = show_splash() 

    # do processing/initialization here and create main window 
    frame = MyFrame(...) 
    frame.Show() 

    splash.Destroy() 
    app.MainLoop() 

if __name__ == '__main__': 
    main() 

Basta creare lo splash screen il prima possibile senza timeout. Continua a caricare e creare la finestra principale dell'applicazione. Quindi distruggi lo splash screen in modo che vada via. La visualizzazione della schermata iniziale non impedisce l'elaborazione di altri processi.

+0

Grazie mille. Sto provando questo. – relima

0

Ti consigliamo di utilizzare due thread: uno per la schermata iniziale, uno per qualsiasi altro codice che si desidera eseguire. Entrambi i thread funzionerebbero allo stesso tempo, fornendo il risultato desiderato.

+0

C'è un problema: non ho mai usato i thread. Ho cercato di farlo funzionare, ma non ho idea di come farlo funzionare. Mi aspettavo di imparare dalle risposte a questa domanda. – relima

+0

Scopri alcuni articoli online sull'argomento. Ho trovato questo che può aiutare: http://www.devshed.com/c/a/Python/Basic-Threading-in-Python – Bernard

+0

Nessun thread sono necessari qui. Lo splash screen non blocca l'esecuzione se usato correttamente. – FogleBird