1
2
3
4
5
6
7
8
9
10 __autor__ = 'http://www.daniweb.com/code/snippet210.html'
11
12 from wxPython.wx import *
13
14
15 [wxID_WXFRAME1, wxID_WXFRAME1BTN0, wxID_WXFRAME1BTN1, wxID_WXFRAME1BTN2,
16 wxID_WXFRAME1BTN3, wxID_WXFRAME1BTN4, wxID_WXFRAME1BTN5, wxID_WXFRAME1BTN6,
17 wxID_WXFRAME1BTN7, wxID_WXFRAME1BTN8, wxID_WXFRAME1BTN9,
18 wxID_WXFRAME1BTNCLEAR, wxID_WXFRAME1BTNDIV, wxID_WXFRAME1BTNDOT,
19 wxID_WXFRAME1BTNEQUAL, wxID_WXFRAME1BTNMINUS, wxID_WXFRAME1BTNMULTI,
20 wxID_WXFRAME1BTNPLUS, wxID_WXFRAME1EDIT,
21 ] = map(lambda _init_ctrls: wxNewId(), range(19))
22
23
24 class wxFrame1(wxFrame):
25
26
27 def _init_ctrls(self, prnt):
28
29 wxFrame.__init__(self, id=wxID_WXFRAME1, name='', parent=prnt,
30 pos=wxPoint(306, 270), size=wxSize(266, 265),
31 style=wxDEFAULT_FRAME_STYLE, title='Calculator1')
32 self.SetClientSize(wxSize(258, 225))
33 self.SetBackgroundColour(wxColour(0, 128, 0))
34
35 self.btn1 = wxButton(id=wxID_WXFRAME1BTN1, label='1', name='btn1',
36 parent=self, pos=wxPoint(16, 136), size=wxSize(32, 32), style=0)
37 EVT_BUTTON(self.btn1, wxID_WXFRAME1BTN1, self.OnBtn1Button)
38
39 self.btn2 = wxButton(id=wxID_WXFRAME1BTN2, label='2', name='btn2',
40 parent=self, pos=wxPoint(64, 136), size=wxSize(32, 32), style=0)
41 EVT_BUTTON(self.btn2, wxID_WXFRAME1BTN2, self.OnBtn2Button)
42
43 self.btn3 = wxButton(id=wxID_WXFRAME1BTN3, label='3', name='btn3',
44 parent=self, pos=wxPoint(112, 136), size=wxSize(32, 32), style=0)
45 EVT_BUTTON(self.btn3, wxID_WXFRAME1BTN3, self.OnBtn3Button)
46
47 self.btn4 = wxButton(id=wxID_WXFRAME1BTN4, label='4', name='btn4',
48 parent=self, pos=wxPoint(16, 96), size=wxSize(32, 32), style=0)
49 EVT_BUTTON(self.btn4, wxID_WXFRAME1BTN4, self.OnBtn4Button)
50
51 self.btn5 = wxButton(id=wxID_WXFRAME1BTN5, label='5', name='btn5',
52 parent=self, pos=wxPoint(64, 96), size=wxSize(32, 32), style=0)
53 EVT_BUTTON(self.btn5, wxID_WXFRAME1BTN5, self.OnBtn5Button)
54
55 self.btn6 = wxButton(id=wxID_WXFRAME1BTN6, label='6', name='btn6',
56 parent=self, pos=wxPoint(112, 96), size=wxSize(32, 32), style=0)
57 EVT_BUTTON(self.btn6, wxID_WXFRAME1BTN6, self.OnBtn6Button)
58
59 self.btn7 = wxButton(id=wxID_WXFRAME1BTN7, label='7', name='btn7',
60 parent=self, pos=wxPoint(16, 56), size=wxSize(32, 32), style=0)
61 EVT_BUTTON(self.btn7, wxID_WXFRAME1BTN7, self.OnBtn7Button)
62
63 self.btn8 = wxButton(id=wxID_WXFRAME1BTN8, label='8', name='btn8',
64 parent=self, pos=wxPoint(64, 56), size=wxSize(32, 32), style=0)
65 EVT_BUTTON(self.btn8, wxID_WXFRAME1BTN8, self.OnBtn8Button)
66
67 self.btn9 = wxButton(id=wxID_WXFRAME1BTN9, label='9', name='btn9',
68 parent=self, pos=wxPoint(112, 56), size=wxSize(32, 32), style=0)
69 EVT_BUTTON(self.btn9, wxID_WXFRAME1BTN9, self.OnBtn9Button)
70
71 self.btn0 = wxButton(id=wxID_WXFRAME1BTN0, label='0', name='btn0',
72 parent=self, pos=wxPoint(16, 176), size=wxSize(32, 32), style=0)
73 EVT_BUTTON(self.btn0, wxID_WXFRAME1BTN0, self.OnBtn0Button)
74
75 self.btnDot = wxButton(id=wxID_WXFRAME1BTNDOT, label='.', name='btnDot',
76 parent=self, pos=wxPoint(64, 176), size=wxSize(32, 32), style=0)
77 EVT_BUTTON(self.btnDot, wxID_WXFRAME1BTNDOT, self.OnBtnDotButton)
78
79 self.btnEqual = wxButton(id=wxID_WXFRAME1BTNEQUAL, label='=',
80 name='btnEqual', parent=self, pos=wxPoint(112, 176),
81 size=wxSize(32, 32), style=0)
82 EVT_BUTTON(self.btnEqual, wxID_WXFRAME1BTNEQUAL, self.OnBtnEqualButton)
83
84 self.edit = wxTextCtrl(id=wxID_WXFRAME1EDIT, name='edit', parent=self,
85 pos=wxPoint(16, 16), size=wxSize(224, 24), style=0, value='')
86
87 self.btnPlus = wxButton(id=wxID_WXFRAME1BTNPLUS, label='+',
88 name='btnPlus', parent=self, pos=wxPoint(160, 56), size=wxSize(32,
89 32), style=0)
90 EVT_BUTTON(self.btnPlus, wxID_WXFRAME1BTNPLUS, self.OnBtnPlusButton)
91
92 self.btnMinus = wxButton(id=wxID_WXFRAME1BTNMINUS, label='-',
93 name='btnMinus', parent=self, pos=wxPoint(160, 96),
94 size=wxSize(32, 32), style=0)
95 EVT_BUTTON(self.btnMinus, wxID_WXFRAME1BTNMINUS, self.OnBtnMinusButton)
96
97 self.btnMulti = wxButton(id=wxID_WXFRAME1BTNMULTI, label='*',
98 name='btnMulti', parent=self, pos=wxPoint(160, 136),
99 size=wxSize(32, 32), style=0)
100 EVT_BUTTON(self.btnMulti, wxID_WXFRAME1BTNMULTI, self.OnBtnMultiButton)
101
102 self.btnDiv = wxButton(id=wxID_WXFRAME1BTNDIV, label='/', name='btnDiv',
103 parent=self, pos=wxPoint(160, 176), size=wxSize(32, 32), style=0)
104 EVT_BUTTON(self.btnDiv, wxID_WXFRAME1BTNDIV, self.OnBtnDivButton)
105
106 self.btnClear = wxButton(id=wxID_WXFRAME1BTNCLEAR, label='C',
107 name='btnClear', parent=self, pos=wxPoint(208, 56),
108 size=wxSize(32, 32), style=0)
109 self.btnClear.SetToolTipString('btnClear')
110 EVT_BUTTON(self.btnClear, wxID_WXFRAME1BTNCLEAR, self.OnBtnClearButton)
111
112 def __init__(self, parent):
113 self._init_ctrls(parent)
114
115
116
117
118 def OnBtn0Button(self, event):
119 val = '0'
120
121 txt = self.edit.GetValue()
122
123 txt = txt + val
124
125 self.edit.SetValue(txt)
126
127 def OnBtn1Button(self, event):
128 val = '1'
129 txt = self.edit.GetValue()
130 txt = txt + val
131 self.edit.SetValue(txt)
132
133 def OnBtn2Button(self, event):
134 val = '2'
135 txt = self.edit.GetValue()
136 txt = txt + val
137 self.edit.SetValue(txt)
138
139 def OnBtn3Button(self, event):
140 val = '3'
141 txt = self.edit.GetValue()
142 txt = txt + val
143 self.edit.SetValue(txt)
144
145 def OnBtn4Button(self, event):
146 val = '4'
147 txt = self.edit.GetValue()
148 txt = txt + val
149 self.edit.SetValue(txt)
150
151 def OnBtn5Button(self, event):
152 val = '5'
153 txt = self.edit.GetValue()
154 txt = txt + val
155 self.edit.SetValue(txt)
156
157 def OnBtn6Button(self, event):
158 val = '6'
159 txt = self.edit.GetValue()
160 txt = txt + val
161 self.edit.SetValue(txt)
162
163 def OnBtn7Button(self, event):
164 val = '7'
165 txt = self.edit.GetValue()
166 txt = txt + val
167 self.edit.SetValue(txt)
168
169 def OnBtn8Button(self, event):
170 val = '8'
171 txt = self.edit.GetValue()
172 txt = txt + val
173 self.edit.SetValue(txt)
174
175 def OnBtn9Button(self, event):
176 val = '9'
177 txt = self.edit.GetValue()
178 txt = txt + val
179 self.edit.SetValue(txt)
180
181 def OnBtnDotButton(self, event):
182 val = '.'
183 txt = self.edit.GetValue()
184 txt = txt + val
185 self.edit.SetValue(txt)
186
187 def OnBtnEqualButton(self, event):
188 txt = self.edit.GetValue()
189
190
191 if '/' in txt:
192 if '.' not in txt:
193 txt = txt + '.0'
194
195 txt = repr(eval(txt))
196
197 self.edit.SetValue(txt)
198
199 def OnBtnPlusButton(self, event):
200 val = '+'
201 txt = self.edit.GetValue()
202 txt = txt + val
203 self.edit.SetValue(txt)
204
205 def OnBtnMinusButton(self, event):
206 val = '-'
207 txt = self.edit.GetValue()
208 txt = txt + val
209 self.edit.SetValue(txt)
210
211 def OnBtnMultiButton(self, event):
212 val = '*'
213 txt = self.edit.GetValue()
214 txt = txt + val
215 self.edit.SetValue(txt)
216
217 def OnBtnDivButton(self, event):
218 val = '/'
219 txt = self.edit.GetValue()
220 txt = txt + val
221 self.edit.SetValue(txt)
222
223 def OnBtnClearButton(self, event):
224 self.edit.SetValue('')
225
226
227
228 def create(parent):
229 return wxFrame1(parent)
230
231 class BoaApp(wxApp):
232 def OnInit(self):
233 wxInitAllImageHandlers()
234 self.main = create(None)
235 self.main.Show()
236 self.SetTopWindow(self.main)
237 return True
238
239 def main():
240 application = BoaApp(0)
241 application.MainLoop()
242
243 if __name__ == '__main__':
244 main()