0

我想知道是否有人可以帮忙?我有一个分成两个面板的表格。一个 leftP 有我的控件,而 rightP 有一个网格。我正在尝试从网格复制/粘贴到剪贴板。使用以下代码,我得到一个错误: AttributeError: 'RightPanel' object has no attribute 'grid'。
我遵循与“LeftPanel”相同的格式,这似乎有效。有什么想法吗?

import wx
import wx.grid as gridlib
import  pyodbc
import sys,os
import csv 

class RightPanel(wx.Panel):
""""""
def __init__(self, parent):
    """Constructor"""
    wx.Panel.__init__(self, parent=parent)
    self.SetBackgroundColour("light blue")        

def LoadData(self, connstr, query, table):  

    grid = gridlib.Grid(self)
    grid.CreateGrid(20,20)  
    con = pyodbc.connect(connstr)        
    cur = con.cursor()  
    # rest of Grid code here

class LeftPanel(wx.Panel):
""""""
def __init__(self, parent):
    """Constructor"""
    wx.Panel.__init__(self, parent=parent)

    # rest of Left Panel code here

class MyForm(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY, "DB Viewer", size=(1150, 450)) 

    splitter = wx.SplitterWindow(self)
    self.leftP = LeftPanel(splitter)
    self.rightP = RightPanel(splitter)

    splitter.SplitVertically(self.leftP, self.rightP)
    splitter.SetMinimumPaneSize(20)

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(splitter, 1, wx.EXPAND)
    self.SetSizer(sizer)


    self.rightP.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.copy)

    self.Layout()

def copy(self, event):
    print "Copy method"
    # Number of rows and cols

    rows = self.rightP.grid.GetSelectionBlockBottomRight()[0][0] -    self.rightP.grid.GetSelectionBlockTopLeft()[0][0] + 1
    cols = self.rightP.grid.GetSelectionBlockBottomRight()[0][1] - self.rightP.grid.GetSelectionBlockTopLeft()[0][1] + 1
    #This is where it throws an error####################    
    #Rest of copy Code goes Here


if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
4

1 回答 1

0

您需要通过放置“self.”将网格设置为实例变量。在它前面,此时网格只是一个局部变量

此外,您还需要事先调用“loaddata”方法,以便创建网格并将其存储为实例变量。

于 2013-05-23T12:01:37.283 回答