【问题标题】:WxPython - How to acces class instance by other classWxPython - 如何通过其他类访问类实例
【发布时间】:2017-11-24 23:30:11
【问题描述】:

我正在使用的代码:

class MyApp(wx.App):
    def OnInit(self):
        frame = MainWindow(None, -1, 'MyApp')
        frame.SetIcon(wx.Icon('tutorial.ico', wx.BITMAP_TYPE_ICO))
        frame.Center()
        frame.Show(True)
        return True


class MainWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(620, 480))
        menubar = wx.MenuBar()
        file = wx.Menu()
        edit = wx.Menu()
        help = wx.Menu()
        file.Append(101, '&New\tCtrl+N')
        file.AppendSeparator()
        file.Append(102, '&Open\tCtrl+O')
        file.Append(103, '&Save\tCtrl+S')
        file.Append(104, '&Save As...')
        file.AppendSeparator()
        file.Append(105, '&Settings')
        file.Append(106, '&Quit\tCtrl+Q')

        self.model = wx.MenuItem(edit, 201, '&Model', 'Model Mode', kind = wx.ITEM_CHECK)

        self.box = wx.BoxSizer(wx.VERTICAL)        
        self.c = CubeCanvas(self)
        self.c.SetMinSize((200, 200))
        self.box.Add(self.c, 0, wx.EXPAND |wx.ALL , 15)


class MyCanvasBase(glcanvas.GLCanvas):
    def __init__(self, parent):
        glcanvas.GLCanvas.__init__(self, parent, -1)
        self.init = False
        self.context = glcanvas.GLContext(self)

        self.lastx = self.x = 30
        self.lasty = self.y = 30
        self.size = None
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        self.Bind(wx.EVT_MOTION, self.OnMouseMotion)

        def OnMouseMotion(self, evt):
            if frame.model.IsChecked(): #NameError: name 'frame' is not defined
                self.lastx, self.lasty = self.x, self.y
                self.x, self.y = evt.GetPosition()
                self.Refresh(False)


class CubeCanvas(MyCanvasBase):
    def InitGL(self):
        # set viewing projection
        glMatrixMode(GL_PROJECTION)
        glFrustum(-0.5, 0.5, -0.5, 0.5, 1.0, 3.0)
        # position viewer
        glMatrixMode(GL_MODELVIEW)
        glTranslatef(0.0, 0.0, -2.0)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glEnable(GL_LINE_SMOOTH)
        glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
        glEnable(GL_POLYGON_SMOOTH)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_LIGHTING)
        glEnable(GL_LIGHT0)

    def OnDraw(self):
        # clear color and depth buffers
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        w, h = self.size
        w = max(w, 1.0)
        h = max(h, 1.0)
        dx = self.x / (w/2) - 1.0
        dy = self.y / (h/2) - 1.0
        glLineWidth(2.5)
        glColor3f(1.0, 0.0, 0.0)
        glBegin(GL_LINES)
        glVertex2f(0, 0)
        glVertex2f(dx, -dy)
        glEnd()
        self.SwapBuffers()


statica = MyApp(0)
statica.MainLoop()

如何从另一个类方法(如MyCanvasBase.onMouseMotion)访问MainWindow 的model 变量? wxPython可以吗?

我试过了:frame.model、MainWindow.model,没有成功。

如何解决wxPython中类之间的通信等问题?在 C++ 中,我有 main 函数,在 python 中,所有逻辑都在 __init__ 或方法中。

【问题讨论】:

标签: python user-interface wxpython pyopengl


【解决方案1】:

在您的 MyCanvasBase 类的 __init__ 中,添加以下内容:

self.parent = parent

由于MyCanvasBase 的父级是MainWindow 的一个实例,那么您将能够像这样访问CubeCanvas 中的框架模型:

self.parent.model

不过,我首选的类间通信方法是使用 Pubsub。您可以在以下位置阅读:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 2013-02-21
    相关资源
    最近更新 更多