import wx
class 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, ''), 0, wx.EXPAND),
(wx.Button(self, 2, ''), 0, wx.EXPAND),
(wx.Button(self, 3, ''), 0, wx.EXPAND),
(wx.Button(self, 4, '/'), 0, wx.EXPAND),
(wx.Button(self, 5, ''), 0, wx.EXPAND),
(wx.Button(self, 6, ''), 0, wx.EXPAND),
(wx.Button(self, 7, ''), 0, wx.EXPAND),
(wx.Button(self, 8, '*'), 0, wx.EXPAND),
(wx.Button(self, 10, ''), 0, wx.EXPAND),
(wx.Button(self, 11, ''), 0, wx.EXPAND),
(wx.Button(self, 9, ''), 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, 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 True app = MyApp(0)
app.MainLoop()
import wx
class 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, ''), 0, wx.EXPAND),
(wx.Button(self, 2, ''), 0, wx.EXPAND),
(wx.Button(self, 3, ''), 0, wx.EXPAND),
(wx.Button(self, 4, '/'), 0, wx.EXPAND),
(wx.Button(self, 5, ''), 0, wx.EXPAND),
(wx.Button(self, 6, ''), 0, wx.EXPAND),
(wx.Button(self, 7, ''), 0, wx.EXPAND),
(wx.Button(self, 8, '*'), 0, wx.EXPAND),
(wx.Button(self, 10, ''), 0, wx.EXPAND),
(wx.Button(self, 11, ''), 0, wx.EXPAND),
(wx.Button(self, 9, ''), 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, 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 True app = MyApp(0)
app.MainLoop()

Python体验(10)-图形界面之计算器的更多相关文章

  1. Python体验(07)-图形界面之菜单

    顺序安装以下程序: python解释器:https://www.python.org/downloads/ wxPython图形界面框架包:http://www.wxpython.org/ pycha ...

  2. Python体验(08)-图形界面之工具栏和状态栏

    # coding=utf-8 import wx # 导入必须的Python包 class MenuForm(wx.Frame): def OnQuit(self,event): self.Close ...

  3. 用aardio给python写个图形界面

    前阵子在用python写一些小程序,写完后就开始思考怎么给python程序配一个图形界面,毕竟控制台实在太丑陋了. 于是百度了下python的图形界面库,眼花缭乱的一整页,拣了几件有“特色”有“噱头” ...

  4. 【Python】 用户图形界面GUI wxpython III 更多组件

    wxpython - 更多组件 我写到的这些组件可能一来不是很详细,二来不是最全的,想要更好地用这些组件,应该还是去看看教程和别的示例.比较简单的,推荐http://download.csdn.net ...

  5. 【Python】 用户图形界面GUI wxpython IV 菜单&对话框

    更多组件 ■ 菜单栏 Menu 菜单是很多GUI必不可少的一部分.要建立菜单,必须先创建菜单栏: menuBar = MenuBar() menu = Menu() item1 = menu.Appe ...

  6. 【Python】 用户图形界面GUI wxpython I 基本用法和组件

    wxpython - 基本用法和组件 wxpython是python对跨平台GUI库wxWidgets的封装.wxWidgets是由C++写成的. wxpython被包装进了wx模块中,用它设计GUI ...

  7. python学习之图形界面编程:

    一 tkinter:tkinter是python自带的支持tk的库,python代码调用tkinter->tk->操作系统提供的本地GUI(TKL语言开发))完成界面开发,不需要安装任何第 ...

  8. Python 的简单图形界面编程【草】

    可用方案 Tkinter python官方附带,方便,但听说存在乱码问题 wxPython 更成熟一些,但需要额外安装(大约50M) pyQt 授权不够宽松 最短代码 Tkinter 待补充 wxPy ...

  9. 【Python】 用户图形界面GUI wxpython II 布局和事件

    wxpython - 布局和事件 这章主要记录布局器Sizer以及事件的用法. // 目前还需要记录的:Sizer的Add方法加空白,Sizer的Layout,Sizer的Remove如何有效 ■ 布 ...

随机推荐

  1. CentOS 一键搭建pptpd

    rpm -Uvh http://poptop.sourceforge.net/yum/stable/rhel6/pptp-release-current.noarch.rpm yum -y insta ...

  2. Xcode中创建文件夹

    如果在xcode工程中new group,只是在视觉效果上分好了几个文件夹,方便分类管理,但在finder中并不会创建新的文件夹,在硬盘目录还是所有文件都并列在一个文件夹内,更恶心的是当你重新打开工程 ...

  3. canvas 画字

    用canvas画字还是头一回,要想和UI设计的画的一模一样还是真有些苦难,不过现在实现的效果已经很像了. <!--通过字体文件引入字体--><style>@font-face ...

  4. 命名空间System.Threading命名空间的同步锁 Monitor类

    官方备注: Monitor类通过向单个线程授予对象所来控制对对象的访问.对象所提供限制访问代码块的能力.当一个线程拥有对象的锁时,其他任何线程都不能获取该锁.还可以使用Monitor来确保不会允许其他 ...

  5. io.sort.spill.percent调整

    引言 MapReduce作出保证:进入每个Reducer的数据行都是有序的(根据数据行的键值进行排序).MapReduce将Mapper的输出进行排序并传递给Reducer作为输入的过程称为Shuff ...

  6. CNC Fanuc 设备数据采集

    为了这个Fanuc(发那科)数控机床数据的采集也花费了不少精力,先是去供应商那里了解,基本都是代理商,没有技术支持. 在网上也有关于Fanuc的以太网Ethernet连接文档,那里面有说明,大概是开发 ...

  7. Mac10.9用brew搭建Eclipse4.4+Maven3.2.3+JDK1.8运行环境

    --------------------------------------- 博文作者:迦壹 博客标题:Mac10.9用brew搭建Eclipse4.4+Maven3.2.3+JDK1.8运行环境 ...

  8. Oracle几个基础配置问题:ORA-12154: TNS: 无法解析指定的连接标识符、ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务、ORA-12516 TNS监听程序找不到符合协议堆栈要求的可用处理程序

    问题1:ORA-12154: TNS: 无法解析指定的连接标识符 在一台服务器上部署了Oracle客户端,使用IP/SID的方式访问,老是报ORA-12154错误,而使用tnsnames访问却没有问题 ...

  9. jQuery中,$('#main') 与 document.getElementById('main')是什么样的关系-转

    $('#main')[0]和document.getElementById('main')两个一模一样.解释:$('#main'):是一个jquery写法,#main是一个过滤器表示方法,表示查找一个 ...

  10. Linux下使用ping快速检测存活主机

    该shell脚本主要是通过ping来批量检测网段内存活主机,执行结果保存在脚本当前目录下的IPinfor目录中,写的比较匆忙,希望大家留下更好的建议. #!/usr/bin/env bash##### ...