2010-10-18 54 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