2010-10-18 51 views
1

我有一個非常簡單的GUI,它接受兩個參數,然後調用名爲DigitalFilter(),BeatByBeatVariables()和GetSummaryOfWholeTest()的其他三個類。這是我第一次寫課程,我需要語法幫助。使用wxpython進行GUI的類繼承

具體來說,你能幫我繼承嗎?我想要在應用程序啓動時調用GUI的類MainWindow(wx.Frame)。然後,當用戶在GUI中單擊self.button時,我希望應用程序首先運行myFilter = DigitalFilter(TestID,FilterTimePeriod),然後再運行myBeatByBeat = BeatByBeatVariables(arg1,arg2),最後運行myTestSummary = GetSummaryOfWholeTest argA,argB)

請注意,myBeatByBeat無法開始運行,直到myFilter完成創建,因爲myFilter的實例化會創建作爲myBeatByBeat輸入所需的csv文件。同樣,myTest摘要無法開始運行,直到myBeatByBeat完成創建,因爲myBeatByBeat的實例化會創建作爲myTestSummary輸入所需的csv文件。

任何人都可以向我展示使用適當的繼承編寫/實例化這些類的正確語法,以便每個類的工作都將按照尊重其輸入/輸出關係的順序完成。

我假設在這裏應該繼承,但我不知道應該從什麼繼承。我也不知道我是否沒有看到其他必要的概念來爲代碼提供必要的相互關係。

下面是相關代碼的概要:

class DigitalFilter(): 
    def __init__(self,TestID,FilterTimePeriod): 
    # All the code for the digital filter goes here. 
    # I am omitting this class' code for simplicity. 

class BeatByBeatVariables(): 
    def __init__(self,arg1,arg2): 
    # I am omitting this class' code for simplicity 

class GetSummaryOfWholeTest(): 
    def __init__(self,argA,ArgB): 
    # This class' code is omitted for simplicity 

class MainWindow(wx.Frame): 
    def __init__(self, parent,id,title): 
     wx.Frame.__init__(self,parent,wx.ID_ANY,title, size = (500,500), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE) 

     # Create self.editname and self.edithear in code that I am omitting for simplicity 
     self.button =wx.Button(self, label="Click here to filter the data", pos=(200, 125)) 
     self.Bind(wx.EVT_BUTTON, self.OnClick,self.button) 

    def OnClick(self,event): 
     FilterTimePeriod = self.editname.GetValue() 
     TestID = self.edithear.GetValue() 
     myFilter=DigitalFilter(TestID,FilterTimePeriod) 
     myBeatByBeat = BeatByBeatVariables(arg1,arg2) 
     myTestSummary = GetSummaryOfWholeTest(argA,argB) 

app = wx.PySimpleApp() 
frame = MainWindow(None,-1,"Filtering Software. Version 1.0") 
app.MainLoop() 

注:我使用Python 2.6,因爲我也使用numpy的和sciepy

回答

1

這聽起來像你有點無所適從繼承手段。從基類繼承的類爲該基類添加額外的功能,但並不意味着實例化順序或數據傳遞的任何內容。如果您只想強制創建對象序列,則應該使用wx.lib.pubsub來了解每個步驟何時結束。

當過濾完成時,您可以發送來自DigitalFilter的消息,並在已實例化的BeatByBeatVariables實例中接收該消息,或者在主類中接收該消息並在此時創建BeatByBeatVariables實例。兩者都有效。如果我知道我需要所有這些情況下,我可能會提前創建它們的時候,就像這樣:

from wx.lib.pubsub import setupkwargs # needed only in version 2.8.11 
from wx.lib.pubsub import pub 

class DigitalFilter(): 
    def __init__(self,TestID,FilterTimePeriod): 
     pass 
    def do_something(self, data): 
     # do something 
     pub.sendMessage('filter.done', data=some_data) 

class BeatByBeatVariables(): 
    def __init__(self,arg1,arg2): 
     pub.subscribe(self.do_something, 'filter.done') 
    def do_something(self, data): 
     # do something 
     pub.sendMessage('beatbybeat.done', data=some_data) 

class GetSummaryOfWholeTest(): 
    def __init__(self,argA,ArgB): 
     pub.subscribe(self.do_something, 'beatbybeat.done') 
    def do_something(self, data): 
     # do something 
     pub.sendMessage('summary.done', data=some_data) 

然後在你的主類,你可以有:

def OnClick(self,event): 
     FilterTimePeriod = self.editname.GetValue() 
     TestID = self.edithear.GetValue() 
     myFilter=DigitalFilter(TestID,FilterTimePeriod) 
     myBeatByBeat = BeatByBeatVariables(arg1,arg2) 
     myTestSummary = GetSummaryOfWholeTest(argA,argB) 

     myFilter.do_something() 
     # all the other do_somethings are triggered by message passing