]> git.saurik.com Git - wxWidgets.git/blame - wxPython/samples/wxPIA_book/Chapter-18/clipboard.py
fix building/running of tex2rtf
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-18 / clipboard.py
CommitLineData
be05b434
RD
1import wx
2
3t1_text = """\
4The whole contents of this control
5will be placed in the system's
6clipboard when you click the copy
7button below.
8"""
9
10t2_text = """\
11If the clipboard contains a text
12data object then it will be placed
13in this control when you click
14the paste button below. Try
15copying to and pasting from
16other applications too!
17"""
18
19class MyFrame(wx.Frame):
20 def __init__(self):
21 wx.Frame.__init__(self, None, title="Clipboard",
22 size=(500,300))
23 p = wx.Panel(self)
24
25 # create the controls
26 self.t1 = wx.TextCtrl(p, -1, t1_text,
27 style=wx.TE_MULTILINE|wx.HSCROLL)
28 self.t2 = wx.TextCtrl(p, -1, t2_text,
29 style=wx.TE_MULTILINE|wx.HSCROLL)
30 copy = wx.Button(p, -1, "Copy")
31 paste = wx.Button(p, -1, "Paste")
32
33 # setup the layout with sizers
34 fgs = wx.FlexGridSizer(2, 2, 5, 5)
35 fgs.AddGrowableRow(0)
36 fgs.AddGrowableCol(0)
37 fgs.AddGrowableCol(1)
38 fgs.Add(self.t1, 0, wx.EXPAND)
39 fgs.Add(self.t2, 0, wx.EXPAND)
40 fgs.Add(copy, 0, wx.EXPAND)
41 fgs.Add(paste, 0, wx.EXPAND)
42 border = wx.BoxSizer()
43 border.Add(fgs, 1, wx.EXPAND|wx.ALL, 5)
44 p.SetSizer(border)
45
46 # Bind events
47 self.Bind(wx.EVT_BUTTON, self.OnDoCopy, copy)
48 self.Bind(wx.EVT_BUTTON, self.OnDoPaste, paste)
49
50 def OnDoCopy(self, evt):
51 data = wx.TextDataObject()
52 data.SetText(self.t1.GetValue())
53 if wx.TheClipboard.Open():
54 wx.TheClipboard.SetData(data)
55 wx.TheClipboard.Close()
56 else:
57 wx.MessageBox("Unable to open the clipboard", "Error")
58
59 def OnDoPaste(self, evt):
60 success = False
61 data = wx.TextDataObject()
62 if wx.TheClipboard.Open():
63 success = wx.TheClipboard.GetData(data)
64 wx.TheClipboard.Close()
65
66 if success:
67 self.t2.SetValue(data.GetText())
68 else:
69 wx.MessageBox(
70 "There is no data in the clipboard in the required format",
71 "Error")
72
73
74app = wx.PySimpleApp()
75frm = MyFrame()
76frm.Show()
77app.MainLoop()