Skip to content

Instantly share code, notes, and snippets.

@xiushu53
Created June 29, 2015 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiushu53/13fde8d0c2555c78a6aa to your computer and use it in GitHub Desktop.
Save xiushu53/13fde8d0c2555c78a6aa to your computer and use it in GitHub Desktop.
摂氏華氏
# coding: utf-8
# Calculator
from __future__ import division
import ui
import clipboard
from console import hud_alert
shows_result = False
def button_tapped(sender):
# Get the button's title for the following logic:
'@type sender: ui.Button'
t = sender.title
global shows_result
# Get the labels:
label = sender.superview['label1']
label2 = sender.superview['label2']
if t in '0123456789':
if shows_result or label.text == '0':
# Replace 0 or last result with number:
label.text = t
else:
# Append number:
label.text += t
elif t == '.' and label.text[-1] != '.':
# Append decimal point (if not already there)
label.text += t
elif t in '℃,℉':
if label.text[-1] in '℃,℉':
# Replace current operator
label.text = label.text[:-1] + t
else:
# Append operator
label.text += t
elif t == 'AC':
# Clear All
label.text = '0'
elif t == 'C':
# Delete the last character:
label.text = label.text[:-1]
if len(label.text) == 0:
label.text = '0'
elif t == '=':
# Evaluate the result:
try:
label2.text = label.text + ' ='
expr = label.text.replace('℃','*1.8+32').replace('℉','/1.8-32/1.8')
label.text = str(round(eval(expr),1))
except SyntaxError:
label.text = 'ERROR'
shows_result = True
if t != '=':
shows_result = False
label2.text = ''
def copy_action(sender):
'@type sender: ui.Button'
t1 = sender.superview['label1'].text
t2 = sender.superview['label2'].text
if t2:
text = t2 + ' ' + t1
else:
text = t1
clipboard.set(text)
hud_alert('Copied')
v = ui.load_view('ondoui')
if ui.get_screen_size()[1] >= 768:
# iPad
v.present('popover')
else:
# iPhone
v.present(orientations=['portrait'])
[{"class":"View","attributes":{"name":"摂氏⇆華氏","tint_color":"RGBA(0.000000,0.478000,1.000000,1.000000)","background_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","enabled":true,"border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","flex":""},"frame":"{{0, 0}, {260, 337}}","nodes":[{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"1","uuid":"9CBA1483-F9EA-4BCE-9244-E3771881A0D4","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","background_color":"RGBA(0.862245,0.928571,0.916867,1.000000)","title":"1"},"frame":"{{18, 212}, {50, 50}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"button1","uuid":"6E122851-7F70-4937-A077-9C5C8136C854","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","background_color":"RGBA(0.862245,0.928571,0.916867,1.000000)","title":"4"},"frame":"{{18, 154}, {50, 50}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"button2","uuid":"43979F64-7215-4388-B133-89518D7EC78B","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","background_color":"RGBA(0.862245,0.928571,0.916867,1.000000)","title":"7"},"frame":"{{18, 96}, {50, 50}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"button3","uuid":"ED9ED715-1A34-476D-B4D9-8115B4DD6CC6","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","background_color":"RGBA(0.862245,0.928571,0.916867,1.000000)","title":"2"},"frame":"{{76, 212}, {50, 50}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"button4","uuid":"6B40E9A3-556D-4724-9365-7AAF965A30B9","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","background_color":"RGBA(0.862245,0.928571,0.916867,1.000000)","title":"5"},"frame":"{{76, 154}, {50, 50}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"button5","uuid":"11DC58F1-3CD4-40B0-96CA-619391DCC1B6","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","background_color":"RGBA(0.862245,0.928571,0.916867,1.000000)","title":"8"},"frame":"{{76, 96}, {50, 50}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"button6","uuid":"B878F4E8-ADB6-4DAE-B41A-92F729EA1FAC","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","background_color":"RGBA(0.862245,0.928571,0.916867,1.000000)","title":"3"},"frame":"{{134, 212}, {50, 50}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"button7","uuid":"08AEFEAD-3058-4E04-9B91-30DE7933090A","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","background_color":"RGBA(0.862245,0.928571,0.916867,1.000000)","title":"6"},"frame":"{{134, 154}, {50, 50}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"button8","uuid":"897DFCA4-70A0-4838-B67C-61DA6D943E70","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","background_color":"RGBA(0.862245,0.928571,0.916867,1.000000)","title":"9"},"frame":"{{134, 96}, {50, 50}}","nodes":[]},{"class":"Button","attributes":{"tint_color":"RGBA(0.404239,0.714286,0.204082,1.000000)","font_size":15,"enabled":true,"font_bold":false,"name":"button9","flex":"","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","uuid":"8A9748AE-DC8D-42CD-BAFD-E3C5F0AD9DB2","background_color":"RGBA(1.000000,0.857143,0.857143,1.000000)","title":"℃"},"frame":"{{192, 96}, {50, 50}}","nodes":[]},{"class":"Button","attributes":{"tint_color":"RGBA(0.403061,0.714286,0.204082,1.000000)","font_size":15,"enabled":true,"font_bold":false,"name":"button10","flex":"","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","uuid":"BEF35E12-EA25-4287-AFE8-627117EE16A0","background_color":"RGBA(1.000000,0.857143,0.857143,1.000000)","title":"℉"},"frame":"{{192, 154}, {50, 50}}","nodes":[]},{"class":"Button","attributes":{"tint_color":"RGBA(1.000000,0.000000,0.000000,1.000000)","font_size":15,"enabled":true,"font_bold":false,"name":"button11","flex":"","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","uuid":"42009062-216A-4AFC-8A3C-A176A48487D0","background_color":"RGBA(1.000000,0.989286,0.642857,1.000000)","title":"AC"},"frame":"{{134, 270}, {50, 50}}","nodes":[]},{"class":"Label","attributes":{"font_size":22,"enabled":true,"text":"","flex":"","name":"label1","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"left","background_color":"RGBA(0.785714,1.000000,0.995714,1.000000)","uuid":"5F43E029-13F6-44DD-A47C-F7FEFAFFAAE8"},"frame":"{{76, 42}, {166, 46}}","nodes":[]},{"class":"Label","attributes":{"tint_color":"RGBA(0.428571,0.622857,1.000000,1.000000)","font_size":17,"enabled":true,"text":"","name":"label2","flex":"","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","text_color":"RGBA(0.214286,0.615000,1.000000,1.000000)","alignment":"left","uuid":"09CD3EF6-CCE9-4CDE-95EE-F28F950F4AE1"},"frame":"{{18, 16}, {224, 20}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"button12","uuid":"1AFAE0D1-B89F-4C81-A121-6995FC2DF33D","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","background_color":"RGBA(1.000000,0.900000,0.642857,1.000000)","title":"="},"frame":"{{192, 212}, {50, 108}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"button13","uuid":"80775B33-2A02-45A1-B465-432A15846C42","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","background_color":"RGBA(0.862245,0.928571,0.916867,1.000000)","title":"."},"frame":"{{76, 270}, {50, 50}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"button14","uuid":"5C4F8406-210F-450D-ACAC-CA5CD7280895","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"button_tapped","background_color":"RGBA(0.862245,0.928571,0.918622,1.000000)","title":"0"},"frame":"{{18, 270}, {50, 50}}","nodes":[]}]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment