2. iPhone Calculator
2.1. References
Youtube guide for using code to create the components: https://www.youtube.com/watch?v=NiQdsK3H57Y
Anvil events: https://anvil.works/docs/client/components
Python try-except: https://www.w3schools.com/python/python_try_except.asp
Python eval: https://www.w3schools.com/python/ref_func_eval.asp
Python enumerate: https://www.w3schools.com/python/ref_func_enumerate.asp
Colour hex values: https://www.w3schools.com/colors/colors_picker.asp?colorhex=85b185
Calculator icon: https://icons.iconarchive.com/icons/tristan-edwards/sevenesque/256/Calculator-icon.png
Grid panel syntax: https://anvil.works/docs/api/anvil#GridPanel
Button syntax: https://anvil.works/docs/api/anvil#Button
2.2. Get started
Click: Blank App.
Choose: Custom HTML
Choose: Blank Panel
2.3. Settings
Click on the cog icon to show the settings tab.
Enter an App name. iPhone_Calculator
Enter an App title. iPhone_Calculator
Enter an App description. iPhone_Calculator using code to build the buttons
Get a calculator icon to upload such as: https://icons.iconarchive.com/icons/tristan-edwards/sevenesque/256/Calculator-icon.png
Click Change Image to upload an App logo.
Close the settings tab.
2.4. Build first part of interface
Drag and drop the image component from the right toolbox onto Form1.
In the properties panel: height section, set the height to
75
.Drag and drop the textbox component from the right toolbox onto Form1 below the image.
In the properties panel: text section, set the align to
left
, the font toConsolas
and the font_size to32
.Click below on the form itself.
In the properties panel: appearance section, set the background to grey:
#eee
.
2.12. Final code
class Form1(Form1Template):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
# Any code you write here will run when the form opens.
# add btn text list
chars = ["AC", "C", "/",
"7", "8", "9", "*",
"4", "5", "6", "-",
"1", "2", "3", "+",
"0",".", "="]
self.btn = {}
gp = GridPanel()
# enumerate buttons
for idx,i in enumerate(chars):
#btn row
if idx < 3:
row = 'A'
elif 3 <= idx < 7:
row = 'B'
elif 7 <= idx < 11:
row = 'C'
elif 11 <= idx < 15:
row = 'D'
else:
row = 'E'
#btn colour
if i in ["AC", "C"]:
bgcol = "#999999"
fgcol = "#000000"
elif i in ["=", "+", "-", "*", "/"]:
bgcol = "#f6aa51"
fgcol = "#FFFFFF"
else:
bgcol = "#444444"
fgcol = "#FFFFFF"
#btn width
if i in ["AC", "0"]:
btnwidth = 2
else:
btnwidth = 1
#create btns
self.btn[i] = Button(align="full", text=i, font="Consolas", font_size=32, bold=False, foreground=fgcol,background=bgcol)
# to collect the tag name when clicked
self.btn[i].tag.name = i
# handle the click event and attach the click method to the event
self.btn[i].set_event_handler('click', self.click)
gp.add_component(self.btn[i], row=row, col_xs=3, width_xs=btnwidth)
# display grid panel
self.add_component(gp)
# add a spacer after grid panle to fill the bottom of the screen
self.space = Spacer(height=500)
self.add_component(self.space)
# click method for btns
def click(self, **event_args):
val = event_args['sender'].tag.name
if val == "=":
try:
self.text_box_1.text = eval(self.text_box_1.text)
except:
self.text_box_1.text += " error"
elif val == "AC":
self.text_box_1.text = ""
elif val == "C":
self.text_box_1.text = self.text_box_1.text[:-1]
else:
self.text_box_1.text += val
Tasks
The keyboard can also be used instead of the buttons. Try it out.
Try adding some maths function buttons in another row. e.g. x^2 which squares the text box. e.g. sqrt which takes the square root.
Try adding some maths function buttons in another row by importing the maths library. See: https://docs.python.org/3/library/math.html