Date
Nov. 25th, 2024
 
2024年 10月 25日

Post: Python : Calculator

Python : Calculator

Published 12:03 Mar 20, 2014.

Created by @ezra. Categorized in #Programming, and tagged as #Python.

Source format: Markdown

Table of Content

用 Python 实现一个 GUI 计算器的基本思路,后面有时间会添加更高级的计算功能:

from __future__ import division
import ui
shows_result = False
def button_tapped(sender):
    t = sender.title
    global shows_result
    label = sender.superview['label1']
    label2 = sender.superview['label2']
    if t in '0123456789':
        if shows_result or label.text == '0':
            label.text = t
        else:
            label.text += t
    elif t == '.' and label.text[-1] != '.':
        label.text += t
    elif t in '+-÷×':
        if label.text[-1] in '+-÷×':
            label.text = label.text[:-1] + t
        else:
            label.text += t
    elif t == 'AC':
        label.text = '0'
    elif t == 'C':
        label.text = label.text[:-1]
        if len(label.text) == 0:
            label.text = '0'
    elif t == '=':
        try:
            label2.text = label.text + ' ='
            expr = label.text.replace('÷', '/').replace('×', '*')
            label.text = str(eval(expr))
        except (SyntaxError, ZeroDivisionError):
            label.text = 'ERROR'
        shows_result = True
    if t != '=':
        shows_result = False
        label2.text = ''
Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
开发、设计与写作皆为所长。
热爱苹果、钟情色彩。
随时恭候 垂询