如何以我用wxFormBuilder创建的形式读取/更新wxgrid

时间:2015-09-21 11:56:48

标签: python wxpython wxformbuilder wxgrid

我在名为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()

1 个答案:

答案 0 :(得分: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

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