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