-3

I've made a simple calculator with buttons like 1, 2 ,3 and so on..but i am unable to put those button text like 1 or 2 etc onto the calculator screen. It would be very helpful if you guys give me some hints..

from tkinter import *
root = Tk()
buttons = '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', '-'

def calc(value):
    res = num.get()
    res = res + " " + value
    res = str(eval(res))
    num.set(res)


num = StringVar()
rows = 1
col = 0

ent = Entry(root, textvariable = num, background = "#D0F3F5", border = 2, width = 50)
ent.bind('<FocusOut>')
ent.grid(row = 0 , column = 0, columnspan = 4, ipadx = 5, ipady = 5)

Button(root, text = '=', width = 45, command = calc).grid(column = 0, row = 5, columnspan = 4)

for button in buttons:
    button = Button(root,width = 10, text = button, command = lambda: calc(button))
    #button.bind('<Button-1>', lambda e: screen(button))
    button.grid(row = rows, column = col, sticky = "W E")
    button['relief']="groove"
    col = col + 1

    if col == 4:
        rows = rows + 1
        col = 0

    if rows > 6:
        break




root.mainloop()
Ahmad
  • 23
  • 1
  • 6
  • Having trouble understanding the purpose of some bits. `command = calc` how does this even get used? `for button in buttons: button = Button(..)` using same naming here. `res + " " + value` `str(eval(res))` what are you trying to evaluate? – Steven Summers Aug 04 '16 at 08:48

1 Answers1

0

There are couple problems in here.

1) Variable button is used for two different purposes which makes one of them override the other. You need to rename one of them.

2) When using lambdas with parameters in for loops, to get current value of that parameter, you need to explicitly write the value.

3) You are not passing any parameter to command of equal sign button.

4) To make calculations, you need to check if the pressed button is =. If not, you shouldn't try to calculate anything.

Applying all these to your code results in:

def calc(value):
    res = num.get()
    res = res + value

    if value == "=": 
        res = str(eval(res[:-1])) #to exclude equal sign itself

    num.set(res)

#equal sign button
Button(root, text = '=', width = 45, command = lambda: calc("=")).grid(...)

#for loop and renaming a variable
for x in buttons:
    button = Button(root,width = 10, text = x, command = lambda x=x: calc(x))
Community
  • 1
  • 1
Lafexlos
  • 7,618
  • 5
  • 38
  • 53
  • Thank You Sir @Lafexlos..it is working now ..You really helped me sir..and now i understood the purpose of lambda with your explation. – Ahmad Aug 04 '16 at 09:18
  • @Ahmad if this answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Lafexlos Aug 08 '16 at 09:06
  • 1
    I didn't know otherwise i'd have done it before..Thanks again..:) – Ahmad Aug 08 '16 at 11:07