从Python wx.Grid复制并粘贴

时间:2013-05-23 10:50:57

标签: wxpython wxgrid

我想知道是否有人可以提供帮助? 我有一个分为两个面板的表格。 leftP有我的控件,rightP有网格。我正在尝试从网格复制/粘贴到剪贴板。使用以下代码,我收到一个错误: AttributeError:'RightPanel'对象没有属性'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()

1 个答案:

答案 0 :(得分:0)

您需要通过“自我”将网格设置为实例变量。'在它面前,目前网格只是一个局部变量

此外,您还需要拨打加载数据'预先设定方法,以便创建网格并将其存储为实例变量。