本文实例为大家分享了wxPython实现计算器的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# - * - coding: utf - 8 - * -
##########################################
## Python code generated with wxFormBuilder (version Feb 16 2016)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
##########################################
import wx
import wx.xrc
import math
#############################################
## Class MyFrame1
#############################################
class MyFrame1(wx.Frame):
def __init__( self , parent):
wx.Frame.__init__( self , parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition,
size = wx.Size( 486 , 448 ), style = wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self .SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
bSizer1 = wx.BoxSizer(wx.VERTICAL)
self .m_textCtrl1 = wx.TextCtrl( self , wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 600 , 60 ), style = wx.TE_RIGHT)
self .m_textCtrl1.SetMinSize(wx.Size( 470 , 60 ))
bSizer1.Add( self .m_textCtrl1, 0 , wx. ALL , 5 )
bSizer2 = wx.BoxSizer(wx.HORIZONTAL)
self .m_button1 = wx.Button( self , wx.ID_ANY, u "退格" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer2.Add( self .m_button1, 0 , wx. ALL , 5 )
self .m_button2 = wx.Button( self , wx.ID_ANY, u "清屏" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer2.Add( self .m_button2, 0 , wx. ALL , 5 )
self .m_button3 = wx.Button( self , wx.ID_ANY, u "sqrt" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer2.Add( self .m_button3, 0 , wx. ALL , 5 )
self .m_button4 = wx.Button( self , wx.ID_ANY, u "/" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer2.Add( self .m_button4, 0 , wx. ALL , 5 )
bSizer1.Add(bSizer2, 0 , wx.EXPAND, 5 )
bSizer6 = wx.BoxSizer(wx.HORIZONTAL)
self .m_button10 = wx.Button( self , wx.ID_ANY, u "7" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer6.Add( self .m_button10, 0 , wx. ALL , 5 )
self .m_button11 = wx.Button( self , wx.ID_ANY, u "8" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer6.Add( self .m_button11, 0 , wx. ALL , 5 )
self .m_button12 = wx.Button( self , wx.ID_ANY, u "9" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer6.Add( self .m_button12, 0 , wx. ALL , 5 )
self .m_button13 = wx.Button( self , wx.ID_ANY, u "*" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer6.Add( self .m_button13, 0 , wx. ALL , 5 )
bSizer1.Add(bSizer6, 0 , wx.EXPAND, 5 )
bSizer7 = wx.BoxSizer(wx.HORIZONTAL)
self .m_button15 = wx.Button( self , wx.ID_ANY, u "4" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer7.Add( self .m_button15, 0 , wx. ALL , 5 )
self .m_button16 = wx.Button( self , wx.ID_ANY, u "5" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer7.Add( self .m_button16, 0 , wx. ALL , 5 )
self .m_button17 = wx.Button( self , wx.ID_ANY, u "6" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer7.Add( self .m_button17, 0 , wx. ALL , 5 )
self .m_button18 = wx.Button( self , wx.ID_ANY, u "-" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer7.Add( self .m_button18, 0 , wx. ALL , 5 )
bSizer1.Add(bSizer7, 0 , wx.EXPAND, 5 )
bSizer34 = wx.BoxSizer(wx.HORIZONTAL)
self .m_button140 = wx.Button( self , wx.ID_ANY, u "1" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer34.Add( self .m_button140, 0 , wx. ALL , 5 )
self .m_button141 = wx.Button( self , wx.ID_ANY, u "2" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer34.Add( self .m_button141, 0 , wx. ALL , 5 )
self .m_button142 = wx.Button( self , wx.ID_ANY, u "3" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer34.Add( self .m_button142, 0 , wx. ALL , 5 )
self .m_button143 = wx.Button( self , wx.ID_ANY, u "+" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer34.Add( self .m_button143, 0 , wx. ALL , 5 )
bSizer1.Add(bSizer34, 0 , wx.EXPAND, 5 )
bSizer35 = wx.BoxSizer(wx.HORIZONTAL)
self .m_button145 = wx.Button( self , wx.ID_ANY, u "0" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer35.Add( self .m_button145, 0 , wx. ALL , 5 )
self .m_button148 = wx.Button( self , wx.ID_ANY, u "." , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer35.Add( self .m_button148, 0 , wx. ALL , 5 )
self .m_button149 = wx.Button( self , wx.ID_ANY, u "+/-" , wx.DefaultPosition, wx.Size( 110 , 60 ), 0 )
bSizer35.Add( self .m_button149, 0 , wx. ALL , 5 )
self .m_button150 = wx.Button( self , wx.ID_ANY, u "=" , wx.DefaultPosition, wx.Size( 110 , 600 ), 0 )
self .m_button150.SetMinSize(wx.Size( 110 , 60 ))
bSizer35.Add( self .m_button150, 0 , wx. ALL , 5 )
bSizer1.Add(bSizer35, 0 , wx.EXPAND, 5 )
self .SetSizer(bSizer1)
self .Layout()
self .Centre(wx.BOTH)
# Connect Events
self .m_button1.Bind(wx.EVT_BUTTON, self .m_button1OnButtonClick)
self .m_button2.Bind(wx.EVT_BUTTON, self .m_button2OnButtonClick)
self .m_button3.Bind(wx.EVT_BUTTON, self .m_button3OnButtonClick)
self .m_button4.Bind(wx.EVT_BUTTON, self .m_button4OnButtonClick)
self .m_button10.Bind(wx.EVT_BUTTON, self .m_button10OnButtonClick)
self .m_button11.Bind(wx.EVT_BUTTON, self .m_button11OnButtonClick)
self .m_button12.Bind(wx.EVT_BUTTON, self .m_button12OnButtonClick)
self .m_button13.Bind(wx.EVT_BUTTON, self .m_button13OnButtonClick)
self .m_button15.Bind(wx.EVT_BUTTON, self .m_button15OnButtonClick)
self .m_button16.Bind(wx.EVT_BUTTON, self .m_button16OnButtonClick)
self .m_button17.Bind(wx.EVT_BUTTON, self .m_button17OnButtonClick)
self .m_button18.Bind(wx.EVT_BUTTON, self .m_button18OnButtonClick)
self .m_button140.Bind(wx.EVT_BUTTON, self .m_button140OnButtonClick)
self .m_button141.Bind(wx.EVT_BUTTON, self .m_button141OnButtonClick)
self .m_button142.Bind(wx.EVT_BUTTON, self .m_button142OnButtonClick)
self .m_button143.Bind(wx.EVT_BUTTON, self .m_button143OnButtonClick)
self .m_button145.Bind(wx.EVT_BUTTON, self .m_button145OnButtonClick)
self .m_button148.Bind(wx.EVT_BUTTON, self .m_button148OnButtonClick)
self .m_button149.Bind(wx.EVT_BUTTON, self .m_button149OnButtonClick)
self .m_button150.Bind(wx.EVT_BUTTON, self .m_button150OnButtonClick)
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def m_button1OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result[: - 1 ]
self .m_textCtrl1.SetValue(result)
def m_button2OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = ''
self .m_textCtrl1.SetValue(result)
def m_button3OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = int (result)
result = math.sqrt(result)
self .m_textCtrl1.SetValue( str (result))
def m_button4OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '/'
self .m_textCtrl1.SetValue(result)
def m_button10OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '7'
self .m_textCtrl1.SetValue(result)
def m_button11OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '8'
self .m_textCtrl1.SetValue(result)
def m_button12OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '9'
self .m_textCtrl1.SetValue(result)
def m_button13OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '*'
self .m_textCtrl1.SetValue(result)
def m_button15OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '4'
self .m_textCtrl1.SetValue(result)
def m_button16OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '5'
self .m_textCtrl1.SetValue(result)
def m_button17OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '6'
self .m_textCtrl1.SetValue(result)
def m_button18OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '-'
self .m_textCtrl1.SetValue(result)
def m_button140OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '1'
self .m_textCtrl1.SetValue(result)
def m_button141OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '2'
self .m_textCtrl1.SetValue(result)
def m_button142OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '3'
self .m_textCtrl1.SetValue(result)
def m_button143OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '+'
self .m_textCtrl1.SetValue(result)
def m_button145OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '0'
self .m_textCtrl1.SetValue(result)
def m_button148OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = result + '.'
self .m_textCtrl1.SetValue(result)
def m_button149OnButtonClick( self , event):
result = self .m_textCtrl1.GetValue()
result = - int (result)
self .m_textCtrl1.SetValue( str (result))
def m_button150OnButtonClick( self , event):
self .m_textCtrl1.SetValue( str ( eval ( self .m_textCtrl1.GetValue())))
app = wx.App()
window = MyFrame1( None )
window.Show( True )
app.MainLoop()
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/qq_37702781/article/details/77585036