本文实例讲述了Python实现的简单计算器功能。分享给大家供大家参考,具体如下:
使用python编写一款简易的计算器
计算器效果图
首先搭建计算器的面板:
计算器面板结构
建造一个继承于wx.Frame
的frame,在init属性函数中搭建面板
1
2
3
4
5
6
7
8
|
class CalcFrame(wx.Frame): #建造一个继承于wx.Frame的frame def __init__( self ,title): wx.Frame.__init__ ( self , None , - 1 ,title, pos = ( 100 , 300 ),size = ( 300 , 320 )) panel = wx.Panel( self ) boxsize = wx.BoxSizer(wx.VERTICAL) gridBox = wx.GridSizer( 4 , 4 , 1 , 1 ) self .equation = "" #记录一个等式,把它print到TextCtrl中 |
建立计算机显示屏TextCtrl
1
2
3
4
5
|
self .textprint = wx.TextCtrl(panel, - 1 ,'',style = wx.TE_RIGHT|wx.TE_READONLY) #文本控制控件 self .bgFont = wx.Font( 25 ,wx.SWISS,wx.NORMAL,wx.BOLD,face = u 'Roboto' ) #设置字体样式 self .textprint.SetFont( self .bgFont) #使用SetFont将设置的字体样式运用在textprint中 self .textprint.SetForegroundColour( 'black' ) #设置前景色 self .textprint.SetBackgroundColour( 'white' ) #设置后景色 |
按钮
1
2
3
4
5
6
7
8
9
|
self .buttonData = "7 8 9 DEL 4 5 6 AC 1 2 3 * / + - =" .split() #产生buttonIterm的label #将label分配到按钮上去 buttonLength = len ( self .buttonData) for i in range (buttonLength): labels = "%s" % self .buttonData[i] buttonIterm = wx.Button(panel,i,labels) self .createHandler(buttonIterm,labels) gridBox.Add(buttonIterm, 0 ,flag = wx.EXPAND) |
将textprint与buttonIterm放入boxsize中,再将将boxsize放入panel里
1
2
3
|
boxsize.Add( self .textprint, 1 ,flag = wx.EXPAND) #在boxsize中加上文本控制控件,比例为1 boxsize.Add(gridBox, 5 ,flag = wx.EXPAND) panel.SetSizerAndFit(boxsize) #将boxsize放入panel里 |
创建不同按钮触发的不同函数
1
2
3
4
5
6
7
8
9
10
11
12
|
def createHandler( self ,button,labels): #self.Bind(wx.EVT_BUTTON, self.OnButton1, self.Button1) #这里self是一个Frame(wxPython中的窗体这样的东西),而Button1是放在这个Frame下面的一个Button,self.OnButton1就是事件处理函数。而wxPython定死了事件处理函数的形式: items = "DEL AC =" if labels not in items: 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 .OnEqual,button) |
不同函数的具体执行过程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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 OnEqual( self ,event): string = self .equation try : taget = eval (string) #执行这段代码得到的结果 self .equation = str (taget) self .textprint.SetValue( self .equation) except SyntaxError: #弹出消息对话框 dlg = wx.MessageDialog( self ,u '输入格式错误' ,wx.Ok|wx.ICON_INFORMATION) dlg.ShowModal() dlg.Destory() |
APP类
1
2
3
4
5
6
|
class App(wx.App): def OnInit( self ): self .frame = CalcFrame(u '计算器' ) self .frame.Center() self .frame.Show() return True |
测试代码:
1
2
3
|
if __name__ = = "__main__" : app = App() app.MainLoop() |
完整示例代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# -*- coding:utf-8 -*- ''' Created on 2018年8月25日 @author: Administrator ''' import wx class CalcFrame(wx.Frame): #建造一个继承于wx.Frame的frame def __init__( self ,title): wx.Frame.__init__ ( self , None , - 1 ,title, pos = ( 100 , 300 ),size = ( 300 , 320 )) panel = wx.Panel( self ) boxsize = wx.BoxSizer(wx.VERTICAL) gridBox = wx.GridSizer( 4 , 4 , 1 , 1 ) self .equation = "" #记录一个等式,把它print到TextCtrl中 self .textprint = wx.TextCtrl(panel, - 1 ,'',style = wx.TE_RIGHT|wx.TE_READONLY) #文本控制控件 self .bgFont = wx.Font( 25 ,wx.SWISS,wx.NORMAL,wx.BOLD,face = u 'Roboto' ) #设置字体样式 self .textprint.SetFont( self .bgFont) #使用SetFont将设置的字体样式运用在textprint中 self .textprint.SetForegroundColour( 'black' ) #设置前景色 self .textprint.SetBackgroundColour( 'white' ) #设置后景色 self .buttonData = "7 8 9 DEL 4 5 6 AC 1 2 3 * / + - =" .split() #产生buttonIterm的label #将label分配到按钮上去 buttonLength = len ( self .buttonData) for i in range (buttonLength): labels = "%s" % self .buttonData[i] buttonIterm = wx.Button(panel,i,labels) self .createHandler(buttonIterm,labels) gridBox.Add(buttonIterm, 0 ,flag = wx.EXPAND) boxsize.Add( self .textprint, 1 ,flag = wx.EXPAND) #在boxsize中加上文本控制控件,比例为1 boxsize.Add(gridBox, 5 ,flag = wx.EXPAND) panel.SetSizerAndFit(boxsize) #将boxsize放入panel里 def createHandler( self ,button,labels): #self.Bind(wx.EVT_BUTTON, self.OnButton1, self.Button1) #这里self是一个Frame(wxPython中的窗体这样的东西),而Button1是放在这个Frame下面的一个Button,self.OnButton1就是事件处理函数。而wxPython定死了事件处理函数的形式: items = "DEL AC =" if labels not in items: 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 .OnEqual,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 OnEqual( self ,event): string = self .equation try : taget = eval (string) #执行这段代码得到的结果 self .equation = str (taget) self .textprint.SetValue( self .equation) except SyntaxError: #弹出消息对话框 dlg = wx.MessageDialog( self ,u '输入格式错误' ,wx.Ok|wx.ICON_INFORMATION) dlg.ShowModal() dlg.Destory() class App(wx.App): def OnInit( self ): self .frame = CalcFrame(u '计算器' ) self .frame.Center() self .frame.Show() return True if __name__ = = "__main__" : app = App() app.MainLoop() |
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://www.jianshu.com/p/37eafdf9815b