/usr/share/pyshared/wxbanker/calculator.py is in wxbanker 0.9.1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | # https://launchpad.net/wxbanker
# calculator.py: Copyright 2007-2010 Mike Rooney <mrooney@ubuntu.com>
#
# This file is part of wxBanker.
#
# wxBanker is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# wxBanker is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with wxBanker. If not, see <http://www.gnu.org/licenses/>.
import wx
from wx.lib.pubsub import Publisher
from wxbanker import localization
class SimpleCalculator(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.numberClears = True
sizer = wx.BoxSizer(wx.VERTICAL)
self.display = wx.TextCtrl(self, -1, '0.00', style=wx.TE_RIGHT)
self.display.Bind(wx.EVT_CHAR, self.onChar)
sizer.Add(self.display, 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 4)
gs = wx.GridSizer(4, 4, 3, 3)
for lbl in "C ()789/456*123-0.+=":
if not lbl == ' ':
btn = wx.Button(self, label=lbl)
btn.Bind(wx.EVT_BUTTON, self.onButton)
else:
btn = wx.StaticText(self)
gs.Add(btn, 1, wx.EXPAND)
sizer.Add(gs, 1, wx.EXPAND)
self.SetSizer(sizer)
Publisher.subscribe(self.onPushChars, "CALCULATOR.PUSH_CHARS")
wx.CallLater(50, self.display.SetInsertionPointEnd)
def onPushChars(self, message):
for char in message.data:
self.onChar(char=char)
self.Parent.Parent.SetExpanded(True)
def onChar(self, event=None, char=None):
"""
Handles characters, either from a button or a key typed into the text control.
Supported: 0123456789./*-+
Special:
* 'c' or 'C': clears the text ctrl
* '=' (or WXK_RETURN): evaluates the expression and displays the result
* WXK_BACK: backspace, deletes the last character
All other characters/keypresses are discarded.
"""
if event:
keycode = event.KeyCode
if keycode == wx.WXK_BACK:
self.display.Value = self.display.Value[:-1]
self.display.SetInsertionPointEnd()
self.numberClears = False
return
elif keycode == wx.WXK_RETURN:
char = '='
else:
try:
char = chr(event.KeyCode)
except ValueError:
# It may have been an arrow key press or something else not chr'able.
if event:
event.Skip()
return
else:
assert char
if char == '=':
self.onEnter()
elif char.upper() == 'C':
self.display.Value = ""
elif char in '()0123456789./*-+':
if char in '()0123456789.' and self.numberClears:
self.display.Value = ""
self.numberClears = False
self.display.AppendText(char)
else:
# Not a valid character.
return
self.display.SetFocus()
self.display.SetInsertionPointEnd()
def onEnter(self, event=None):
"""
Attempt to do a simple eval on the text.
Note no implicit order of operations is respected.
"""
try:
# Multiply by float first, so 1/2 is 0.50 and not 0.00
result = eval("1.*"+self.display.Value)
except:
return
self.display.Value = "%.2f"%result
self.numberClears = True
self.display.SetInsertionPointEnd()
def onButton(self, event=None):
btn = event.EventObject
command = btn.Label
self.onChar(char=command)
class CollapsableWidget(wx.CollapsiblePane):
def __init__(self, parent, widget, labels, *args, **kwargs):
self.Labels = labels
wx.CollapsiblePane.__init__(self, parent, label=self.Labels[0], style=wx.CP_DEFAULT_STYLE|wx.CP_NO_TLW_RESIZE)
pane = self.GetPane()
self.widget = widget(pane, *args, **kwargs)
pane.Sizer = wx.BoxSizer()
pane.Sizer.Add(self.widget, 1, wx.EXPAND)
self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.OnPaneChanged)
def SetExpanded(self, expanded):
"""
Set whether the contained widget is collapsed or expanded. If there will be no change, don't do anything.
"""
if expanded != self.IsExpanded():
self.Collapse(not expanded)
self.OnPaneChanged()
def OnPaneChanged(self, evt=None):
"""Redo the text and layout when the widget state is toggled."""
self.Layout()
# Change the labels
expanded = int(self.IsExpanded())
self.Label = self.Labels[expanded]
Publisher.sendMessage("CALCULATOR.TOGGLED", ("SHOW", "HIDE")[expanded])
|