Skip to content

Instantly share code, notes, and snippets.

@piti118
Created April 4, 2022 13:37
Show Gist options
  • Save piti118/c98ecae034b33c63de2b5a15904db44a to your computer and use it in GitHub Desktop.
Save piti118/c98ecae034b33c63de2b5a15904db44a to your computer and use it in GitHub Desktop.
tkinter resizable layout via grid weight
import tkinter as tk
def asf(frame, prefix):
lb = tk.Listbox(frame)
for i in range(10):
lb.insert(i, f'{prefix} {i}')
return lb
def make_top_left_frame(root):
tl = tk.Frame(root, background="red", width=100, height=50)
lb = asf(tl, 'tl')
lb.pack(expand=True, fill=tk.BOTH)
return tl
def make_tr_list_box(root):
return asf(root, 'tr')
def make_hello_bar(root):
frame = tk.Frame(root, background='yellow', border=10)
frame.columnconfigure(1, weight=1)
btn = tk.Button(frame, text='hello')
btn.grid(row=1, column=1, sticky='e')
return frame
def make_top_right_frame(root):
frame = tk.Frame(root, background="orange", width=100, height=50)
frame.rowconfigure(1, weight=1)
frame.columnconfigure(1, weight=1)
lb = asf(frame, 'tr')
lb.grid(row=1, column=1, sticky='nsew')
hello_bar = make_hello_bar(frame)
hello_bar.grid(row=2, column=1, sticky='nsew')
return frame
def make_bottom_left_frame(root):
bl = tk.Frame(root, background="green", width=100, height=50)
return bl
def make_bottom_right_frame(root):
br = tk.Frame(root, background="blue", width=100, height=50)
return br
def make_root():
root = tk.Tk()
root.columnconfigure(2, weight=1)
root.rowconfigure(1, weight=1)
tl = make_top_left_frame(root)
tl.grid(row=1, column=1, sticky='nsew')
tr = make_top_right_frame(root)
tr.grid(row=1, column=2, sticky='nsew')
bl = make_bottom_left_frame(root)
bl.grid(row=2, column=1, sticky='nsew')
br = make_bottom_right_frame(root)
br.grid(row=2, column=2, sticky='nsew')
return root
def main():
root = make_root()
root.mainloop()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment