wx计算器

来源:互联网 发布:csi犯罪现场调查网络 编辑:程序博客网 时间:2024/06/11 21:17


#!/usr/bin/env python# -*- encoding: utf-8 -*-import wxdef onclick(event):button = event.GetEventObject()label = button.GetLabel()nums = [str(i) for i in range(10)] + ['.', '+', '-', 'x', u'÷']# print repr(label)if label in nums:xyz.AppendText(label)elif label == 'clear':xyz.SetValue('')def resul(event):x = xyz.GetValue()x = x.replace('x', '*').replace(u'÷', '/')if '.' not in x:x = x + '.0'# print repr(x)if x:xyz.SetValue(str(eval(x, {"__builtins__": None}, {})))if __name__ == '__main__':app = wx.App()win = wx.Frame(None, title='Simple BC', size=(325, 320))bkg = wx.Panel(win)xyz = wx.TextCtrl(bkg)result_button = wx.Button(bkg, label='=')hbox = wx.BoxSizer()hbox.Add(xyz, proportion=3, flag=wx.EXPAND)hbox.Add(result_button, proportion=1, flag=wx.EXPAND, border=5)xbox = wx.GridSizer(4,4,4,4)buttons = ['7', '8', '9', u'÷', '4', '5', '6', 'x', '1', '2',   '3', '-', '.', '0', 'clear', '+']for x in buttons:button = wx.Button(bkg, label=x) xbox.Add(button, proportion=1, flag=wx.EXPAND, border=5)button.Bind(wx.EVT_BUTTON, onclick)vbox = wx.BoxSizer(wx.VERTICAL)vbox.Add(hbox, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)vbox.Add(xbox, proportion=4, flag=wx.EXPAND|wx.ALL, border=5)bkg.SetSizer(vbox)result_button.Bind(wx.EVT_BUTTON, resul)win.Show()app.MainLoop()


原创粉丝点击