Title: Tkinter: Using the same frame to display data based on user input
Tags: python;tkinter
Question: Is there a way to use the same frame to display two different layouts of data based on user input? For example, if I have a button to make a variable either true or false, is there a way to show different text, labels, and data on the same frame based on if that variable is true or false? Something like this:
self.frame1 = Frame(root)
self.frame1.pack()
def page1(self):
self.text1 = Label(self.frame1, text='Text 1')
self.text1.pack()
def page2(self):
self.text2 = Label(self.frame1, text='Text 2')
self.text2.pack()
self.page1_or_page2 = True
if self.page1_or_page2 == True:
page1(self)
page2(self)
Also, is overlapping data on a frame the best way to achieve something like this? Thanks!

Here is the accepted answer: You can do it by associating a command to a Checkbutton that will change the widgets displayed in the frame. You can use pack_forget to remove the widgets displayed in the frame before packing new ones.
Here is an example:
import tkinter as tk
def toggle_details():
if variable.get():
text1.pack_forget()
text2.pack()
else:
text2.pack_forget()
text1.pack()
root = tk.Tk()
frame = tk.Frame(root)
text1 = tk.Label(frame, text='short text')
text2 = tk.Label(frame, text='short text\n------\nwith details below')
variable = tk.BooleanVar(root)
button = tk.Checkbutton(root, text='Show details', command=toggle_details, variable=variable)
text1.pack()
frame.pack()
button.pack(side='bottom')
root.mainloop()
Comment for this answer: In this simple case _one_ `Label` would have been enough and then simply changing the text of that label. And for more widgets in that frame I would put those into a frame themselves so you have just one widget to layout and forget. And somehow you are almost reinventing `ttk.Notebook` here.
Comment for this answer: Great answer! Do the order geography methods have something like `pack_forget()` as well?
Comment for this answer: Fantastic! Well let me fool around with the code today and I will keep my post updated!
Comment for this answer: @Evan Yes, `grid_forget` and `place_forget` also exist.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:08/08/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><tkinter>', 'Title': 'Tkinter: Using the same frame to display data based on user input', 'ViewCount': 859, 'id': 45568841, 'user_AboutMe': '<p>Part time rookie programmer, full time student. I study Computer Science at a local university.</p>\n', 'user_AccountId': 9865590.0, 'user_DisplayName': 'GreenSaber', 'user_Reputation': 1090.0, 'user_Views': 163.0, 'user_id': 7307172.0}}