Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过新线程在Python中打开wx.Frame_Python_Multithreading_Wxpython - Fatal编程技术网

通过新线程在Python中打开wx.Frame

通过新线程在Python中打开wx.Frame,python,multithreading,wxpython,Python,Multithreading,Wxpython,我有一个框架,作为启动屏幕存在,供用户在主程序启动前进行选择。在用户做出选择后,我需要屏幕作为一种启动屏幕,直到主程序在后台完成加载 我通过创建应用程序并启动线程来实现这一点: class App(wx.App): ''' Creates the main frame and displays it Returns true if successful ''' def OnInit(self): try: '''

我有一个框架,作为启动屏幕存在,供用户在主程序启动前进行选择。在用户做出选择后,我需要屏幕作为一种启动屏幕,直到主程序在后台完成加载

我通过创建应用程序并启动线程来实现这一点:

class App(wx.App):
    '''
    Creates the main frame and displays it
    Returns true if successful
    '''
    def OnInit(self):
        try:
            '''
            Initialization
            '''
            self.newFile = False
            self.fileName = ""

            self.splashThread = Splash.SplashThread(logging, self)
            self.splashThread.start()
            #...More to the class
它会启动一个框架:

class SplashThread(threading.Thread):
    def __init__(self, logger, app):
        threading.Thread.__init__(self)
        self.logger = logger
        self.app = app

    def run(self):
        frame = Frame(self.logger, self.app)
        frame.Show()
应用程序值是必需的,因为它包含回调,当用户做出选择时,允许主程序继续。问题是启动屏幕只会闪烁一毫秒,然后消失,不允许用户进行选择并阻止其余的启动


有什么想法吗?提前谢谢

这不需要线程。缺点是,启动窗口在加载时会被阻塞,但只有当您希望更新其内容或设置动画或希望能够拖动时,这才是一个问题。例如,可以通过定期调用wx.SafeYield来解决的问题

import time
import wx


class Loader(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)
        self.btn1 = wx.Button(self, label="Option 1")
        self.btn2 = wx.Button(self, label="Option 2")
        sizer.Add(self.btn1, flag=wx.EXPAND)
        sizer.Add(self.btn2, flag=wx.EXPAND)
        self.btn1.Bind(wx.EVT_BUTTON, self.OnOption1)
        self.btn2.Bind(
            wx.EVT_BUTTON, lambda e: wx.MessageBox("There is no option 2")
        )

    def OnOption1(self, event):
        self.btn1.Hide()
        self.btn2.Hide()
        self.Sizer.Add(
            wx.StaticText(self, label="Loading Option 1..."),
            1, wx.ALL | wx.EXPAND, 15
        )
        self.Layout()
        self.Update()
        AppFrame(self).Show()

class AppFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        time.sleep(3)
        parent.Hide()

        # the top window (Loader) is hidden so the app needs to be told to exit
        # when this window is closed
        self.Bind(wx.EVT_CLOSE, lambda e: wx.GetApp().ExitMainLoop())


app = wx.PySimpleApp()
app.TopWindow = Loader()
app.TopWindow.Show()
app.MainLoop()

完美-在我尝试穿线之前,我曾想过类似的事情,但我就是不能把所有的部分放在一起。。。谢谢