博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python体验(10)-图形界面之计算器
阅读量:6709 次
发布时间:2019-06-25

本文共 8206 字,大约阅读时间需要 27 分钟。

 

 

1 import wx  2 class Form(wx.Frame):  3     def __init__( self, parent, id, title ):  4         wx.Frame.__init__(self,parent,id,title,wx.DefaultPosition,wx.Size(300, 250))  5         self.formula = False  6         menuBar = wx.MenuBar()  7         mnuFile = wx.Menu()  8         mnuFile.Append( 22, '&Quit', 'Exit Calculator' )  9         menuBar.Append( mnuFile, '&File' ) 10         wx.EVT_MENU( self, 22, self.OnClose ) 11         self.SetMenuBar( menuBar ) 12  13         self.display = wx.TextCtrl(self, -1, '', style=wx.TE_RIGHT) 14         gs = wx.GridSizer(5, 4, 3, 3) 15         gs.AddMany( 16             [ 17                 (wx.Button(self, 12, '-'), 0, wx.EXPAND), 18                 (wx.Button(self, 20, 'Cls'), 0, wx.EXPAND), 19                 (wx.Button(self, 21, 'Bck'), 0, wx.EXPAND), 20                 (wx.StaticText(self, -1, ''), 0, wx.EXPAND), 21                 (wx.Button(self, 22, 'Close'), 0, wx.EXPAND), 22                 (wx.Button(self, 1, '7'), 0, wx.EXPAND), 23                 (wx.Button(self, 2, '8'), 0, wx.EXPAND), 24                 (wx.Button(self, 3, '9'), 0, wx.EXPAND), 25                 (wx.Button(self, 4, '/'), 0, wx.EXPAND), 26                 (wx.Button(self, 5, '4'), 0, wx.EXPAND), 27                 (wx.Button(self, 6, '5'), 0, wx.EXPAND), 28                 (wx.Button(self, 7, '6'), 0, wx.EXPAND), 29                 (wx.Button(self, 8, '*'), 0, wx.EXPAND), 30                 (wx.Button(self, 10, '2'), 0, wx.EXPAND), 31                 (wx.Button(self, 11, '3'), 0, wx.EXPAND), 32                 (wx.Button(self, 9, '1'), 0, wx.EXPAND), 33                 (wx.Button(self, 16, '+'), 0, wx.EXPAND), 34                 (wx.Button(self, 15, '='), 0, wx.EXPAND), 35                 (wx.Button(self, 14, '.'), 0, wx.EXPAND), 36                 (wx.Button(self, 13, '0'), 0, wx.EXPAND) 37             ] 38         ) 39         sizer = wx.BoxSizer( wx.VERTICAL ) 40         sizer.Add(self.display, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 4) 41         sizer.Add(gs, 1, wx.EXPAND) 42         self.SetSizer(sizer) 43         self.Centre() 44  45         wx.EVT_BUTTON(self, 20, self.OnClear) 46         wx.EVT_BUTTON(self, 21, self.OnBackspace) 47         wx.EVT_BUTTON(self, 22, self.OnClose) 48         wx.EVT_BUTTON(self, 1, self.OnNumber) 49         wx.EVT_BUTTON(self, 2, self.OnNumber) 50         wx.EVT_BUTTON(self, 3, self.OnNumber) 51         wx.EVT_BUTTON(self, 4, self.OnFormula) 52         wx.EVT_BUTTON(self, 5, self.OnNumber) 53         wx.EVT_BUTTON(self, 6, self.OnNumber) 54         wx.EVT_BUTTON(self, 7, self.OnNumber) 55         wx.EVT_BUTTON(self, 8, self.OnFormula) 56         wx.EVT_BUTTON(self, 9, self.OnNumber) 57         wx.EVT_BUTTON(self, 10, self.OnNumber) 58         wx.EVT_BUTTON(self, 11, self.OnNumber) 59         wx.EVT_BUTTON(self, 12, self.OnFormula) 60         wx.EVT_BUTTON(self, 13, self.OnNumber) 61         wx.EVT_BUTTON(self, 14, self.OnFormula) 62         wx.EVT_BUTTON(self, 15, self.OnEqual) 63         wx.EVT_BUTTON(self, 16, self.OnFormula) 64  65     def OnClear(self, event): 66         self.display.Clear() 67     def OnBackspace(self, event): 68         formula = self.display.GetValue() 69         self.display.Clear() 70         self.display.SetValue(formula[:-1]) 71     def OnClose(self, event): 72         self.Close() 73     def OnEqual(self,event): 74         if self.formula: 75             return 76         formula = self.display.GetValue() 77         self.formula = True 78         try: 79             self.display.Clear() 80             output = eval(formula) 81             self.display.AppendText(str(output)) 82         except StandardError: 83             self.display.AppendText("Error") 84  85     def OnFormula(self,event): 86         if self.formula: 87             return 88         self.display.AppendText(event.EventObject.LabelText) 89  90     def OnNumber(self,event): 91         if self.formula: 92             self.display.Clear() 93             self.formula=False 94         self.display.AppendText(event.EventObject.LabelText) 95  96 class MyApp(wx.App): 97     def OnInit(self): 98         frame = Form(None, -1, "Phoenix Caculator") 99         frame.Show(True)100         self.SetTopWindow(frame)101         return True102 103 app = MyApp(0)104 app.MainLoop()
import wxclass Form(wx.Frame):    def __init__( self, parent, id, title ):        wx.Frame.__init__(self,parent,id,title,wx.DefaultPosition,wx.Size(300, 250))        self.formula = False        menuBar = wx.MenuBar()        mnuFile = wx.Menu()        mnuFile.Append( 22, '&Quit', 'Exit Calculator' )        menuBar.Append( mnuFile, '&File' )        wx.EVT_MENU( self, 22, self.OnClose )        self.SetMenuBar( menuBar )        self.display = wx.TextCtrl(self, -1, '', style=wx.TE_RIGHT)        gs = wx.GridSizer(5, 4, 3, 3)        gs.AddMany(            [                (wx.Button(self, 12, '-'), 0, wx.EXPAND),                (wx.Button(self, 20, 'Cls'), 0, wx.EXPAND),                (wx.Button(self, 21, 'Bck'), 0, wx.EXPAND),                (wx.StaticText(self, -1, ''), 0, wx.EXPAND),                (wx.Button(self, 22, 'Close'), 0, wx.EXPAND),                (wx.Button(self, 1, '7'), 0, wx.EXPAND),                (wx.Button(self, 2, '8'), 0, wx.EXPAND),                (wx.Button(self, 3, '9'), 0, wx.EXPAND),                (wx.Button(self, 4, '/'), 0, wx.EXPAND),                (wx.Button(self, 5, '4'), 0, wx.EXPAND),                (wx.Button(self, 6, '5'), 0, wx.EXPAND),                (wx.Button(self, 7, '6'), 0, wx.EXPAND),                (wx.Button(self, 8, '*'), 0, wx.EXPAND),                (wx.Button(self, 10, '2'), 0, wx.EXPAND),                (wx.Button(self, 11, '3'), 0, wx.EXPAND),                (wx.Button(self, 9, '1'), 0, wx.EXPAND),                (wx.Button(self, 16, '+'), 0, wx.EXPAND),                (wx.Button(self, 15, '='), 0, wx.EXPAND),                (wx.Button(self, 14, '.'), 0, wx.EXPAND),                (wx.Button(self, 13, '0'), 0, wx.EXPAND)            ]        )        sizer = wx.BoxSizer( wx.VERTICAL )        sizer.Add(self.display, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 4)        sizer.Add(gs, 1, wx.EXPAND)        self.SetSizer(sizer)        self.Centre()        wx.EVT_BUTTON(self, 20, self.OnClear)        wx.EVT_BUTTON(self, 21, self.OnBackspace)        wx.EVT_BUTTON(self, 22, self.OnClose)        wx.EVT_BUTTON(self, 1, self.OnNumber)        wx.EVT_BUTTON(self, 2, self.OnNumber)        wx.EVT_BUTTON(self, 3, self.OnNumber)        wx.EVT_BUTTON(self, 4, self.OnFormula)        wx.EVT_BUTTON(self, 5, self.OnNumber)        wx.EVT_BUTTON(self, 6, self.OnNumber)        wx.EVT_BUTTON(self, 7, self.OnNumber)        wx.EVT_BUTTON(self, 8, self.OnFormula)        wx.EVT_BUTTON(self, 9, self.OnNumber)        wx.EVT_BUTTON(self, 10, self.OnNumber)        wx.EVT_BUTTON(self, 11, self.OnNumber)        wx.EVT_BUTTON(self, 12, self.OnFormula)        wx.EVT_BUTTON(self, 13, self.OnNumber)        wx.EVT_BUTTON(self, 14, self.OnFormula)        wx.EVT_BUTTON(self, 15, self.OnEqual)        wx.EVT_BUTTON(self, 16, self.OnFormula)    def OnClear(self, event):        self.display.Clear()    def OnBackspace(self, event):        formula = self.display.GetValue()        self.display.Clear()        self.display.SetValue(formula[:-1])    def OnClose(self, event):        self.Close()    def OnEqual(self,event):        if self.formula:            return        formula = self.display.GetValue()        self.formula = True        try:            self.display.Clear()            output = eval(formula)            self.display.AppendText(str(output))        except StandardError:            self.display.AppendText("Error")    def OnFormula(self,event):        if self.formula:            return        self.display.AppendText(event.EventObject.LabelText)    def OnNumber(self,event):        if self.formula:            self.display.Clear()            self.formula=False        self.display.AppendText(event.EventObject.LabelText)class MyApp(wx.App):    def OnInit(self):        frame = Form(None, -1, "Phoenix Caculator")        frame.Show(True)        self.SetTopWindow(frame)        return Trueapp = MyApp(0)app.MainLoop()

 

转载于:https://www.cnblogs.com/flaaash/p/5300408.html

你可能感兴趣的文章
JavaScript 日期与时间戳互转
查看>>
php中ereg() ,preg_match() 与preg_match_all的区别?代码详细比较
查看>>
C#关于ref的用法(多个实参值的传递)
查看>>
19. Remove Nth Node From End of List
查看>>
OpenCV 2.2版本以上显示图片到 MFC 的 Picture Control 控件中
查看>>
关于构造方法
查看>>
很好的学习网站
查看>>
转-对指针进行初始化中的若干问题的思考与反思
查看>>
(转 部分修改) IOS 手势密码(简单版)
查看>>
关于block块使用的6点注意事项
查看>>
OpenCart 2.x设置SSL访问
查看>>
Vue项目开启步骤
查看>>
Ubuntu14.04下搜狗输入法的安装及配置
查看>>
《Objective-c》Foundation框架 -(block)
查看>>
11-22 JS中级复习
查看>>
WPF之DataGrid控件根据某列的值设置行的前景色(色
查看>>
用Jquery Widgets Factory写自己的表格控件——AFGrid(支持增、删、改)
查看>>
maven阿里云中央仓库
查看>>
[转]Linux Swap交换分区、交换文件
查看>>
JS全局屏蔽回车事件
查看>>