Привет, я пытаюсь изменить размеры изображений вместе с кнопками на сетке всякий раз, когда изменяется размер окна программы, большинство руководств просто учит, как изменять размер кнопок, но кнопки или текст внутри них остаются прежними. Я думаю, что это возможно с pack (), но не уверен, и хотел бы знать, если это возможно с сеткой.
спасибо за ваше время.
from tkinter import *
import math
# ----- calculator class
class calc:
def getandreplace(self):
# replaces x with * and ÷ with /
self.expression = self.e.get()
self.newtext=self.expression.replace("/","/")
self.newtext=self.newtext.replace("x","*")
def equals(self):
# when = button is pressed
self.getandreplace()
try:
# evaluates expresion with eval command
self.value = eval(self.newtext)
except SyntaxError or NameError or ZeroDivisionError:
self.e.delete(0,END)
self.e.insert(0,"Invalid input!")
else:
self.e.delete(0,END)
self.e.insert(0,self.value)
def squareroot(self):
# square root
self.getandreplace()
try:
# evaluates expresion with eval command
self.value = eval(self.newtext)
except SyntaxError or NameError:
self.e.delete(0, END)
self.e.insert(0, "Invalid input!")
else:
self.sqrtval=math.sqrt(self.value)
self.e.delete(0,END)
self.e.insert(0,self.sqrtval)
def square(self):
# square
self.getandreplace()
try:
# evaluates expresion with eval command
self.value = eval(self.newtext)
except SyntaxError or NameError:
self.e.delete(0, END)
self.e.insert(0, "Invalid input!")
else:
self.sqval=math.pow(self.value,2)
self.e.delete(0,END)
self.e.insert(0,self.sqval)
def clearall(self):
# function to clear input from screen
self.e.delete(0,END)
def clear1(self):
self.txt=self.e.get()[:-1]
self.e.delete(0,END)
self.e.insert(0,self.txt)
def action(self,argi):
# when presing button,value typed will be inserted into end of text area
self.e.insert(END,argi)
def __init__(self,master):
# constructor
for x in range(6):
for y in range(4):
Grid.columnconfigure(master, x, weight=1)
Grid.rowconfigure(master, y, weight=1)
master.title("Calculator")
master.geometry('500x500')
master.iconbitmap(r'number_5.ico')
master.minsize(300,370)
self.e = Entry(master,font=20)
self.e.grid(row=0,column=0,columnspan=6,pady=16, sticky=N+S+E+W)
self.e.focus_set() # sets focus to input text area
# makes buttons
Button(master,text="=",width=11,height=3,font=("Helvetica", 15),fg="black",
bg="white",command=lambda:self.equals()).grid(
row=4, column=4,columnspan=2, sticky=N+S+E+W)
Button(master, text="cl_all",width=5,height=3,font=("Helvetica", 15),
fg="red", bg = "light green",
command=lambda:self.clearall()).grid(row=1, column=4, sticky=N+S+E+W)
Button(master, text="cl_one", width=5, height=3,font=("Helvetica", 15),
fg="red", bg="light green",
command=lambda: self.clear1()).grid(row=1, column=5, sticky=N+S+E+W)
Button(master, text="+", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="white",
command=lambda: self.action("+")).grid(row=4, column=3, sticky=N+S+E+W)
Button(master, text="x", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="white",
command=lambda: self.action("x")).grid(row=2, column=3, sticky=N+S+E+W)
Button(master, text="-", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="white",
command=lambda: self.action("-")).grid(row=3, column=3, sticky=N+S+E+W)
Button(master, text="÷", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="white",
command=lambda: self.action("/")).grid(row=1, column=3, sticky=N+S+E+W)
Button(master, text="%", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="white",
command=lambda: self.action("%")).grid(row=4, column=2, sticky=N+S+E+W)
Button(master, text="7", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("7")).grid(row=1, column=0, sticky=N+S+E+W)
Button(master, text="8", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("8")).grid(row=1, column=1, sticky=N+S+E+W)
Button(master, text="9", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("9")).grid(row=1, column=2, sticky=N+S+E+W)
Button(master, text="4", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("4")).grid(row=2, column=0, sticky=N+S+E+W)
photo = PhotoImage(file='number_5.png')
label_photo = Label(master, image=photo)
label_photo.photo = photo
label_photo.grid(column=1, row=2, sticky=N+S+E+W)
Button(master, image=photo, width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("5")).grid( pady=2, padx=2, row=2, column=1, sticky=N+S+E+W)
Button(master, text="6", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("6")).grid(row=2, column=2, sticky=N+S+E+W)
Button(master, text="1", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("1")).grid(row=3, column=0, sticky=N+S+E+W)
Button(master, text="2", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("2")).grid(row=3, column=1, sticky=N+S+E+W)
Button(master, text="3", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action("3")).grid(row=3, column=2, sticky=N+S+E+W)
Button(master, text="0", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="light blue",
command=lambda: self.action(0)).grid(row=4, column=0, sticky=N+S+E+W)
Button(master, text=".", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="azure3",
command=lambda: self.action(".")).grid(row=4, column=1, sticky=N+S+E+W)
Button(master, text="(", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="azure3",
command=lambda: self.action("(")).grid(row=2, column=4, sticky=N+S+E+W)
Button(master, text=")", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="azure3",
command=lambda: self.action(")")).grid(row=2, column=5, sticky=N+S+E+W)
Button(master, text="S_root", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="azure3",
command=lambda: self.squareroot()).grid(row=3, column=4, sticky=N+S+E+W)
Button(master, text="x²", width=5, height=3,font=("Helvetica", 15),
fg="black", bg="azure3",
command=lambda: self.square()).grid(row=3, column=5, sticky=N+S+E+W)
window = Tk()
obj = calc(window)
window.mainloop()