Show and hide frames in tkinter

How to show and hide frames in tkinter with pack_forget.

import tkinter as tk

on = 1
def close_frame(evt):
	global on, frame, lbx

	if on:
		frame.pack_forget()
		on = 0
	else:
		frame.pack_forget()
		frame1.pack_forget()
		create_frame()
		on = 1



root = tk.Tk()

def create_frame():
	"""create frame to be hidden when we press k"""
	global lbx, lbx1, frame, frame1

	frame = tk.Frame(root)
	frame.pack(side="left")
	lbx = tk.Listbox(frame, bg="gold")
	lbx.pack()
	lbx.insert(0, 1)
	frame1 = tk.Frame(root)
	frame1.pack()
	lbx1 = tk.Listbox(frame1, bg="cyan")
	lbx1.pack(side="left")
	lbx1.insert(0, 2)



create_frame()


root.bind("<k>", close_frame)

root.mainloop()
Explaining the code to hide and show a frame in tkinter

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.