代码拉取完成,页面将自动刷新
import wx
from math import *
class CalcFrame(wx.Frame):
def __init__(self, title):
super(CalcFrame, self).__init__(None, title=title, size=(300, 250))
self.InitUI()
self.Centre()
self.Show()
def InitUI(self):
# 定义一个BoxSizer来放置TextCtrl文本框
vbox = wx.BoxSizer(wx.VERTICAL)
self.textprint = wx.TextCtrl(self, -1, '', style = wx.TE_RIGHT|wx.TE_READONLY) # 不能让键盘输入, 只能点击按钮进行输入
self.equation = ''
vbox.Add(self.textprint, flag = wx.EXPAND|wx.TOP|wx.BOTTOM, border = 4)
# 在放置一个 GridSizer 来防止按钮
gridBox = wx.GridSizer(5, 4, 5, 5)
labels = ['AC', 'DEL', 'pi', 'CLOSE', '7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=', '+']
for label in labels:
buttonItem = wx.Button(self, label = label)
self.createHandler(buttonItem, label) # 点击不同的按钮, 选择不同的回调
gridBox.Add(buttonItem, 1, wx.EXPAND)
vbox.Add(gridBox, proportion = 1, flag = wx.EXPAND)
self.SetSizer(vbox)
# 创建按钮处理方法
def createHandler(self, button, labels):
item = "DEL AC = CLOSE"
if labels not in item:
self.Bind(wx.EVT_BUTTON, self.OnAppend, button)
elif labels == 'DEL':
self.Bind(wx.EVT_BUTTON, self.OnDel, button)
elif labels == 'AC':
self.Bind(wx.EVT_BUTTON, self.OnAc, button)
elif labels == '=':
self.Bind(wx.EVT_BUTTON, self.OnTarget, button)
elif labels == 'CLOSE':
self.Bind(wx.EVT_BUTTON, self.OnExit, button)
# 添加运算符和数字
def OnAppend(self, event):
eventbutton = event.GetEventObject()
label = eventbutton.GetLabel()
self.equation += label
self.textprint.SetValue(self.equation)
def OnDel(self, event):
self.equation = self.equation[:-1]
self.textprint.SetValue(self.equation)
def OnAc(self, event):
self.textprint.Clear()
self.equation=""
def OnTarget(self, event):
string = self.equation
try:
target = eval(string)
self.equation = str(target)
self.textprint.SetValue(self.equation)
except SyntaxError:
dlg = wx.MessageDialog(self, u'格式错误, 请输入正确的等式!', u'请注意', wx.OK|wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def OnExit(self, event):
self.Close()
if __name__ == "__main__":
app = wx.App()
CalcFrame(title = '无敌计算器')
app.MainLoop()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。