user2565422
user2565422

Reputation: 237

tkinter making frame sticky with window

I am new to this so not sure where I am going wrong here. I want to make my Frame_1 stick to the four corners of the window as you drag it out from the bottom right hand corner.

from tkinter import *
from tkinter import scrolledtext
from tkinter import ttk

window = Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)

tab1 = ttk.Frame(tab_control)
tab1.grid(row=0, column=0)

tab2 = ttk.Frame(tab_control)
tab2.grid(row=0, column=0)

tab_control.grid(row=0, column=0, sticky=NSEW)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')

labe1frame_1 = LabelFrame(tab1, text="Frame_1")
labe1frame_1.grid(row=0, column=0, padx=10, pady=10, sticky=NSEW)

txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.grid(row=0, column=0)

window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
labe1frame_1.rowconfigure(0, weight=1)
labe1frame_1.columnconfigure(0, weight=1)

window.mainloop()

Upvotes: 1

Views: 12158

Answers (4)

Module_art
Module_art

Reputation: 1068

The width of a grid column inside a given widget will be equal to the width of its widest cell, and the height of a grid row will be the height of its tallest cell. The sticky attribute on a widget controls only where it will be placed if it doesn't completely fill the cell.

from tkinter import *
from tkinter import scrolledtext
from tkinter import ttk

window = Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)

tab1 = ttk.Frame(tab_control)
tab1.grid(row=0, column=0, sticky=NSEW) #add sticky option

tab2 = ttk.Frame(tab_control)
tab2.grid(row=0, column=0)

tab_control.grid(row=0, column=0, sticky=NSEW)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')

labe1frame_1 = LabelFrame(tab1, text="Frame_1")
labe1frame_1.grid(row=0, column=0, padx=10, pady=10, sticky=NSEW)


txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.grid(row=0, column=0, sticky=NSEW) #add sticky option

window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
labe1frame_1.rowconfigure(0, weight=1)
labe1frame_1.columnconfigure(0, weight=1)

#configure the row and column size of parent window
tab1.columnconfigure(0,weight=3)
tab1.rowconfigure(0,weight=3)

window.mainloop()

Upvotes: 0

Reblochon Masque
Reblochon Masque

Reputation: 36712

In your current GUI set up, using pack throughout may be a better idea:

import tkinter as tk

from tkinter import scrolledtext
from tkinter import ttk


if __name__ == '__main__':

    window = tk.Tk()
    window.title("My Program")
    tab_control = ttk.Notebook(window)

    tab1 = tk.Frame(tab_control)
    tab1.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

    tab2 = tk.Frame(tab_control)
    tab2.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

    tab_control.pack(fill=tk.BOTH, expand=True)
    tab_control.add(tab1, text='First')
    tab_control.add(tab2, text='Second')

    labe1frame_1 = tk.LabelFrame(tab1, text="Frame_1")
    labe1frame_1.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

    txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
    txtbox.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

    window.mainloop()

Upvotes: 2

Mike - SMT
Mike - SMT

Reputation: 15226

You can allow your textbox and Frame 1 to expand by adding a weight to tab1 and a sticky to textbox.

When using grid() you will want to use columnconfig() and rowconfig() to provide weights to that frame so it can expand with the window resizing.

For the textbox to expand with the frame you will need to add the sticky argument also like this:

txtbox.grid(row=0, column=0, sticky="nswe")

See below code.

from tkinter import *
from tkinter import scrolledtext
from tkinter import ttk

window = Tk()
window.title("My Program")

tab_control = ttk.Notebook(window)


tab1 = ttk.Frame(tab_control)
tab1.grid(row=0, column=0)
tab1.columnconfigure(0, weight=1) # added weight
tab1.rowconfigure(0, weight=1) # added weight

tab2 = ttk.Frame(tab_control)
tab2.grid(row=0, column=0)

tab_control.grid(row=0, column=0, sticky="nswe")
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')

labe1frame_1 = LabelFrame(tab1, text="Frame_1")
labe1frame_1.grid(row=0, column=0, padx=10, pady=10, sticky="nswe")

txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.grid(row=0, column=0, sticky="nswe")  # added sticky

window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
labe1frame_1.rowconfigure(0, weight=1)
labe1frame_1.columnconfigure(0, weight=1)

window.mainloop()

Upvotes: 0

AD WAN
AD WAN

Reputation: 1474

You can pack your Labeframe and scrolledText with this command to achieve that pack(expand=True, fil=BOTH) by removing the grid geometry layout manager.

from tkinter import *
from tkinter import scrolledtext
from tkinter import ttk

window = Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)

tab1 = ttk.Frame(tab_control)
tab1.grid(row=0, column=0)

tab2 = ttk.Frame(tab_control)
tab2.grid(row=0, column=0, sticky=NSEW)

tab_control.grid(row=0, column=0, columnspan=3, padx=10, pady=10, sticky=E+W+N+S)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')

labe1frame_1 = LabelFrame(tab1, text="Frame_1")
labe1frame_1.pack(expand=True, fil=BOTH)

txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.pack(expand=True, fil=BOTH)

window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
labe1frame_1.rowconfigure(0, weight=1)
labe1frame_1.columnconfigure(0, weight=1)

window.mainloop()

Upvotes: 0

Related Questions