wxPython:不同模块中的事件方法

发布于 2024-12-25 01:55:04 字数 1418 浏览 1 评论 0原文

我有两个这样的模块(非常非常简化):

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 中是行不通的(正如 a 所期望的那样)。 所以,我的问题是,我必须在代码中更改哪些内容才能使事件方法或其他方法可以从另一个模块访问?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同尘 2025-01-01 01:55:04

我不确定你想要什么。如果这是关于您想要在类中实现的一般行为(例如关闭它们的特定方式、报告状态、添加气球提示系统等),那么一种方法是创建一个具有相应方法的类并从她继承。

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):
        ..........

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.

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

then you import that class and inherit from it:

class MyFrame(wx.Frame, BehaviorPack):
    def __init__(self, *args, **kargs):
        ..........
迟到的我 2025-01-01 01:55:04

您始终可以使用 PubSub 模型(其中包含一个作为 wxpython 的一部分/派生自 wxpython 的模型)。

您可以在一个模块中设置一个侦听器(订阅者),然后向其发送一个事件(发布)以及事件发生的上下文。

这样可以轻松分离。他们有特定的 wxpython 示例供您遵循:http://pubsub.sourceforge.net/

You can always use a PubSub model (there is one included as part of wxpython/derived from wxpython).

You'd set up a listener (subscriber) in one module and then sent it an event (publish) along with the context in which the event occurs.

This allows for easy separation. They have specific wxpython examples even for you to follow: http://pubsub.sourceforge.net/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文