Python体验(10)-图形界面之计算器
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)-图形界面之计算器的更多相关文章
- Python体验(07)-图形界面之菜单
顺序安装以下程序: python解释器:https://www.python.org/downloads/ wxPython图形界面框架包:http://www.wxpython.org/ pycha ...
- Python体验(08)-图形界面之工具栏和状态栏
# coding=utf-8 import wx # 导入必须的Python包 class MenuForm(wx.Frame): def OnQuit(self,event): self.Close ...
- 用aardio给python写个图形界面
前阵子在用python写一些小程序,写完后就开始思考怎么给python程序配一个图形界面,毕竟控制台实在太丑陋了. 于是百度了下python的图形界面库,眼花缭乱的一整页,拣了几件有“特色”有“噱头” ...
- 【Python】 用户图形界面GUI wxpython III 更多组件
wxpython - 更多组件 我写到的这些组件可能一来不是很详细,二来不是最全的,想要更好地用这些组件,应该还是去看看教程和别的示例.比较简单的,推荐http://download.csdn.net ...
- 【Python】 用户图形界面GUI wxpython IV 菜单&对话框
更多组件 ■ 菜单栏 Menu 菜单是很多GUI必不可少的一部分.要建立菜单,必须先创建菜单栏: menuBar = MenuBar() menu = Menu() item1 = menu.Appe ...
- 【Python】 用户图形界面GUI wxpython I 基本用法和组件
wxpython - 基本用法和组件 wxpython是python对跨平台GUI库wxWidgets的封装.wxWidgets是由C++写成的. wxpython被包装进了wx模块中,用它设计GUI ...
- python学习之图形界面编程:
一 tkinter:tkinter是python自带的支持tk的库,python代码调用tkinter->tk->操作系统提供的本地GUI(TKL语言开发))完成界面开发,不需要安装任何第 ...
- Python 的简单图形界面编程【草】
可用方案 Tkinter python官方附带,方便,但听说存在乱码问题 wxPython 更成熟一些,但需要额外安装(大约50M) pyQt 授权不够宽松 最短代码 Tkinter 待补充 wxPy ...
- 【Python】 用户图形界面GUI wxpython II 布局和事件
wxpython - 布局和事件 这章主要记录布局器Sizer以及事件的用法. // 目前还需要记录的:Sizer的Add方法加空白,Sizer的Layout,Sizer的Remove如何有效 ■ 布 ...
随机推荐
- mybatis:Invalid bound statement (not found)
[常规解决办法] 如果出现: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 一般的原因 ...
- java的三大框架(二)---Struts2
Strtu2框架 1.控制器:ActionServlet充当控制层 2.模型层:由ActionForm及业务JavaBean实现 3.视图:用户的看到并与之交互的界面 由struts标签库和jsp ...
- iOS开发常见错误解决方法
1.出现下面的错误: StoryboardApp[8593:207] Failed to instantiate the default view controller for UIMainStory ...
- ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- BOM和DOM
Day 01 正课: 1.什么是DOM: 2.DOM Tree 3.遍历DOM树: 1.什么是DOM: 原生js=ECMAScript(核心语法)+ DOM(专门操作网页内容的API)+ 3天 BOM ...
- maven加载jar包配置
maven build时报程序包不存在和找不到符号的错误,但是代码中不报错,如下: [ERROR] Failed to execute goal org.apache.maven.plugins:ma ...
- Quartz conTrigger 定时器表达式大全
CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 秒 是 0-59 , ...
- 基本的HTML标签
html的基本标签: <html><body> <h1>My First Heading</h1><h2>this second headi ...
- spring加载bean实例化顺序
问题来源: 有一个bean为 A,一个bean为B.想要A在容器实例化的时候的一个属性name赋值为B的一个方法funB的返回值. 如果只是在A里单纯的写着: private B b;private ...
- Orchard学习笔记
1.下载Orchard sourcrs资源文件,同时也可以去百度下载中文包 资源地址(https://github.com/OrchardCMS/Orchard/releases/download/1 ...