Python Tkinter Frame - making frame width span full root window width - python

I am learning Python GUI programming with tkinter. I wanted to place a frame in my root window using the grid geometry manager, specify a height, and have the frame expand to the full width of the root window. I tried to do this using the sticky options but this does not produce the desired result. How do I make the frame expand to the full width of the window without manually specifying the width?
Code:
import tkinter
import tkinter.ttk
win = tkinter.Tk()
win.geometry('600x600')
frame = tkinter.Frame(win, height=300)
frame.configure(bg='red')
frame.grid(column=0, sticky=tkinter.E + tkinter.W)
win.mainloop()

I believe this code will achieve the result you are looking for (note that call to grid_columnconfigure is on win, which is the parent of your frame widget):
import tkinter
import tkinter.ttk
win = tkinter.Tk()
win.geometry('600x600')
frame = tkinter.Frame(win, bg='red', height=300)
frame.grid(row=0, column=0, sticky='ew')
win.grid_columnconfigure(0,weight=1)
win.mainloop()

Related

How to set Font size to fit a specific width of the frame in tkinter

actually I am making a project with the help of tkinter in python so basically I want to shrink my font size of a label according to the width of the frame that it will be put in. I want that just i am giving a string and it will automatically adjust the size of the font according to the width.
For an Example:- Let I am Giving a String that is " Hotel Raj Kundra and Family Resturant", let the width of the Frame/label is 500.
so how it will automatically adjust in this size of window without wrapped the text. just fit in the window size
with .config you can adjust Attributes after you placed it.
Now you can adjust the window size with the len() of your string.
I hope it helps.
import tkinter as tk
def adjust_Window(canvas, text):
canvas.config(width=len(text)*50)
def main():
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack()
adjust_Window(canvas, "i hope it helps <3")
root.mainloop()
if __name__ == '__main__':
main()
Easier way to do this:
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the window
win.geometry("700x350")
def update_width():
l.configure(text='Hotel Raj Kundra and Family Resturant', background='blue', foreground='white', font=("Calibri,8,itlaic"))
# Create a frame
frame=Frame(win, background="skyblue3", width=700, height=250)
frame.pack()
# Add a button in the main window
ttk.Button(win, text="Update", command=update_width).pack()
l = ttk.Label(win, background='red', text="so how it will automatically adjust in this size of window without wrapped the text. just fit in the window siz" , font=("Calibri,32,Bold"))
l.pack()
win.mainloop()
Result widest:
Result to shrink:

(Tkinter) Get height of frame before it is packed

As the title suggests, I'm wondering if it's possible to get the height of a frame full of widgets before it is packed and displayed.
Here is some example code:
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, bg='green')
label = tk.Label(frame, text='hello')
label.pack(side='top')
button = tk.Button(frame, text='Test Button')
button.pack(side='top')
print(frame['height']) # Outputs 0
frame.pack(side='top')
root.mainloop()
I want to be able to do this so that if the frame is taller than a specific threshold, it will instead go inside a scrollable canvas.
Thanks.
As stated in TheLizzard's comment, I can do this by using the following code:
...
frame.update()
print(frame.winfo_reqheight())
frame.pack(side='top')
...

Prevent a tkinter window from shrinking while allowing it to expand? [duplicate]

The size of Tkinter windows can be controlled via the following methods:
.minsize()
.maxsize()
.resizable()
Are there equivalent ways to control the size of Tkinter or ttk Frames?
#Bryan: I changed your frame1.pack code to the following:
frame1.pack(fill='both', expand=True)
frame1.bind( '<Configure>', maxsize )
And I added this event handler:
# attempt to prevent frame from growing past a certain size
def maxsize( event=None ):
print frame1.winfo_width()
if frame1.winfo_width() > 200:
print 'frame1 wider than 200 pixels'
frame1.pack_propagate(0)
frame1.config( width=200 )
return 'break'
The above event handler detects that a frame's width is too big, but is unable to prevent the increase in size from happening. Is this a limitation of Tkinter or have I misunderstood your explanation?
There is no single magic function to force a frame to a minimum or fixed size. However, you can certainly force the size of a frame by giving the frame a width and height. You then have to do potentially two more things: when you put this window in a container you need to make sure the geometry manager doesn't shrink or expand the window. Two, if the frame is a container for other widget, turn grid or pack propagation off so that the frame doesn't shrink or expand to fit its own contents.
Note, however, that this won't prevent you from resizing a window to be smaller than an internal frame. In that case the frame will just be clipped.
import Tkinter as tk
root = tk.Tk()
frame1 = tk.Frame(root, width=100, height=100, background="bisque")
frame2 = tk.Frame(root, width=50, height = 50, background="#b22222")
frame1.pack(fill=None, expand=False)
frame2.place(relx=.5, rely=.5, anchor="c")
root.mainloop()
A workaround - at least for the minimum size: You can use grid to manage the frames contained in root and make them follow the grid size by setting sticky='nsew'. Then you can use root.grid_rowconfigure and root.grid_columnconfigure to set values for minsize like so:
from tkinter import Frame, Tk
class MyApp():
def __init__(self):
self.root = Tk()
self.my_frame_red = Frame(self.root, bg='red')
self.my_frame_red.grid(row=0, column=0, sticky='nsew')
self.my_frame_blue = Frame(self.root, bg='blue')
self.my_frame_blue.grid(row=0, column=1, sticky='nsew')
self.root.grid_rowconfigure(0, minsize=200, weight=1)
self.root.grid_columnconfigure(0, minsize=200, weight=1)
self.root.grid_columnconfigure(1, weight=1)
self.root.mainloop()
if __name__ == '__main__':
app = MyApp()
But as Brian wrote (in 2010 :D) you can still resize the window to be smaller than the frame if you don't limit its minsize.

How can I center a label using Tkinter's grid method in Python

I'm trying to center a Label in Tkinter. I am using the grid() method to show my widgets.
In the code below I created a frame and put the label in that frame.
The sticky method doesn't seem to work for some reason.
I have tried sticky='WE' but the label is still stuck to the left side of the frame.
'''
from tkinter import *
from tkinter import ttk
mw=Tk()
mw.title('Window')
mw_width, mw_height = 300, 200
mw.minsize(mw_width,mw_height)
frame1=Frame(mw)
frame1.grid(row=0,column=0)
main_label=ttk.Label(frame1,text='My label')
main_label.grid(row=0,column=0,sticky='WE')
mw.mainloop()
'''
grid by default will center a widget in the space allocated to it. The problem is that you've not configured the column to grow to fill the window. Also by default, a row or column will be just large enough to fit the widgets in it.
To do that, you need to give column 0 a non-zero weight:
mw.grid_columnconfigure(0, weight=1)
Do you need to use the grid() method for your layout? pack() would automatically center it.
from tkinter import *
from tkinter import ttk
mw=Tk()
mw.title('Window')
mw_width, mw_height = 300, 200
mw.minsize(mw_width,mw_height)
frame1=Frame(mw)
frame1.pack()
main_label=ttk.Label(frame1,text='My label', justify="center")
main_label.pack()
mw.mainloop()
You could also use place().
from tkinter import *
from tkinter import ttk
mw = Tk()
mw.title('Window')
mw_width, mw_height = 300, 200
mw.minsize(mw_width, mw_height)
frame1 = Frame(mw)
frame1.place(width=mw_width, height=mw_height)
main_label = ttk.Label(frame1, text='My label', justify="center")
main_label.place(relx=.5, rely=0, anchor="n")
mw.mainloop()

How to set the min and max height or width of a Frame?

The size of Tkinter windows can be controlled via the following methods:
.minsize()
.maxsize()
.resizable()
Are there equivalent ways to control the size of Tkinter or ttk Frames?
#Bryan: I changed your frame1.pack code to the following:
frame1.pack(fill='both', expand=True)
frame1.bind( '<Configure>', maxsize )
And I added this event handler:
# attempt to prevent frame from growing past a certain size
def maxsize( event=None ):
print frame1.winfo_width()
if frame1.winfo_width() > 200:
print 'frame1 wider than 200 pixels'
frame1.pack_propagate(0)
frame1.config( width=200 )
return 'break'
The above event handler detects that a frame's width is too big, but is unable to prevent the increase in size from happening. Is this a limitation of Tkinter or have I misunderstood your explanation?
There is no single magic function to force a frame to a minimum or fixed size. However, you can certainly force the size of a frame by giving the frame a width and height. You then have to do potentially two more things: when you put this window in a container you need to make sure the geometry manager doesn't shrink or expand the window. Two, if the frame is a container for other widget, turn grid or pack propagation off so that the frame doesn't shrink or expand to fit its own contents.
Note, however, that this won't prevent you from resizing a window to be smaller than an internal frame. In that case the frame will just be clipped.
import Tkinter as tk
root = tk.Tk()
frame1 = tk.Frame(root, width=100, height=100, background="bisque")
frame2 = tk.Frame(root, width=50, height = 50, background="#b22222")
frame1.pack(fill=None, expand=False)
frame2.place(relx=.5, rely=.5, anchor="c")
root.mainloop()
A workaround - at least for the minimum size: You can use grid to manage the frames contained in root and make them follow the grid size by setting sticky='nsew'. Then you can use root.grid_rowconfigure and root.grid_columnconfigure to set values for minsize like so:
from tkinter import Frame, Tk
class MyApp():
def __init__(self):
self.root = Tk()
self.my_frame_red = Frame(self.root, bg='red')
self.my_frame_red.grid(row=0, column=0, sticky='nsew')
self.my_frame_blue = Frame(self.root, bg='blue')
self.my_frame_blue.grid(row=0, column=1, sticky='nsew')
self.root.grid_rowconfigure(0, minsize=200, weight=1)
self.root.grid_columnconfigure(0, minsize=200, weight=1)
self.root.grid_columnconfigure(1, weight=1)
self.root.mainloop()
if __name__ == '__main__':
app = MyApp()
But as Brian wrote (in 2010 :D) you can still resize the window to be smaller than the frame if you don't limit its minsize.

Categories

Resources