Tkinter - How to stop the size of a frame change when adding a widget?

In the frame of the page, I weighed two frames inside the innerFrame so that each of them took up half the screen, however, when I add the widget to one of these frames, I used the list as an example, how big it is, one of the frames now takes longer than the other . How to make sure that frames do not change size when adding a widget, and each of them remains half the size of the window?

No list

With listbox

Here is my code:

import tkinter as tk from tkinter import ttk class Program(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) tk.Tk.iconbitmap(self, default = "") tk.Tk.wm_title(self, "") container = tk.Frame(self) container.pack(side="top", fill="both", expand=True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} for F in (Page, Other): frame = F(container, self) self.frames[F] = frame frame.grid(row = 0, column = 0, sticky = "nsew") self.show_frame(Page) def show_frame(self,cont): frame = self.frames[cont] frame.tkraise() class Page(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) innerFrame = tk.Frame(self, bg="red") innerFrame.place(relx=.5, rely=.5, anchor="c", relwidth=1.0, relheight=1.0) innerFrame.grid_rowconfigure(0, weight=1) innerFrame.grid_rowconfigure(1, weight=1) innerFrame.grid_columnconfigure(0, weight=1) #First Half frameOne = tk.Frame(innerFrame, bg="pink") frameOne.grid(row=0, sticky="NSWE") #Second Half frameTwo = tk.Frame(innerFrame, bg="green") frameTwo.grid(row=1, sticky="NSWE") lb = tk.Listbox(frameTwo) lb.pack(fill="both", expand=True) class Other(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) app = Program() app.state('zoomed') app.mainloop() 
+5
source share
2 answers

The grid takes a uniform parameter, which takes an arbitrary value. All rows (or all columns) with the same value are considered part of the "single group". This causes the rows (or columns) to be the same size in proportion to their weight.

If you have two lines in a frame, both lines have the same weight, and both belong to the same homogeneous group, each of them will occupy exactly 50% of the available space.

In your case, you can do this:

 innerFrame.grid_rowconfigure(0, weight=1, uniform="x") innerFrame.grid_rowconfigure(1, weight=1, uniform="x") 

(again, "x" is arbitrary, it can be any value if it is the same for both lines)

The official documentation for tk (on which tkinter is built) describes this as follows (see http://tcl.tk/man/tcl8.5/TkCmd/grid.htm#M24 )

An optional parameter, when a non-empty value is supplied, puts the line in a homogeneous group with other lines that have the same value for -uniform. The space for rows belonging to a homogeneous group is distributed so that their sizes are always in strict accordance with their weight values.

...

When several rows or columns belong to a single group, the space allocated for them is always proportional to their weights. (A zero weight is considered equal to 1.) In other words, a row or column configured with a weighted 1-uniform a will have the same size as any other row or column configured with a weighted 1-uniform a. A row or column configured with a weighted 2-uniform b will be twice as large as one configured with a weighted 1-shaped b.


Note. The uniform parameter does not appear in the tkinter documentation, but this is a perfectly valid option. It has been part of tk (on which tkinter is built) for many, many years.

+4
source

You can use max-size , min-sixe for the frame:

 master = tk() master.minsize(width=777, height=777) master.maxsize(width=777, height=777) 
+1
source

Source: https://habr.com/ru/post/1242122/


All Articles