17 Jun Markham Saturday 9:30 Python Homework 20.06.13.
Continue to program the code we made in the class at the bottom of this page. Try to use for loop to create buttons from 1-9, so the tkinter window would display as below:
You can use “for i in range(1, 10):” or “for i in ‘123456789’:” to build these 9 buttons, however, you should thinking of how to use math techniques to solve the row and column.
for example, buttons 1,2,3 are in row 1 and buttons 4,5,6 are in row 2. You should think that int(1/3) is 0 or 1//3 is 0, 2//3 is still 0, 3//3 is 1, 4//3 is 1, 5//3 is 1, 6//3 is 2. Then how can you make 1,2,3 all become 1, while 4,5,6 all become 2?
You should use %(finding division remainder) to solve columns, but how?
from tkinter import *
def __init__(self, master):
self.master = master
self.f1 = Frame(master)
self.f1.option_add('*Font', 'arial 20 bold')
self.strResult = StringVar()
self.lblResult = Label(self.f1, textvariable=self.strResult, justify='right')
self.f2 = Frame(master, borderwidth=5, bd=1, bg='grey')
self.f2.option_add('*Font', 'arial 14')
self.f2.pack(side=TOP, expand=YES, fill=BOTH)
self.btnCE = Button(self.f2, text='CE', bg='#f8f8f8', width=4, command=self.reset)
# reciprocal 3/7 -> 7/3
self.btnRecip = Button(self.f2, text='1/x', bg='#f8f8f8', width=4, command=self.recip)
tk = Tk()
myCal = Calculator(tk)