DETAILS:
- I wanted to make a simple calculator in Tkinter.
- I have attached two codes , I just wanted to know what is the difference between the two codes as in one the buttons are coming 3 in a row but in other it is coming all in a row.
CODE 1 (in which everything is coming on a different line)
from tkinter import *
def click(event):
global scvalue
text = event.widget.cget("text")
if text == "=":
if scvalue.get().isdigit():
value = int(scvalue.get())
else:
try:
value = eval(screen.get())
except Exception as e:
print(e)
value = "Error"
scvalue.set(value)
screen.update()
elif text == "C":
scvalue.set("")
screen.update()
else:
scvalue.set(scvalue.get() + text)
screen.update()
root = Tk()
root.geometry("644x970")
root.title("Calculator By CodeWithHarry")
# root.wm_iconbitmap("1.ico")
scvalue = StringVar()
scvalue.set("")
screen = Entry(root, textvar=scvalue, font="lucida 40 bold")
screen.pack(fill=X, ipadx=8, pady=10, padx=10)
f = Frame(root, bg="grey")
b = Button(f, text="9", padx=28, pady=18, font="lucida 35 bold")
b.pack(side=LEFT, padx=18, pady=5)
# b.bind("", click)
b = Button(f, text="8", padx=28, pady=18, font="lucida 35 bold")
b.pack(side=LEFT, padx=18, pady=5)
# b.bind("", click)
b = Button(f, text="7", padx=28, pady=18, font="lucida 35 bold")
b.pack(side=LEFT, padx=18, pady=5)
# b.bind("", click)
f.pack()
f = Frame(root, bg="grey")
b = Button(f, text="6", padx=28, pady=18, font="lucida 35 bold")
b.pack(side=LEFT, padx=18, pady=5)
# b.bind("", click)
b = Button(f, text="5", padx=28, pady=18, font="lucida 35 bold")
b.pack(side=LEFT, padx=18, pady=5)
# b.bind("", click)
b = Button(f, text="4", padx=28, pady=18, font="lucida 35 bold")
b.pack(side=LEFT, padx=18, pady=5)
# b.bind("", click)
f.pack()
Code 2(In which everything is there on the same line)
root = Tk()
root.geometry('400x800')
string = StringVar()
string.set("")
def click(event):
global screen
text = event.widget.cget("text")
print(text)
screen = Entry(root,textvariable=string,font = 'Algerian 50 bold')
# screen.place(height=70,width = 'X')
screen.pack(padx = 10,pady = 10 , fill = X,ipady=20)
frame1 = Frame(root,bg = "Black")
b = Button(frame1,text = '9',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
b = Button(frame1,text = '8',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
b = Button(frame1,text = '7',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
frame1.pack()
b = Button(frame1,text = '6',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
b = Button(frame1,text = '5',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
b = Button(frame1,text = '4',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
frame1.pack()
b = Button(frame1,text = '3',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
b = Button(frame1,text = '2',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
b = Button(frame1,text = '1',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
frame1.pack()
b = Button(frame1,text = '0',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
b = Button(frame1,text = '+',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
b = Button(frame1,text = '-',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
frame1.pack()
b = Button(frame1,text = '*',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
b = Button(frame1,text = '/',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
b = Button(frame1,text = '%',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
frame1.pack()
b = Button(frame1,text = '**',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
b = Button(frame1,text = '=',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
b = Button(frame1,text = 'C',font = 'Dolce 45')
b.pack(side = LEFT , padx = 15 , pady = 15)
b.bind('<Button-1>',click)
frame1.pack()
root.mainloop()