wxPython类未在窗口中显示

问题描述 投票:0回答:2

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

这是我期望看到的:

Expected

这就是我看到的:

What i should see

这是框架类

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

这就是我从主程序中调用类的方式:

frame=ThreadFrame(self)

主程序也有一个打开的框架。因此,这是一个打开的框架,然后启动一个线程进行计算,然后停止。我想这就是全部。我用伪代码代替了计算,因为我的大脑受伤了,我现在无法提出要求。但是我觉得在所有试穿和大小调整器,面板和框架之间我走错了地方。我目前完全不看所有这些东西。

python-3.x wxpython wxwidgets
2个回答
0
投票

您显示的代码似乎与屏幕截图不符。无论您可能遇到的布局有什么问题,框架标题中都应该仍然有“ Bitte warten”,但是您的屏幕截图甚至都没有显示此内容。您要么根本不执行显示的代码,要么在代码的其他位置创建其他框架(在此处看到)。或者,当然,您上传了错误的屏幕截图或代码版本。但是这里有些不适合。


0
投票

所以我不确定是什么引起了问题。我从头开始,现在可以使用。这次我没有使用新框架,因为该类本身已经是一个框架。

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
© www.soinside.com 2019 - 2024. All rights reserved.