内容大致分为两个部分
前面是一个页面的布局 和组件的添加
后面是功能函数
整体来说很简单,可能还有bug
新手,写的代码比较繁琐,还望大佬多多指教
wx不懂的可以看下
wx基础
对于计算的核心是想办法维护两个变量
一个 first_num
和 second_num
在加些限制条件防止出现错误
里面的 i j k end
作为辅助变量 防止出现非人类显示和操作
i 是标记是否为第一次运算, 因为初始是first_num = ‘’,在之后我就讲结果赋值给first_num
所以对于第一个first _num 的维护 要单算一下
j 是防止 运算符号出现后仍然出现运算符号
k 的话是防止运算过程中 first_num
和 second_num出现错误
整体逻辑比较繁琐,乱。
计算思路希望可以帮到大家。
import wx
class client(wx.Frame):
def __init__(self, parent=None, id=-1, title='计算器', pos= (200,200), size=(340,550)):
wx.Frame.__init__(self,parent,id,title,pos,size)
# 定义套接字
pl = wx.Panel(self)
# 设置垂直尺寸器 用于定义组件分布
con = wx.BoxSizer(wx.VERTICAL)
# 定义按钮
self.add = wx.Button(pl, size=(80, 50), label='+')
self.sub = wx.Button(pl, size=(80, 50), label='-')
self.mul = wx.Button(pl, size=(80, 50), label='*')
self.div = wx.Button(pl, size=(80, 50), label='/')
self.mod = wx.Button(pl, size=(80, 50), label='%')
self.sqr = wx.Button(pl, size=(80, 50), label='sqr')
self.clear = wx.Button(pl, size=(80, 50), label='C')
self.clear_now = wx.Button(pl, size=(80, 50), label='CE')
self.square = wx.Button(pl, size=(80, 50), label='x²')
self.turn_back = wx.Button(pl, size=(80, 50), label='1 / x')
self.div_c = wx.Button(pl, size=(80, 50), label='//')
self.equla = wx.Button(pl, size=(80, 50), label='=')
self.dot = wx.Button(pl, size=(80, 50), label='.')
self.one = wx.Button(pl, size=(80, 50), label='1')
self.two = wx.Button(pl, size=(80, 50), label='2')
self.three = wx.Button(pl, size=(80, 50), label='3')
self.four = wx.Button(pl, size=(80, 50), label='4')
self.five = wx.Button(pl, size=(80, 50), label='5')
self.six = wx.Button(pl, size=(80, 50), label='6')
self.seven = wx.Button(pl, size=(80, 50), label='7')
self. eight = wx.Button(pl, size=(80, 50), label='8')
self.nine = wx.Button(pl, size=(80, 50), label='9')
self.zero = wx.Button(pl, size=(80, 50), label='0')
self.pow = wx.Button(pl, size=(80, 50), label='^')
self.Text1 = wx.TextCtrl(pl, size=(320, 50),style = wx.TE_MULTILINE | wx.TE_READONLY)
self.Text2 = wx.TextCtrl(pl, size=(320, 150), style = wx.TE_READONLY | wx.TE_RIGHT | wx.TE_MULTILINE,value = '\n\n0')
font = wx.Font(30, wx.MODERN, wx.NORMAL, wx.NORMAL, False, 'Consolas')
self.Text2.SetFont(font)
# 设置 无语i
self.i = 1
self.j = 1
self.k = 1
'''测试'''
self.Bind(wx.EVT_BUTTON, self.rp1, self.one)
self.Bind(wx.EVT_BUTTON, self.rp2, self.two)
self.Bind(wx.EVT_BUTTON, self.rp3, self.three)
self.Bind(wx.EVT_BUTTON, self.rp4, self.four)
self.Bind(wx.EVT_BUTTON, self.rp5, self.five)
self.Bind(wx.EVT_BUTTON, self.rp6, self.six)
self.Bind(wx.EVT_BUTTON, self.rp7, self.seven)
self.Bind(wx.EVT_BUTTON, self.rp8, self. eight)
self.Bind(wx.EVT_BUTTON, self.rp9, self.nine)
self.Bind(wx.EVT_BUTTON, self.rp0, self.zero)
self.Bind(wx.EVT_BUTTON, self.rp_mul, self.mul)
self.Bind(wx.EVT_BUTTON, self.rp_div, self.div)
self.Bind(wx.EVT_BUTTON, self.rp_sub, self.sub)
self.Bind(wx.EVT_BUTTON, self.rp_add, self.add)
self.Bind(wx.EVT_BUTTON, self.rp_mod, self.mod)
self.Bind(wx.EVT_BUTTON, self.rp_pow, self.pow)
self.Bind(wx.EVT_BUTTON, self.clear_all, self.clear)
self.Bind(wx.EVT_BUTTON, self.div_com, self.div_c)
self.Bind(wx.EVT_BUTTON, self.equl_cnt, self.equla)
self.Bind(wx.EVT_BUTTON, self.rp_dot, self.dot)
self.Bind(wx.EVT_BUTTON, self.squ, self.square)
self.Bind(wx.EVT_BUTTON, self.sqrt, self.sqr)
self.Bind(wx.EVT_BUTTON, self.CE, self.clear_now)
self.Bind(wx.EVT_BUTTON, self.turn_b, self.turn_back)
# 定义水平尺寸布局
subcon1 = wx.BoxSizer(wx.HORIZONTAL)
subcon2 = wx.BoxSizer(wx.HORIZONTAL)
subcon3 = wx.BoxSizer(wx.HORIZONTAL)
subcon4 = wx.BoxSizer(wx.HORIZONTAL)
subcon5 = wx.BoxSizer(wx.HORIZONTAL)
subcon6 = wx.BoxSizer(wx.HORIZONTAL)
subcon7 = wx.BoxSizer(wx.HORIZONTAL)
subcon8 = wx.BoxSizer(wx.HORIZONTAL)
# 添加组件
subcon1.Add(self.mod)
subcon1.Add(self.clear_now)
subcon1.Add(self.clear)
subcon1.Add(self.div_c)
subcon2.Add(self.turn_back)
subcon2.Add(self.square)
subcon2.Add(self.sqr)
subcon2.Add(self.div)
subcon3.Add(self.seven)
subcon3.Add(self.eight)
subcon3.Add(self.nine)
subcon3.Add(self.mul)
subcon4.Add(self.four)
subcon4.Add(self.five)
subcon4.Add(self.six)
subcon4.Add(self.sub)
subcon5.Add(self.one)
subcon5.Add(self.two)
subcon5.Add(self.three)
subcon5.Add(self.add)
subcon6.Add(self.pow)
subcon6.Add(self.zero)
subcon6.Add(self.dot)
subcon6.Add(self.equla)
subcon7.Add(self.Text1)
subcon8.Add(self.Text2)
con.Add(subcon7)
con.Add(subcon8)
con.Add(subcon1)
con.Add(subcon2)
con.Add(subcon3)
con.Add(subcon4)
con.Add(subcon5)
con.Add(subcon6)
pl.SetSizer(con)
'''窗口'''
# 定义计算时用到的三个个字符串
self.first_num = ''
self.second_num = ''
self.ans_num = None
self.flag_cut = ''
self.end = False
'''测试'''
def rp1(self, event):
if self.i == 1:
self.first_num += '1'
ans_1 = self.Text1.GetValue()
if ans_1 != '':
if self.Text2.GetValue() != '\n\n0' and self.k == 1:
self.Text2.AppendText(self.one.Label)
if self.end == True:
self.Text1.Clear()
self.end = False
self.Text2.SetValue('\n\n' + self.one.Label)
self.k = 1
if self.k == 2:
self.Text2.SetValue('\n\n' + self.one.Label)
self.k = 1
if ans_1[-1] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'] or self.j == 2:
self.second_num += '1'
self.j = 2
else:
if self.Text2.GetValue() == '\n\n0':
self.Text2.SetValue('\n\n' + self.one.Label)
else:
self.Text2.AppendText(self.one.Label)
def rp2(self, event):
if self.first_num == '':
self.first_num += '2'
elif self.i == 1:
self.first_num += '2'
ans_1 = self.Text1.GetValue()
if ans_1 != '':
if self.Text2.GetValue() != '\n\n0' and self.k == 1:
self.Text2.AppendText(self.two.Label)
if self.end == True:
self.Text1.Clear()
self.end = False
self.Text2.SetValue('\n\n' + self.two.Label)
self.k = 1
if self.k == 2:
self.Text2.SetValue('\n\n' + self.two.Label)
self.k = 1
if ans_1[-1] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'] or self.j == 2:
self.second_num += '2'
self.j = 2
else:
# self.Text1.AppendText(self.two.Label)
if self.Text2.GetValue() == '\n\n0':
self.Text2.SetValue('\n\n' + self.two.Label)
else:
self.Text2.AppendText(self.two.Label)
def rp3(self, event):
if self.first_num == '':
self.first_num += '3'
elif self.i == 1:
self.first_num += '3'
ans_1 = self.Text1.GetValue()
if ans_1 != '':
if ans_1 != '':
if self.Text2.GetValue() != '\n\n0' and self.k == 1:
self.Text2.AppendText(self.three.Label)
if self.end == True:
self.Text1.Clear()
self.end = False
self.Text2.SetValue('\n\n' + self.three.Label)
self.k = 1
if self.k == 2:
self.Text2.SetValue('\n\n' + self.three.Label)
self.k = 1
if ans_1[-1] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'] or self.j == 2:
self.second_num += '3'
self.j = 2
else:
if self.Text2.GetValue() == '\n\n0':
self.Text2.SetValue('\n\n' + self.three.Label)
else:
self.Text2.AppendText(self.three.Label)
def rp4(self, event):
if self.first_num == '':
self.first_num += '4'
elif self.i == 1:
self.first_num += '4'
ans_1 = self.Text1.GetValue()
if ans_1 != '':
if ans_1 != '':
if self.Text2.GetValue() != '\n\n0' and self.k == 1:
self.Text2.AppendText(self.four.Label)
if self.end == True:
self.Text1.Clear()
self.end = False
self.Text2.SetValue('\n\n' + self.four.Label)
self.k = 1
if self.k == 2:
self.Text2.SetValue('\n\n' + self.four.Label)
self.k = 1
if ans_1[-1] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'] or self.j == 2:
self.second_num += '4'
self.j = 2
else:
# self.Text1.AppendText(self.four.Label)
if self.Text2.GetValue() == '\n\n0':
self.Text2.SetValue('\n\n' + self.four.Label)
else:
self.Text2.AppendText(self.four.Label)
def rp5(self, event):
if self.first_num == '':
self.first_num += '5'
elif self.i == 1:
self.first_num += '5'
ans_1 = self.Text1.GetValue()
if ans_1 != '':
if self.Text2.GetValue() != '\n\n0' and self.k == 1:
self.Text2.AppendText(self.five.Label)
if self.end == True:
self.Text1.Clear()
self.end = False
self.Text2.SetValue('\n\n' + self.five.Label)
self.k = 1
if self.k == 2:
self.Text2.SetValue('\n\n' + self.five.Label)
self.k = 1
if ans_1[-1] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'] or self.j == 2:
self.second_num += '5'
self.j = 2
else:
if self.Text2.GetValue() == '\n\n0':
self.Text2.SetValue('\n\n' + self.five.Label)
else:
self.Text2.AppendText(self.five.Label)
def rp6(self, event):
if self.first_num == '':
self.first_num += '6'
elif self.i == 1:
self.first_num += '6'
ans_1 = self.Text1.GetValue()
if ans_1 != '':
if ans_1 != '':
if self.Text2.GetValue() != '\n\n0' and self.k == 1:
self.Text2.AppendText(self.six.Label)
if self.end == True:
self.Text1.Clear()
self.end = False
self.Text2.SetValue('\n\n' + self.six.Label)
self.k = 1
if self.k == 2:
self.Text2.SetValue('\n\n' + self.six.Label)
self.k = 1
if ans_1[-1] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'] or self.j == 2:
self.second_num += '6'
self.j = 2
else:
# self.Text1.AppendText(self.six.Label)
if self.Text2.GetValue() == '\n\n0':
self.Text2.SetValue('\n\n' + self.six.Label)
else:
self.Text2.AppendText(self.six.Label)
def rp7(self, event):
if self.first_num == '':
self.first_num += '7'
elif self.i == 1:
self.first_num += '7'
ans_1 = self.Text1.GetValue()
if ans_1 != '':
if self.Text2.GetValue() != '\n\n0' and self.k == 1:
self.Text2.AppendText(self.seven.Label)
if self.end == True:
self.Text1.Clear()
self.end = False
self.Text2.SetValue('\n\n' + self.seven.Label)
self.k = 1
if self.k == 2:
self.Text2.SetValue('\n\n' + self.seven.Label)
self.k = 1
if ans_1[-1] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'] or self.j == 2:
self.second_num += '7'
self.j = 2
else:
if self.Text2.GetValue() == '\n\n0':
self.Text2.SetValue('\n\n' + self.seven.Label)
else:
self.Text2.AppendText(self.seven.Label)
def rp8(self, event):
if self.first_num == '':
self.first_num += '8'
elif self.i == 1:
self.first_num += '8'
ans_1 = self.Text1.GetValue()
if ans_1 != '':
if self.Text2.GetValue() != '\n\n0' and self.k == 1:
self.Text2.AppendText(self.eight.Label)
if self.end == True:
self.Text1.Clear()
self.end = False
self.Text2.SetValue('\n\n' + self.eight.Label)
self.k = 1
if self.k == 2:
self.Text2.SetValue('\n\n' + self.eight.Label)
self.k = 1
if ans_1[-1] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'] or self.j == 2:
self.second_num += '8'
self.j = 2
else:
# self.Text1.AppendText(self.eight.Label)
if self.Text2.GetValue() == '\n\n0':
self.Text2.SetValue('\n\n' + self.eight.Label)
else:
self.Text2.AppendText(self.eight.Label)
def rp9(self, event):
if self.first_num == '':
self.first_num += '9'
elif self.i == 1:
self.first_num += '9'
ans_1 = self.Text1.GetValue()
if ans_1 != '':
if self.Text2.GetValue() != '\n\n0' and self.k == 1:
self.Text2.AppendText(self.nine.Label)
if self.end == True:
self.Text1.Clear()
self.end = False
self.Text2.SetValue('\n\n' + self.nine.Label)
self.k = 1
if self.k == 2:
self.Text2.SetValue('\n\n' + self.nine.Label)
self.k = 1
if ans_1[-1] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'] or self.j == 2:
self.second_num += '9'
self.j = 2
else:
if self.Text2.GetValue() == '\n\n0':
self.Text2.SetValue('\n\n' + self.nine.Label)
else:
self.Text2.AppendText(self.nine.Label)
def rp0(self, event):
if self.first_num == '':
self.first_num += '0'
elif self.i == 1:
self.first_num += '0'
ans_1 = self.Text1.GetValue()
if ans_1 != '':
if self.Text2.GetValue() != '\n\n0' and self.k == 1:
self.Text2.AppendText(self.zero.Label)
if self.end == True:
self.Text1.Clear()
self.end = False
self.Text2.SetValue('\n\n' + self.zero.Label)
self.k = 1
if self.k == 2:
self.Text2.SetValue('\n\n' + self.zero.Label)
self.k = 1
if ans_1[-1] not in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'] or self.j == 2:
self.second_num += '0'
self.j = 2
else:
if self.Text2.GetValue() == '\n\n0':
self.Text2.SetValue('\n\n' + self.zero.Label)
else:
self.Text2.AppendText(self.zero.Label)
def rp_dot(self , event):
'''小数'''
if self.first_num != '' and self.i == 1 and self.first_num.find('.') == -1:
self.Text2.AppendText('.')
self.first_num += '.'
if self.second_num != '' and self.second_num.find('.') == -1:
self.Text2.AppendText('.')
self.second_num += '.'
def rp_mul(self, event):
result = self.Text1.GetValue()
if self.end == True:
return
if result != '':
if result[-1] in ['+', '-', '*', '/', '%', '^'] and self.second_num == '':
self.Text1.SetValue(result[0:-1] + "+")
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.mul.Label)
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.mul.Label)
self.i = 2
self.k = 2
if self.second_num != '':
self.calculate()
self.j = 1
self.first_num = self.ans_num
self.second_num = ''
self.Text2.SetValue("\n\n" + self.ans_num)
self.ans_num = ''
self.flag_cut = '*'
else:
self.flag_cut = '*'
'''测试'''
def rp_add(self, event):
result = self.Text1.GetValue()
if self.end == True:
return
if result != '':
if result[-1] in ['+', '-', '*', '/', '%', '^'] and self.second_num == '':
self.Text1.SetValue(result[0:-1] + "+")
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.add.Label)
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.add.Label)
self.i = 2
self.k = 2
if self.second_num != '':
self.calculate()
self.j = 1
self.first_num = self.ans_num
self.second_num = ''
self.Text2.SetValue("\n\n" + self.ans_num)
self.ans_num = ''
self.flag_cut = '+'
else:
self.flag_cut = '+'
def rp_sub(self, event):
result = self.Text1.GetValue()
if self.end == True:
return
if result != '':
if result[-1] in ['+', '-', '*', '/', '%', '^']:
self.Text1.SetValue(result[0:-1] + "-")
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.sub.Label)
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.sub.Label)
self.i = 2
self.k = 2
if self.second_num != '':
self.calculate()
self.j = 1
self.first_num = self.ans_num
self.second_num = ''
self.Text2.SetValue("\n\n" + self.ans_num)
self.ans_num = ''
self.flag_cut = '-'
else:
self.flag_cut = '-'
def rp_div(self, event):
result = self.Text1.GetValue()
if self.end == True:
return
if result != '':
if result[-1] in ['+', '-', '*', '/', '%', '^'] and self.second_num == '':
self.Text1.SetValue(result[0:-1] + "/")
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.div.Label)
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.div.Label)
self.i = 2
self.k = 2
if self.second_num != '':
self.calculate()
self.j = 1
self.first_num = self.ans_num
self.second_num = ''
self.Text2.SetValue("\n\n" + self.ans_num)
self.ans_num = ''
self.flag_cut = '/'
else:
self.flag_cut = '/'
def rp_mod(self, event):
result = self.Text1.GetValue()
if self.end == True:
return
if result != '':
if result[-1] in ['+', '-', '*', '/', '%', '^'] and self.second_num == '':
self.Text1.SetValue(result[0:-1] + "%")
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.mod.Label)
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.mod.Label)
self.i = 2
self.k = 2
if self.second_num != '':
self.calculate()
self.j = 1
self.first_num = self.ans_num
self.second_num = ''
self.Text2.SetValue("\n\n" + self.ans_num)
self.ans_num = ''
self.flag_cut = '%'
else:
self.flag_cut = '%'
def rp_pow(self, event):
result = self.Text1.GetValue()
if self.end == True:
return
if result != '':
if result[-1] in ['+', '-', '*', '/', '%', '^'] and self.second_num == '':
self.Text1.SetValue(result[0:-1] + "^")
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.pow.Label)
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.pow.Label)
self.i = 2
self.k = 2
if self.second_num != '':
self.calculate()
self.j = 1
self.first_num = self.ans_num
self.second_num = ''
self.Text2.SetValue("\n\n" + self.ans_num)
self.ans_num = ''
self.flag_cut = '^'
else:
self.flag_cut = '^'
def clear_all(self, event):
self.Text2.SetValue("\n\n0")
self.Text1.Clear()
self.first_num = ''
self.second_num = ''
self.i = 1
self.j = 1
self.flag_cut = ''
self.end = False
def equl_cnt(self ,event):
if self.second_num != '':
self.calculate()
self.j = 1
self.i = 1
self.first_num = ''
self.second_num = ''
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + '=' + self.ans_num)
self.Text2.SetValue("\n\n" + self.ans_num)
self.ans_num = ''
self.flag_cut = ''
self.end = True
else:
return
def div_com(self, event):
result = self.Text1.GetValue()
if self.end == True:
return
if result != '':
if result[-1] in ['+', '-', '*', '/', '%', '^'] and self.second_num == '':
self.Text1.SetValue(result[0:-1] + "//")
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.div_c.Label)
else:
re2 = self.Text2.GetValue()
self.Text1.AppendText(re2.strip('\n') + self.div_c.Label)
self.i = 2
self.k = 2
if self.second_num != '':
self.calculate()
self.j = 1
self.first_num = self.ans_num
self.second_num = ''
self.Text2.SetValue("\n\n" + self.ans_num)
self.ans_num = ''
self.flag_cut = '//'
else:
self.flag_cut = '//'
def calculate(self):
'''测试'''
if self.flag_cut == '*':
self.ans_num = str(float(self.first_num) * float(self.second_num))
if self.flag_cut == '/':
self.ans_num = str(float(self.first_num) / float(self.second_num))
if self.flag_cut == '+':
self.ans_num = str(float(self.first_num) + float(self.second_num))
if self.flag_cut == '-':
self.ans_num = str(float(self.first_num) - float(self.second_num))
if self.flag_cut == '%':
self.ans_num = str(float(self.first_num) % float(self.second_num))
if self.flag_cut == '^':
self.ans_num = str(pow(float(self.first_num), float(self.second_num)))
if self.flag_cut == '//':
self.ans_num = str(float(self.first_num) // float(self.second_num))
def CE(self, event):
if self.end == False:
self.Text2.Clear()
self.second_num = ''
else:
self.clear_all(event)
def squ(self, event):
if self.first_num == '':
return
else:
ans = str(pow(float(self.first_num),2))
self.Text2.SetValue('\n\n' + ans)
self.Text1.SetValue(ans)
self.end = True
def turn_b(self, event):
if self.first_num == '':
return
else:
ans = str(pow(float(self.first_num),-1))
self.Text2.SetValue('\n\n' + ans)
self.Text1.SetValue(ans)
self.end = True
def sqrt(self, event):
if self.first_num == '':
return
else:
ans = str(pow(float(self.first_num),0.5))
self.Text2.SetValue('\n\n' + ans)
self.Text1.SetValue(ans)
self.end = True
if __name__ == '__main__':
app = wx.App()
client().Show()
app.MainLoop()