0

我在名为 gui.py 的 .py 文件中创建了一个名为 MyFrame1 的带有 wxFormBuilder 的框架。我正在尝试在该网格中读取和写入值,但为了解决问题已经卡了几个小时。这是代码的简化版本,因为我之前发布的示例过于复杂。

该应用程序在 maingridtest.py 中启动,我正在尝试从那里读取和写入网格。如果我将所有内容集成到一个文件中(wFormBuilder gui 和 maingridtest 到说'code.py'我可以读写网格没有问题。我希望 formbulider 代码保持分开以简化对 gui 的更新。

无论我尝试什么,我都无法让 python 在 gui.py 中找到 m_grid1。

这是 maingridtest.py

    __author__ = 'Paul'

import wx
import wx.xrc
import wx.grid
from gui import MyFrame1


class ReadGrid(MyFrame1):
    def __init__(self, parent):
        MyFrame1.__init__(self, parent)

    test = m_grid1.GetCellValue(2, 2)
    print test


if __name__ == '__main__':
    app = wx.App(0)
    MainApp = MyFrame1(None)
    MainApp.Show()
    app.MainLoop()

这是gui.py

# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Jun 17 2015)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc
import wx.grid

ID_ABOUT = 1000

###########################################################################
## Class MyFrame1
###########################################################################

class MyFrame1 ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Grid Test", pos = wx.DefaultPosition, size = wx.Size( 818,525 ), style = wx.CAPTION|wx.CLOSE_BOX|wx.DEFAULT_FRAME_STYLE|wx.SYSTEM_MENU|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_grid1 = wx.grid.Grid( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )

        # Grid
        self.m_grid1.CreateGrid( 5, 5 )
        self.m_grid1.EnableEditing( True )
        self.m_grid1.EnableGridLines( True )
        self.m_grid1.EnableDragGridSize( False )
        self.m_grid1.SetMargins( 0, 0 )

        # Columns
        self.m_grid1.EnableDragColMove( False )
        self.m_grid1.EnableDragColSize( True )
        self.m_grid1.SetColLabelSize( 30 )
        self.m_grid1.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )

        # Rows
        self.m_grid1.EnableDragRowSize( True )
        self.m_grid1.SetRowLabelSize( 80 )
        self.m_grid1.SetRowLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )

        # Label Appearance

        # Cell Defaults
        self.m_grid1.SetDefaultCellAlignment( wx.ALIGN_LEFT, wx.ALIGN_TOP )
        bSizer1.Add( self.m_grid1, 0, wx.ALL, 5 )


        self.SetSizer( bSizer1 )
        self.Layout()
        self.m_menubar1 = wx.MenuBar( 0 )
        self.file = wx.Menu()
        self.m_menubar1.Append( self.file, u"File" ) 

        self.help = wx.Menu()
        self.about = wx.MenuItem( self.help, ID_ABOUT, u"About", wx.EmptyString, wx.ITEM_NORMAL )
        self.help.AppendItem( self.about )

        self.m_menubar1.Append( self.help, u"Help" ) 

        self.SetMenuBar( self.m_menubar1 )


        self.Centre( wx.BOTH )

        # Connect Events
        self.Bind( wx.EVT_CLOSE, self.closeGridFrame )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def closeGridFrame( self, event ):
        event.Skip()
4

1 回答 1

0

你只需要改变几件小事:

import wx
import wx.xrc
import wx.grid
from gui import MyFrame1


class ReadGrid(MyFrame1):
    def __init__(self, parent):
        MyFrame1.__init__(self, parent)


        test = self.m_grid1.GetCellValue(2, 2)
        print test


if __name__ == '__main__':
    app = wx.App(0)
    MainApp = ReadGrid(None)
    MainApp.Show()
    app.MainLoop()

首先,您需要实际调用您的子类,ReadGrid而不是MyFrame. 那根本行不通。接下来你想m_grid1通过这样调用它来访问:

test = self.m_grid1.GetCellValue(2, 2)

由于您没有在该单元格中设置值,因此只会返回一个空字符串,因此您仍然不会得到任何输出。所以我编辑了你的gui.py代码,所以它有一个按钮,你可以用它来获取那个值:

import wx
import wx.xrc
import wx.grid

ID_ABOUT = 1000

###########################################################################
## Class MyFrame1
###########################################################################

class MyFrame1 ( wx.Frame ):

    def __init__( self, parent ):
        style = wx.CAPTION|wx.CLOSE_BOX|wx.DEFAULT_FRAME_STYLE|wx.SYSTEM_MENU|wx.TAB_TRAVERSAL
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Grid Test",
                            pos = wx.DefaultPosition, size = wx.Size( 818,525 ),
                            style = style )
        panel = wx.Panel(self)

        #self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.m_grid1 = wx.grid.Grid( panel, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )

        # Grid
        self.m_grid1.CreateGrid( 5, 5 )
        self.m_grid1.EnableEditing( True )
        self.m_grid1.EnableGridLines( True )
        self.m_grid1.EnableDragGridSize( False )
        self.m_grid1.SetMargins( 0, 0 )

        # Columns
        self.m_grid1.EnableDragColMove( False )
        self.m_grid1.EnableDragColSize( True )
        self.m_grid1.SetColLabelSize( 30 )
        self.m_grid1.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )

        # Rows
        self.m_grid1.EnableDragRowSize( True )
        self.m_grid1.SetRowLabelSize( 80 )
        self.m_grid1.SetRowLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )

        # Label Appearance

        # Cell Defaults
        self.m_grid1.SetDefaultCellAlignment( wx.ALIGN_LEFT, wx.ALIGN_TOP )
        bSizer1.Add( self.m_grid1, 0, wx.ALL, 5 )

        # get a value from the grid
        value_btn = wx.Button(panel, label='Get Value')
        value_btn.Bind(wx.EVT_BUTTON, self.onGetValue)
        bSizer1.Add(value_btn, 0, wx.ALL, 5)


        panel.SetSizer( bSizer1 )
        self.Layout()
        self.m_menubar1 = wx.MenuBar( 0 )
        self.file = wx.Menu()
        self.m_menubar1.Append( self.file, u"File" )

        self.help = wx.Menu()
        self.about = wx.MenuItem( self.help, ID_ABOUT, u"About", wx.EmptyString, wx.ITEM_NORMAL )
        self.help.AppendItem( self.about )

        self.m_menubar1.Append( self.help, u"Help" )

        self.SetMenuBar( self.m_menubar1 )


        self.Centre( wx.BOTH )

        # Connect Events
        self.Bind( wx.EVT_CLOSE, self.closeGridFrame )

    def __del__( self ):
        pass


    # Virtual event handlers, overide them in your derived class
    def closeGridFrame( self, event ):
        event.Skip()

    def onGetValue(self, event):
        value = self.m_grid1.GetCellValue(2, 2)
        print value

我还将小部件的父级设置为 a 的实例,wx.Panel因为这是大多数小部件的推荐父级。通过这样做,您将在每个平台上获得正确的外观,它还使您能够正确地在小部件之间切换。

于 2015-09-22T14:56:19.770 回答