I have two modules like this (very very simplified):
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')
Now i would like to move the OnClose method from main.py to window.py (not only this one, if have many other methods in main.py, and I want to move all of them to different modules to make my code more structured).
But just cutting the module from main.py and pasting it in window.py doesn’t work (as a was expecting).
So, my question is, what do I have to change in my code to make an event method or another method accessible from another module?
I am not sure what you want. If this is about general behavior you want to implement in your classes (like a particular way of closing them, report state, adding a balloontip system, etc), then one way is to create a class with the corresponding methods and to inherit from her.
then you import that class and inherit from it: