Hi,
i have just started programming with python and i have beken given a task where i must create a python calculator.
So far i have created the code (seen below) however i need help explaining it-e.g. saying what each part of the code does:

from tkinter import *

root = Tk()
root.title("Python Calculator")

button_layout_Design = [
"1","2","3","+",
"4","5","6","-",
"7","8","9","*",
"C","0","=","/"
]

r = 1
c = 0
for buttons in button_layout_Design:
    cmd = lambda key=buttons: onClick(key)
    Button(root,text=buttons,relief="flat",command=cmd).grid(row=r,column=c)
    c += 1
    if c > 3:
        r += 1
        c = 0
view = Entry(root)
view.grid(row=0,column=0,columnspan=4)

def onClick(key):
    if key == "C":
        view.delete(0,END)
    elif key == "=":
        try:
            ans = eval(view.get())
            view.delete(0,END)
            view.insert(0,ans)
        except SyntaxError:
            view.delete(0,END)
            view.insert(0,"Syntax Error")
        except ZeroDivisionError:
            view.delete(0,END)
            view.insert(0,"You cann't divide by zero!")
    else:
        view.insert(END,key)

Any help would be much appreciated!

I have python2 so I changed tkinter to Tkinter. I am not a big tkinter expert.

Firstly the code does not work. root.mainloop() or similar is missing. If I put it in, it works.

Secondly. If you have created the code, then there was something on your mind, or not?

Thirdly: I tried to do what I understand you want.

* We create a list of one character strings, which are the button labels and numbers or operations at the same time. 
* We start a loop on button label elements. We interpret an index of a button label element as a row, column index. row= index//4+1 column=index%4
* In this loop for each element **begin**
* We create a button, with the following attributes 
    * the actual label element as text 
    * a function call with the actual label element as a parameter (function not explained here)
    * we set the button's grid, to place the button to row and column
* In this loop for each element **end**
* We create an entry widget  on the first (0th) row and make it span 4 column.

Hi Slate,
Thanks for your help. I enjoy programming however i find writing everything down, as in what every line of code does in way that people understand, quite difficult!

Also, what part of my code does each of the lines which you have written relate to?
Thanks again for all your help - it is much appreciated!

* We create a list of one character strings, which are the button labels and numbers or operations at the same time. 
line 6-11
* We start a loop on button label elements. We interpret an index of a button label element as a row, column index. row= index//4+1 column=index%4
line 13-15, line 19-21, 
* In this loop for each element **begin**
* We create a button, with the following attributes 
    * the actual label element as text 
line 17 text=buttons
    * a function call with the actual label element as a parameter (function not explained here)
line 16 and line 17 'command=cmd '
    * we set the button's grid, to place the button to row and column
line 17 '.grid(row=r, column=c)'
* In this loop for each element **end**
* We create an entry widget  on the first (0th) row and make it span 4 column.
line 22-23

Thank you very much for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.