Date
Sep. 8th, 2024
 
2024年 8月 6日

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.
反曲点科技创始人和首席执行官。
程序猿、设计师、奇怪的博主。
苹果死忠、热爱色彩斑斓的世界。
如有意向请随时 与我联系