嗨,我是python新手,另外我是自学程序员。在
我试图让复制和粘贴方法在wxpython中为剪贴板工作。在
我已经找到并实现了我在这个主题上的发现,但是在我的mac电脑(osx10.10.5)上使用时有一个问题。在
所附的代码是一个示例应用程序,它本身运行良好(给定网格的限制)。它还可以很好地从网格单元格复制和粘贴到外部记事本或电子表格。这意味着SetData在构建剪贴板数据时获取并维护制表符分隔符和新行。在
但是,如果我从同一个记事本或电子表格中选择制表符分隔和多行数据并继续粘贴到网格中,则只得到一列数据。这意味着在GetData中制表符和换行符丢失。在
数据选择为
1 2 3个
4 5 6个
在电子表格中。在
按照建议,使用print repr(data)来获取剪贴板所保存的内容, 在应用程序中复制和粘贴会导致 粘贴数据-打印报告(数据)=u'1\t2\t3\n4\t5\t6\n'
从外部源复制数据时,粘贴似乎只有\r返回字符和ufeff??我不知道的?也许这是一把钥匙?(在mac上)
打印repr(data)=u'\ufeff\r1\r2\r3\r4\r5\r6\r\r'
现在这在我的Windows机器上运行得很好,但在Mac上不行。在
这是已知问题吗?有没有解决办法,或者有没有我缺少的或者我不理解的设置?在
非常感谢任何帮助或指导。 谢谢 弗兰克
import wx
import wx.grid as dg
class cp(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, wx.ID_ANY, size = (600,600))
self.dgGrid = dg.Grid(self, size = (500,500))
self.dgGrid.CreateGrid(10,5)
self.dgGrid.Bind(wx.EVT_KEY_DOWN, self.OnKeyPress)
def OnKeyPress(self, event):
# If Ctrl+V is pressed...
if event.ControlDown() and event.GetKeyCode() == 86:
print "Ctrl+V"
# Call paste method
self.Paste()
if event.ControlDown() and event.GetKeyCode() == 67:
print "Ctrl+C"
# Call copy method
self.copy()
event.Skip()
def copy(self):
print "Copy method"
# Number of rows and cols
rows = self.dgGrid.GetSelectionBlockBottomRight()[0][0] - self.dgGrid.GetSelectionBlockTopLeft()[0][0] + 1
cols = self.dgGrid.GetSelectionBlockBottomRight()[0][1] - self.dgGrid.GetSelectionBlockTopLeft()[0][1] + 1
# data variable contain text that must be set in the clipboard
data = ''
# For each cell in selected range append the cell value in the data variable
# Tabs '\t' for cols and '\r' for rows
for r in range(rows):
for c in range(cols):
data = data + str(self.dgGrid.GetCellValue(self.dgGrid.GetSelectionBlockTopLeft()[0][0] + r, self.dgGrid.GetSelectionBlockTopLeft()[0][1] + c))
if c < cols - 1:
data = data + '\t'
data = data + '\n'
# Create text data object
clipboard = wx.TextDataObject()
# Set data object value
clipboard.SetText(data)
# Put the data in the clipboard
if wx.TheClipboard.Open():
wx.TheClipboard.SetData(clipboard)
wx.TheClipboard.Close()
else:
wx.MessageBox("Can't open the clipboard", "Error")
def Paste(self):
print "Paste method"
clipboard = wx.TextDataObject()
if wx.TheClipboard.Open():
wx.TheClipboard.GetData(clipboard)
wx.TheClipboard.Close()
else:
wx.MessageBox("Can't open the clipboard", "Error")
return
data = clipboard.GetText()
y = -1
# Convert text in a array of lines
for r in data.splitlines():
y = y +1
x = -1
print r
# Convert c in a array of text separated by tab
for c in r.split('\t'):
x = x +1
print c
self.dgGrid.SetCellValue(self.dgGrid.GetGridCursorRow() + y, self.dgGrid.GetGridCursorCol() + x, c)
if __name__ == '__main__':
print ' running locally not imported '
app = wx.App(False)
MainFrame = wx.Frame(None, title = "TestingCopy and Paste", size = (600,600))
cppanel = cp(MainFrame)
MainFrame.Refresh()
MainFrame.Show()
app.MainLoop()
目前没有回答
相关问题 更多 >
编程相关推荐