Ive been playing around with a Page Switching example from here: Switch between two frames in tkinter
Im trying to use the "place()" layout manager to sort widgets on the page but whenever I use it, they simply don't appear on the page. It's important I use it over pack and grid for the program I want to create, how can I make them work?
For example, in the following code, "Welcome" doesn't show up on the "StartTest" page.
import tkinter as tk
import random
title_font=("Microsoft Jhenghei UI Light", 35)
class MathsApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(StartTest)
self.title("Maths Revision App")
self.geometry("800x500")
self.configure(bg="white")
def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
class StartTest(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
#welcome label
tk.Label(self, text="Welcome!", font=title_font, bg="white", fg="#004d99").place(x=30, y=20)
tk.Button(self, text="Open page one",
command=lambda: master.switch_frame(PageOne)).pack()
tk.Button(self, text="Open page two",
command=lambda: master.switch_frame(PageTwo)).pack()
class PageOne(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
tk.Label(self, text="This is page one").pack(side="top", fill="x", pady=10)
tk.Button(self, text="Return to start page",
command=lambda: master.switch_frame(StartTest)).pack()
class PageTwo(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
tk.Label(self, text="This is page two").pack(side="top", fill="x", pady=10)
tk.Button(self, text="Return to start page",
command=lambda: master.switch_frame(StartTest)).pack()
if __name__ == "__main__":
app = MathsApp()
app.mainloop()
I tried removing the other buttons on the page, but the "Welcome" Label still didn't show up.