Date
Jul. 14th, 2024
 
2024年 6月 9日

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
I'm looking for a SOFTWARE PROJECT DIRECTOR / SOFTWARE R&D DIRECTOR position in a fresh and dynamic company. I would like to gain the right experience and extend my skills while working in great teams and big projects.
Feel free to contact me.
For more information, please view online résumé or download PDF
本人正在寻求任职 软件项目经理 / 软件技术经理 岗位的机会, 希望加⼊某个新鲜⽽充满活⼒的公司。
如有意向请随时 与我联系
更多信息请 查阅在线简历下载 PDF