Is there a way to find dimensions of a frame in tkinter? - python

I am trying to append an image to a frame I have. However, I have many frames within each other so I don't know what the dimensions of this frame are.
Is there any function or anything that will output the dimensions of the frame like "100x500", for example?
Thanks

Every widget has the methods winfo_width and winfo_height which return the current dimensions of the widget. You can also use winfo_reqwidth and winfo_reqheight to get the requested width and height. The two can be different if the size of the widget changes due to how it is managed (eg: when using sticky with grid, or fill with pack.
If the window has not yet actually been rendered, the width and height returned by winfo_width and winfo_height will be 1.

Related

Set size for a frame

What is the point of setting a size for a frame on Tkinter, if it will resize every time I add a new widget.
`
frame1 = Frame(self.MainWindow,width=600,height=100,bg='Blue')
frame1.grid(row=0,column=0)
#frame1.pack()
#Title
self.mainTitle = Label(frame1,text='Welcome')
self.mainTitle.grid(row=0,column=1,padx=60,pady=5)
#Back button
backButton = Button(frame1,text='Home',command=None)
backButton.grid(row=0,column=0,padx=60,pady=5)
nexButton = Button(frame1,text='Next',command=None)
nexButton.grid(row=0,column=2,padx=60,pady=5)
`
I do not know if there is any way to work with frames like divs in html.
What is the point of setting a size for a frame on Tkinter, if it will resize every time I add a new widget.
The vast majority of the time there is no advantage for setting a size on a frame. It can be useful in those cases when you have very strict requirements, but more often than not it's better to let tkinter size frames for you. It's very good at making responsive GUIs when you let it do all of the work of calculating sizes.

Python QT5 - QTWidget Layout grow/shrink to maximumSize/minmumSize

I have a layout question for python with qt5.
So, there is a main window with a normal vertical layout with 2 widgets.
The first one is a widget with a scrollarea which has a minimumSize & maximumSize set.
The second one is another widget with yet another scrollarea widget.
My goal is this. I load some content in the first widget and depending on the content it should either shrink or grow to the set min/max sizes and if it gets bigger than maxsize the scrollarea should take effect.
The second widget should always take all the rest of the available space.
I've tried all kinds of sizePolicy combinations but can't get it to quite to work. The second widget has a sizePolicy of Prefered/Prefered with vertical stretch set to 1, so it takes all the available space for itself.
The 1st widget has a miniumSize of 100 and a maximumSize of 250. So ideally it should shrink to something between 100-250 if the content is less and it should grow to 250 and activate the scrollbar if the content is getting bigger.
If I update the content of widget one (i.e. by clicking on a button) it should 1. resize/shrink/grow so the content fits, but not smaller than 100 and not larger than 250. If I have less content I should not be able to make widget one reszie to 250 - it shoudl always just take the exact needed height.
Any idea on how to do that?
example layout
Your scrollarea isn't going to change size, because it's a scrollarea. It doesn't need to size itself to its content, because the size of the contents of a scrollarea is basically unbounded. You can just scroll more. If you want to change the size of the first scrollarea, you're going to have to set its height manually.

How to resize images in a canvas dynamically in tkinter?

In Tkinter, resizing a canvas and/or frame can be done using
canvas.pack(fill="both", expand=True)
This way I can drag the tkinter window with the mouse and the canvas and frames within will adapt to the new size.
However I have not found a solution for applying this to images within the canvas. Only solutions so far are to independently change the size of the images through event actions.
Is there any way to make images within a canvas to resize dynamically, just like the canvas does with the one-liner above?
Is there any way to make images within a canvas to resize dynamically, just like the canvas does with the one-liner above?
No, there is no way to do what you want. Images aren't like widgets which can automatically grow and shrink. You will need to set up a binding on the <Configure> event of the containing widget, and in the bound function you will have to convert the image to the desired size.

Treat Label as image when not displaying an image?

I have a Label that will eventually display an image, but when the program starts I just want it to be blank. However, I also need to set the width and height of the Label in order to make the layout look good.
Because Label treats width and height as number of lines/characters when the image attribute isn't set, things look very wrong.
So how can I make the Label behave as an image (pixel width/height) without actually setting an image?

Tkinter - text widget distorting column sizes

I am using the grid manager and have two frames side by side, and five columns with 1 button in each below the two frames in a second row, evenly spaced. All use "sticky" NSEW since I want them to scale proportionally if I enlarge the window.
When I add a text entry widget to the right frame, it distorts the buttons below them so they are larger than those to the left. I can't figure out how to prevent this distortion, or put another way, how to keep each column the same size.
Is there a reason why the text entry widget is not respective the row/col/weighting? Thanks in advance!
You can try to use columnspan or rowspan. This should allow the buttons to be unaffected by the text widget.
for example:
root=Tk()
button1=Button(root,text="button1")
button1.grid(row=1,column=0)
textbox=Text(root)
textbox.grid(row=1,column=1,rowspan=2)
In each frame you should set the "propagate" status to false, this will keep the frame from resizing based on what is inside. So if the frame uses grid() set grid_propagate(False) and so on.
I think this is due to the fact you've given the entry widget a specific width (or are accepting the default). Since the widget wants to be a particular size, it will cause the column to grow in order to fit the requested size of its children.
One solution is to set the size of the entry widget to 1. Then, because of the sticky settings for E and W, it will expand to exactly fit the column.

Categories