wxPython class 未显示在 window 中

wxPython class doesnt show in window

所以我创建了一个 class,它在单独的线程中运行大量计算。

这是我希望看到的:

这就是我所看到的:

这是框架class

class ThreadFrame(wx.Frame):

def __init__(self,parent=None):
    wx.Frame.__init__(self,parent=parent)
    self.frame = wx.Frame(None, title='Bitte Warten',style=wx.FRAME_NO_TASKBAR)
    self.frame.SetSize(500,100)
    self.panel=wx.Panel(self.frame)
    self.parent=parent
    self.WaitLbl=wx.StaticText(self.panel,-1)
    self.WaitLbl.SetLabel('Geotags werden ausgelesen und abgerufen.')
    self.progress = wx.Gauge(self.panel,size=(500,30), range=self.parent.list_ctrl.GetItemCount())
    self.btn = wx.Button(self.panel,label='Abbrechen')
    self.btn.Bind(wx.EVT_BUTTON, self.OnExit)

    self.Sizer=wx.BoxSizer(wx.VERTICAL)          

    #Add Widgets LeftSizer
    self.Sizer.Add(self.WaitLbl,0,wx.ALL|wx.CENTER,5)
    self.Sizer.Add(self.progress,0,wx.ALL,5)
    self.Sizer.Add(self.btn,0,wx.ALL|wx.CENTER,5)


    self.panel.SetSizer(self.Sizer)
    self.Sizer.Fit(self.panel)
    self.panel.Layout()            
    self.Centre()            



    #Bind to the progress event issued by the thread
    self.Bind(EVT_PROGRESS_EVENT, self.OnProgress)
    #Bind to Exit on frame close
    #self.Bind(wx.EVT_CLOSE, self.OnExit)
    self.Show()

    self.mythread = TestThread(self.frame, self.parent)
    #Enable the GUI to be responsive by briefly returning control to the main App
    while self.mythread.isAlive():
        time.sleep(0.1)
        wx.GetApp().Yield()
        continue

    try:
        self.OnExit(None)
    except:
        pass

def OnProgress(self, event):
    self.progress.SetValue(event.count)
    #or for indeterminate progress
    #self.progress.Pulse()

def OnExit(self, event):
    if self.mythread.isAlive():
        print('Thread lebt noch')
        self.mythread.terminate() # Shutdown the thread
        print('Thread wird beendet')
        self.mythread.join() # Wait for it to finish

    self.Close()

这是计算的线程 运行

class TestThread(Thread):
def __init__(self,parent_target,toplevel):
    Thread.__init__(self)
    self.parent = toplevel
    self.ownparent=parent_target
    self.stopthread = False
    self.start()    # start the thread

def run(self):
    print('Thread gestartet')
    i=0
    while self.stopthread == False:        
           #if calculation is not finished:
                #do calculation and count i one up
                evt = progress_event(count=i)

                    #Send back current count for the progress bar
                try:
                    wx.PostEvent(self.ownparent, evt)

                except: # The parent frame has probably been destroyed

                    self.terminate()
                i=i+1
        else:
            print('Thread Terminated')
            self.terminate()


def terminate(self):
    self.stopthread = True

这就是我在主程序中调用 class 的方式:

frame=ThreadFrame(self)

主程序也有框架打开。所以这是一个打开的框架,然后启动一个线程进行计算,然后停止。 我认为这就是所有要知道的。我用伪代码替换了计算,因为我的大脑受伤了,我现在不能想出一个 plachold。但我觉得在所有配件、尺寸测量器、面板和框架之间,我哪里出错了。我现在完全不看所有这些东西。

您显示的代码似乎与屏幕截图不符。无论您的布局有什么问题,框架标题中仍然应该有 "Bitte warten",但您的屏幕截图甚至没有显示这一点。要么您根本没有执行您显示的代码,要么您在此处看到的代码的其他地方创建了一些其他框架。或者,当然,您上传了错误的屏幕截图或代码版本。但是有些东西不适合这里。

所以我不确定是什么导致了这个问题。我从头开始,现在可以了。这次我没有使用新的框架,因为 class 本身就是一个框架。

class GeoThreadFrame(wx.Frame):

def __init__(self, radsteuer):
    wx.Frame.__init__(self,parent=radsteuer.frame,style=wx.DEFAULT_FRAME_STYLE | wx.STAY_ON_TOP|wx.FRAME_NO_TASKBAR)
    panel = wx.Panel(self)
    self.SetWindowStyle(wx.FRAME_NO_TASKBAR|wx.STAY_ON_TOP)

    self.progress = wx.Gauge(panel,size=(300,30), pos=(10,50), range=radsteuer.list_ctrl.GetItemCount())

    self.btn = wx.Button(panel,label='Abbrechen', size=(200,30), pos=(10,10))
    self.btn.Bind(wx.EVT_BUTTON, self.OnExit)
    panel.Center()
    #Bind to the progress event issued by the thread
    self.Bind(EVT_PROGRESS_EVENT, self.OnProgress)
    #Bind to Exit on frame close
    self.Bind(wx.EVT_CLOSE, self.OnExit)
    self.Center()
    self.Show()

    self.mythread = GeoLocationThread(self, radsteuer)
    #Enable the GUI to be responsive by briefly returning control to the main App
    while self.mythread.isAlive():            
        #time.sleep(0.1)
        wx.GetApp().Yield()
        continue

    try:
        self.OnExit(None)
    except:
        pass

def OnProgress(self, event):
    self.progress.SetValue(event.count)
    #or for indeterminate progress
    #self.progress.Pulse()

def OnExit(self, event):

    if self.mythread.isAlive():            
        self.mythread.terminate() # Shutdown the thread            
        #self.mythread.join(3) # Wait for it to finish            
    self.Destroy()

class GeoLocationThread(Thread):
    def __init__(self,parent_target,mainparent):
        Thread.__init__(self)
        self.parent = parent_target
        self.mainparent=mainparent
        self.stopthread = False
        self.start()    # start the thread

    def run(self):
        # A loop that will run for 5 minutes then terminate
        i=0
        while self.stopthread == False:


            if i < self.mainparent.list_ctrl.GetItemCount():
                self.calculation(i)
                evt = progress_event(count=i)
                i=i+1
                #Send back current count for the progress bar
                try:
                    wx.PostEvent(self.parent, evt)
                except: # The parent frame has probably been destroyed
                    self.terminate()
            else:
                self.terminate()

    def terminate(self):

        self.stopthread = True

    def calculate(self,i):
        #your calculation here