1 Star 0 Fork 0

mr_nobody/python_wx2048

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
calculator.py 2.62 KB
一键复制 编辑 原始数据 按行查看 历史
mr_nobody 提交于 2017-03-17 16:39 . 完成计算器, 并打包成exe文件
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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/mr_nobody/python_wx2048.git
[email protected]:mr_nobody/python_wx2048.git
mr_nobody
python_wx2048
python_wx2048
master

搜索帮助