I am playing around with Tkinter and building a calculator from the base up. To try and understand as learn as much as possible about the event and the library as I can while I go along.
Right now I am at a point where I simply want the buttons to pass the value on the button to the label at the top.
I've used a for loop to create most of the buttons to avoid redundant code but now the only value being passed onto the textvariable in the label is the last item, '.', in my buttons list and I am not sure why that is. Can someone help?
code below:
from Tkinter import *
import Tkinter as tk
# main window
root = Tk()
root.title('Calculator')
# button set
buttons = ['1','2','3','4','5','6','7','8','9','0','+','-','/','*','.']
sum_value = StringVar()
# output window
output_window = tk.Label(root, textvariable=sum_value, width=20, height=2).grid(row=0, columnspan=3, sticky=(E,W))
# button creation
r=1
c=0
for i in buttons:
if c < 2:
bi = tk.Button(root, text = i, command = lambda: sum_value.set(i), padx = 5, pady = 3).grid(row = r, column = c, sticky = (N,S,E,W))
c += 1
else:
bi = tk.Button(root, text = i, command = lambda: sum_value.set(i), pady = 3).grid(row = r,column = c,sticky = (N,S,E,W))
r += 1
c = 0
# clear and equals button
clear = tk.Button(root,text='=',padx = 5, pady=3).grid(row=6,column=0,sticky=(N,S,E,W))
clear = tk.Button(root,text='CLEAR',padx = 5, pady=3).grid(row=6,column=1, columnspan = 2,sticky=(N,S,E,W))
root.mainloop()