0

hi i am trying to resize images together with buttons on a grid whenever program window is resized,most of the guides just teach how to resize buttons but buttons or text inside of them stays the same .i think that this is possible with pack() but not sure and would like to know if its possible with grid.

thanks for your time.

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()

  • You cannot change the size of an image with `grid` or `pack`. – Bryan Oakley Feb 25 '20 at 16:24
  • tkinter has very limited functionality for resizing images (basically `PhotoImage.zoom` and `Photoimage.subsample`) most would recommend either altering the images before you import them so they're already the correct size, or use `Image.resize` from the `PIL` module https://pillow.readthedocs.io/en/stable/ – tgikal Feb 25 '20 at 17:38
  • You can get what you want by binding the `""` event to the widget which holds the `image`. The widget have to be allowed to resize, e.g. `.grid_*configure(.., weight=1)`. For reference: [Tkinter button expand using grid](https://stackoverflow.com/a/53079377/7414759) – stovfl Feb 25 '20 at 17:53

0 Answers0