]>
Commit | Line | Data |
---|---|---|
1 | import wx | |
2 | ||
3 | t1_text = """\ | |
4 | The whole contents of this control | |
5 | will be placed in the system's | |
6 | clipboard when you click the copy | |
7 | button below. | |
8 | """ | |
9 | ||
10 | t2_text = """\ | |
11 | If the clipboard contains a text | |
12 | data object then it will be placed | |
13 | in this control when you click | |
14 | the paste button below. Try | |
15 | copying to and pasting from | |
16 | other applications too! | |
17 | """ | |
18 | ||
19 | class 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 | ||
74 | app = wx.PySimpleApp() | |
75 | frm = MyFrame() | |
76 | frm.Show() | |
77 | app.MainLoop() |