2012-01-03 3 views
0

다른 모듈에서 이벤트있어서의 wxPython : I는 (아주 단순화) 다음과 같은 두 개의 모듈이

main.py :

from window import * 

class MyApp(wx.Frame): 

    def __init__(self, parent, label, pos, size): 
     wx.Frame.__init__(self, parent = parent, title = label, pos = pos, size = size) 
     self.Centre() 
     create_window(self) 
     self.Bind(wx.EVT_CLOSE, self.OnClose) 

    def OnClose(self, event): 
     self.dlg = wx.MessageDialog(self, 'Quit application', 
      'Please confirm', wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION) 
     if self.dlg.ShowModal() == wx.ID_YES: 
      self.Destroy() 

if __name__ == '__main__': 
    app = wx.App() 
    frame = MyApp(None, 'MyApp', (0, 0), (740, 640)) 
    frame.Show() 
    frame.SetFocus() 
    app.MainLoop() 

window.py : 지금

import wx    

def create_window(self): 

    self.menubar = wx.MenuBar() 
    self.fileMenu = wx.Menu() 

    self.item = self.fileMenu.Append(wx.ID_EXIT, 'Quit', 'Quit application') 

    self.Bind(wx.EVT_MENU, self.OnClose, self.item) 

    self.menubar.Append(self.fileMenu, '&File') 
    self.SetMenuBar(self.menubar) 

    self.statusbar = self.CreateStatusBar() 
    self.statusbar.SetStatusText('Ready') 

난 OnClose 메서드를 main.py에서 window.py로 옮기고 싶습니다 (main.py에는 다른 많은 메서드가있는 경우이 메서드뿐만 아니라 모든 코드를 다른 모듈로 이동하여 코드를 좀 더 구조화하고 싶습니다). 그러나 main.py에서 모듈을 잘라내서 window.py에 붙여 넣기 만하면 작동하지 않습니다 (예상대로). 내 질문에, 이벤트 코드 또는 다른 모듈에서 액세스 할 수있는 다른 메서드를 만들기 위해 코드에서 무엇을 변경해야합니까?

+0

클래스 내에서 create_window 메소드를 사용하거나 함수처럼 보입니까? – joaquin

답변

0

나는 당신이 원하는 것이 확실하지 않습니다. 이것이 클래스에서 구현하려는 일반적인 동작 (예 : 클래스를 닫거나 상태를보고하고 ballonontip 시스템을 추가하는 등)을 사용하는 경우 한 가지 방법은 해당 메소드로 클래스를 만들고 그 클래스에서 상속하는 것입니다 .

class BehaviorPack(object): 
    def __init__(self): 
     whatever 
    def OnClose(self, event) 
     whatever 
    def colour_panel(self): 
     whatever 

는 해당 클래스를 가져 와서 상속 :

class MyFrame(wx.Frame, BehaviorPack): 
    def __init__(self, *args, **kargs): 
     .......... 
0

당신은 항상 PubSub 모델을 사용할 수 있습니다 (하나의 wxPython에서 파생 된 wxPython에의 부품/포함이있다).

한 모듈에서 수신기 (구독자)를 설정 한 다음 이벤트가 발생하는 컨텍스트와 함께 이벤트 (게시)를 보냅니다.

이렇게하면 쉽게 분리 할 수 ​​있습니다. 그들은 wxpython을 따르는 특정 예제를 가지고 있습니다 : http://pubsub.sourceforge.net/

관련 문제