So I'm new to Tkinter, and I decide to make my simple calculator nicer using images. So I try to add images for the buttons: (example)
and for some reason with the size, the buttons look like this in the GUI:
and I can't seem to figure out how to change the size of the images. I've already tried using width and length paremeters on the tk.PhotoImage objects and the button() object. Before the images existed, the buttons weren't small. Here's my code:
import tkinter as tk
expression = ''
def press(sym):
global expression, equation
expression = expression + str(sym)
equation.set(expression)
def equalpress():
global expression, equation
try:
result = str(eval(expression))
equation.set(result)
except:
equation.set('ERROR')
expression = ''
def clearpress():
global expression, equation
equation.set('')
expression = ''
def main():
global expression, equation
print('hi')
root = tk.Tk()
root.title('Calculator 1.0')
root.geometry("360x200")
equation = tk.StringVar()
expression_field = tk.Entry(root, textvariable=equation)
expression_field.grid(columnspan=4, ipadx=70)
photo = tk.PhotoImage(file = r'1_button.png', height=1, width=7)
button1 = tk.Button(root,image=photo,command=lambda: press(1))
button1.grid(row=2, column=0)
photo = tk.PhotoImage(file = r'2_button.png', height=1, width=7)
button2 = tk.Button(root,image=photo, command=lambda: press(2))
button2.grid(row=2, column=1)
photo = tk.PhotoImage(file = r'3_button.png', height=1, width=7)
button3 = tk.Button(root,image=photo,command=lambda: press(3))
button3.grid(row=2, column=2)
photo = tk.PhotoImage(file = r'4_button.png', height=1, width=7)
button4 = tk.Button(root,image=photo,command=lambda: press(4))
button4.grid(row=3, column=0)
photo = tk.PhotoImage(file = r'5_button.png', height=1, width=7)
button5 = tk.Button(root,image=photo,command=lambda: press(5))
button5.grid(row=3, column=1)
photo = tk.PhotoImage(file = r'6_button.png', height=1, width=7)
button6 = tk.Button(root,image=photo,command=lambda: press(6))
button6.grid(row=3, column=2)
photo = tk.PhotoImage(file = r'7_button.png', height=1, width=7)
button7 = tk.Button(root,image=photo,command=lambda: press(7))
button7.grid(row=4, column=0)
photo = tk.PhotoImage(file = r'8_button.png', height=1, width=7)
button8 = tk.Button(root,image=photo,command=lambda: press(8))
button8.grid(row=4, column=1)
photo = tk.PhotoImage(file = r'9_button.png', height=1, width=7)
button9 = tk.Button(root,image=photo,command=lambda: press(9))
button9.grid(row=4, column=2)
photo = tk.PhotoImage(file = r'0_button.png', height=1, width=7)
button0 = tk.Button(root,image=photo,command=lambda: press(0))
button0.grid(row=5, column=0)
photo = tk.PhotoImage(file = r'plus_button.png', height=1, width=7)
plus = tk.Button(root,image=photo,command=lambda: press("+"))
plus.grid(row=2, column=3)
photo = tk.PhotoImage(file = r'minus_button.png', height=1, width=7)
minus = tk.Button(root,image=photo,command=lambda: press("-"))
minus.grid(row=3, column=3)
photo = tk.PhotoImage(file = r'times_button.png', height=1, width=7)
multiply = tk.Button(root,image=photo,command=lambda: press("*"))
multiply.grid(row=4, column=3)
photo = tk.PhotoImage(file = r'divide_button.png', height=1, width=7)
divide = tk.Button(root, image=photo,command=lambda: press("/"))
divide.grid(row=5, column=3)
photo = tk.PhotoImage(file = r'equals_button.png', height=1, width=7)
equal = tk.Button(root, image=photo,command=equalpress)
equal.grid(row=5, column=2)
photo = tk.PhotoImage(file = r'clear_button.png', height=1, width=7)
clear = tk.Button(root, image=photo,command=clearpress)
clear.grid(row=5, column='1')
photo = tk.PhotoImage(file = r'decimal_button.png', height=1, width=7)
Decimal= tk.Button(root, image=photo,command=lambda: press('.'))
Decimal.grid(row=6, column=0)
# start the GUI
root.mainloop()
if __name__ == '__main__':
main()
else:
print('You are not in the right file')
How can I change my button/image size?
(I am also using python 3)