I'm trying to make a tkinter application(my first real code) but I'm having a little trouble with the interface. I want to get my widgets to all dynamically resize as the window resizes. I managed to make my background image resize itself to fit the entire window size, as the window is resized by the user, by writing a function that resizes the PIL image. Here is the function:
def resizeimage(self,event) :
width, height = self.winfo_width(), self.winfo_height()
image = self.bg_image.resize((width,height))
self.image1 = ImageTk.PhotoImage(image)
self.bg_label.config(image = self.image1)
I've bound this function to the label that displays the background image like this:
self.bg_image = Image.open("project_pics\\start_pg.png")
bg_image = ImageTk.PhotoImage(self.bg_image)
self.bg_label = Label(self,image=bg_image)
self.bg_label.image = bg_image
self.bg_label.bind('<Configure>',self.resizeimage)
self.bg_label.grid(sticky="nwse")
here, self.bg_image
is the image to be displayed as background and self.bg_label
is the label that displays the image. The reason why I wrote this code to resize the background image was because, when I ran my application on a different computer, since the new one had a different screen size, the background image didn't fit the size of the screen as I would have liked it to. This function solved that problem but then I noticed a new problem. The other widgets on the screen remained the same size. So when the window was shrunk, you could see that all the widgets remained the same size and just bunched together on the smaller screen. I know that I could write some function similar to resizeimage()
to resize all the other widgets in proportion to the window and similar to the background image, bind all the widgets on the screen to such a function but it seems very tedious and not very efficient. I'm looking for a more dynamic solution .
This is how The window looks normally :
This is how it looks when The window is shrunk:
My question is this. Is there any way I could dynamically resize all the widgets on the window so that they don't bunch up like in image 2 but resize themselves in proportion to the window so that they look just like image 1 when shrunk, just smaller? I think a similar question was asked here but the solution given didn't quite answer the entire question.