Grid function in Tkinter not working when creating multiple pages - python

I am trying to use the grid function instead of the pack function in Tkinter so I can arrange my widgets better, however it is not working. I get this error message:
cannot use geometry manager pack inside .!frame.!startpage which already has slaves managed by grid
Any help would be appreciated. Scroll down to see where I have used grid, I have left a comment.
import tkinter as tk
from tkinter import ttk
from tkcalendar import *
LARGE_FONT= ("Verdana", 12)
class StudyFriendO(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("StudyFriendO") #naming window title
self.geometry('850x830') #size of window
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, HomePage, ToDoPage, TimetablePage): #list of multiple frames of program
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame): #creating start page
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="StudyFriendO", font = LARGE_FONT)
label.grid(row=1, column=2) #HERE IS WHERE I USE GRID
cal = Calendar(self, background="#e0f6fc", disabledbackground="white", bordercolor="light blue", headersbackground="light blue", normalbackground="#e0f6fc", foreground="black", normalforeground='black', headersforeground='white', selectmode="day", year=2021, month=8, day=9)
cal.pack(pady=20 ) #calendar
button1 = ttk.Button(self, text="Enter",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.pack()
class HomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Home", font = LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="To Do",
command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="Timetable",
command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="Home",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.pack()
class ToDoPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="To Do", font = LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Home",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="Timetable",
command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="To Do",
command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
button1.pack()
class TimetablePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Timetable", font = LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Home",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="Timetable",
command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="To Do",
command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
button1.pack()
app = StudyFriendO()
app.mainloop()

It already has slaves manages by grid, either use pack or grid but don't mix them up

Related

How to change Label according to user input in tkinter app?

I am creating a planning app and I want it to be personalized by having a title that says [Name]'s Planner. The user can enter their name into a text entry widget, press Submit, and then it will be displayed in the label. My application also has multiple pages, so the text entry will be on the start page and the label will be on the home page.
I have named the text entry widget nameentry and have created a function that takes the data from nameentry and displays it in the label name_label, however, I am getting an error message when I try to call the function by clicking the Submit button which says NameError: name 'nameentry' is not defined.
I am confused as I believe I have already defined nameentry by saying it equals the text entry widget.
import calendar
import tkinter as tk
from tkinter import ttk
from tkcalendar import *
from datetime import datetime
#calendar and display in label
#window = Tk()
#window.title("StudyFriendO")
LARGE_FONT= ("Verdana", 24)
class StudyFriendO(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("StudyFriendO") #naming window title
self.geometry('850x830') #size of window
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, HomePage, ToDoPage, TimetablePage): #list of multiple frames of program
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def set_date(self, cont):
#frame: StartPage = self.frames[cont]
#frame.date_label.config(text="Today's date is " + datetime.today().strftime("%B %d, %Y")) #function to get date from
frame2: HomePage = self.frames[HomePage]
frame2.date_label.config(text= datetime.today().strftime("%B %d, %Y")) #function to get date from
def getname(self, cont): #FUNCTION WHICH GRABS NAME FROM TEXT ENTRY
frame2: HomePage = self.frames[HomePage]
frame2.name_label.configure(text=nameentry.get() + "'s Planner ")
class StartPage(tk.Frame): #creating start page
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="StudyFriendO", font = LARGE_FONT, bg="#f2fcff")
label.pack(fill="x")
#place(x=315,y=100)
photo1 = tk.PhotoImage(file='apple.png') #photo1 is a variable
#label (window, image=photo1, bg="black") .grid(row=0, column=0, sticky=E)
panel = tk.Label(self, image = photo1)
panel.image = photo1
panel.pack(fill="x")
#place(x=270,y=150)
label2 = tk.Label(self, text="Enter today's date and your first name below:", bg="#f2fcff")
label2.pack(fill="x", ipady=20)
#place(x=305, y=400)
cal = Calendar(self, background="#e0f6fc", disabledbackground="white", bordercolor="light blue", headersbackground="light blue", normalbackground="#e0f6fc", foreground="black", normalforeground='black', headersforeground='white', selectmode="day", year=2021, month=8, day=9)
cal.place(relx=0.5, rely=0.5, anchor='center')
#(x=300,y=430) #calendar
#TEXT ENTRY WHERE PERSON INPUTS NAME
nameentry = tk.Entry(self, width=20, bg="white") #text input for users name
nameentry.place(relx=0.5, rely=0.68, anchor='center')
#(x=365, y=635)
caldate = ttk.Button(self, text="Submit",
command=lambda: controller.set_date(StartPage)) #button to get current date
caldate.place(relx=0.5, rely=0.63, anchor='center')
button1 = ttk.Button(self, text="Enter",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.place(relx=0.5, rely=0.73, anchor='center')
#(x=387,y=660)
#BUTTON WHICH CALLS FUNCTION WHICH GRABS NAME (CURRENTLY LOCATED IN WEIRD LOCATION)
namebutton = ttk.Button(self, text="Submit", command=lambda: controller.getname(StartPage))
namebutton.place(relx=0.5, y=60)
#self.date_label = tk.Label(self, text="") #label to display date
#self.date_label.pack(pady=20)
self.configure(bg='#f2fcff')
class HomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#label = tk.Label(self, text="Home", font = LARGE_FONT)
#label.pack(pady=10,padx=10)
#width of x
frame = tk.Frame(self, bg="#c0e3ed", height=110)
frame.pack(fill="x")
#LABEL WHICH DISPLAYS NAME FROM TEXT ENTRY
#tk.Label(self, text ="Name's Planner", fg="white", bg="#c0e3ed", font = LARGE_FONT).place(relx=0.5, y=50, anchor='center')
self.name_label = tk.Label(self, text="", font=("Arial Bold", 50))
self.name_label.place(x=0, y=0)
self.date_label = tk.Label(self, text="", bg="#c0e3ed")
# keep placing the label at top right corner
self.date_label.place(relx=1, y=0, anchor='ne')
#pack(side='top', anchor='ne')
#height of Y
tk.Label(self, text="", fg="white", bg="#99cbd8").pack(side="left", fill="y", ipadx=73)
button1 = tk.Button(self, height=2, width=10, bg="#e0f6fc", text="Home", command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.place(x=35, y=145)
button1 = tk.Button(self, height=2, width=10, bg="#e0f6fc", text="To Do", command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
button1.place(x=35, y=225)
#(relx=0.2, rely=0.4, anchor='center')
button1 = tk.Button(self, height=2, width=10, bg="#e0f6fc", text="Timetable", command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
button1.place(x=35, y=305)
photo2 = tk.PhotoImage(file='apple.png') #photo1 is a variable
#label (window, image=photo1, bg="black") .grid(row=0, column=0, sticky=E)
panel = tk.Label(self, image = photo2)
panel.image = photo2
panel.place(x=35, y=400)
self.configure(bg='#f7f6f6')
class ToDoPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="To Do", font = LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Home",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="Timetable",
command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="To Do",
command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
button1.pack()
class TimetablePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Timetable", font = LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Home",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="Timetable",
command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="To Do",
command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
button1.pack()
app = StudyFriendO()
app.mainloop()
The problem is happening because you're trying to reference a variable that's local to one method of one page on another. In an app with this architecture, a good way to share data among the Page class instances is through the controller argument they are all passed when they're created, which in this case is the StudyFriendO class instance.
Since you may have more than one variable to share as you develop your app, I've added a shared_data dictionary attribute and initialized with a tk.StringVar associated with the key 'name'. It is used as the textvariable= option when creating the tk.Entry widget on the StartPage and then also by the getname() method of the StudyFriendO controller. Using a StringVar allows the dictionary item to be defined before the Entry widget using it is created. After that happens (i.e. the StartPage instance is created) the current value of the Entry can be retrieved indirectly through it instead of the widget itself in the getname() method. You can easily add more variables in the future should you need them.
Below is your code with the modifications described. I've indicated where the changes are with #### comments to make them standout.
import calendar
import tkinter as tk
from tkinter import ttk
from tkcalendar import *
from datetime import datetime
#calendar and display in label
#window = Tk()
#window.title("StudyFriendO")
IMAGE_PATH = 'apple.png'
LARGE_FONT= ("Verdana", 24)
class StudyFriendO(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("StudyFriendO") #naming window title
self.geometry('850x830') #size of window
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
#### ADDED
self.shared_data = { # Variables shared by all pages.
'name': tk.StringVar(value='')
}
self.frames = {}
for F in (StartPage, HomePage, ToDoPage, TimetablePage): #list of multiple frames of program
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def set_date(self, cont):
#frame: StartPage = self.frames[cont]
#frame.date_label.config(text="Today's date is " + datetime.today().strftime("%B %d, %Y")) #function to get date from
frame2: HomePage = self.frames[HomePage]
frame2.date_label.config(text= datetime.today().strftime("%B %d, %Y")) #function to get date from
def getname(self, cont): #FUNCTION WHICH GRABS NAME FROM TEXT ENTRY
frame2: HomePage = self.frames[HomePage]
#### frame2.name_label.configure(text=nameentry.get() + "'s Planner ")
frame2.name_label.configure(text=self.shared_data['name'].get() + "'s Planner ")
class StartPage(tk.Frame): #creating start page
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="StudyFriendO", font = LARGE_FONT, bg="#f2fcff")
label.pack(fill="x")
#place(x=315,y=100)
photo1 = tk.PhotoImage(file=IMAGE_PATH) #photo1 is a variable
#label (window, image=photo1, bg="black") .grid(row=0, column=0, sticky=E)
panel = tk.Label(self, image = photo1)
panel.image = photo1
panel.pack(fill="x")
#place(x=270,y=150)
label2 = tk.Label(self, text="Enter today's date and your first name below:", bg="#f2fcff")
label2.pack(fill="x", ipady=20)
#place(x=305, y=400)
cal = Calendar(self, background="#e0f6fc", disabledbackground="white", bordercolor="light blue", headersbackground="light blue", normalbackground="#e0f6fc", foreground="black", normalforeground='black', headersforeground='white', selectmode="day", year=2021, month=8, day=9)
cal.place(relx=0.5, rely=0.5, anchor='center')
#(x=300,y=430) #calendar
#TEXT ENTRY WHERE PERSON INPUTS NAME
#### nameentry = tk.Entry(self, width=20, bg="white") #text input for users name
nameentry = tk.Entry(self, width=20, bg="white", #text input for users name
textvariable=controller.shared_data['name'])
nameentry.place(relx=0.5, rely=0.68, anchor='center')
#(x=365, y=635)
caldate = ttk.Button(self, text="Submit",
command=lambda: controller.set_date(StartPage)) #button to get current date
caldate.place(relx=0.5, rely=0.63, anchor='center')
button1 = ttk.Button(self, text="Enter",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.place(relx=0.5, rely=0.73, anchor='center')
#(x=387,y=660)
#BUTTON WHICH CALLS FUNCTION WHICH GRABS NAME (CURRENTLY LOCATED IN WEIRD LOCATION)
namebutton = ttk.Button(self, text="Submit", command=lambda: controller.getname(StartPage))
namebutton.place(relx=0.5, y=60)
#self.date_label = tk.Label(self, text="") #label to display date
#self.date_label.pack(pady=20)
self.configure(bg='#f2fcff')
class HomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#label = tk.Label(self, text="Home", font = LARGE_FONT)
#label.pack(pady=10,padx=10)
#width of x
frame = tk.Frame(self, bg="#c0e3ed", height=110)
frame.pack(fill="x")
#LABEL WHICH DISPLAYS NAME FROM TEXT ENTRY
#tk.Label(self, text ="Name's Planner", fg="white", bg="#c0e3ed", font = LARGE_FONT).place(relx=0.5, y=50, anchor='center')
self.name_label = tk.Label(self, text="", font=("Arial Bold", 50))
self.name_label.place(x=0, y=0)
self.date_label = tk.Label(self, text="", bg="#c0e3ed")
# keep placing the label at top right corner
self.date_label.place(relx=1, y=0, anchor='ne')
#pack(side='top', anchor='ne')
#height of Y
tk.Label(self, text="", fg="white", bg="#99cbd8").pack(side="left", fill="y", ipadx=73)
button1 = tk.Button(self, height=2, width=10, bg="#e0f6fc", text="Home", command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.place(x=35, y=145)
button1 = tk.Button(self, height=2, width=10, bg="#e0f6fc", text="To Do", command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
button1.place(x=35, y=225)
#(relx=0.2, rely=0.4, anchor='center')
button1 = tk.Button(self, height=2, width=10, bg="#e0f6fc", text="Timetable", command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
button1.place(x=35, y=305)
photo2 = tk.PhotoImage(file=IMAGE_PATH) #photo1 is a variable
#label (window, image=photo1, bg="black") .grid(row=0, column=0, sticky=E)
panel = tk.Label(self, image = photo2)
panel.image = photo2
panel.place(x=35, y=400)
self.configure(bg='#f7f6f6')
class ToDoPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="To Do", font = LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Home",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="Timetable",
command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="To Do",
command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
button1.pack()
class TimetablePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Timetable", font = LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Home",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="Timetable",
command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="To Do",
command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
button1.pack()
app = StudyFriendO()
app.mainloop()

Sticking a label in top right corner of Tkinter application

I am working on creating a Tkinter planner application that has multiple pages. On the start page, you can enter a date in the calendar and press the 'enter' button to enter the application.
The home page then displays the date in the top right corner. Is there any way for the date label to overlap with the blue banner so that there isn't a grey line at the top? The date also needs to be placed so that it stays in the same spot even when the window is resized, hence why I have used the pack function. However, I believe that the pack function cannot overlap with other labels.
import calendar
import tkinter as tk
from tkinter import ttk
from tkcalendar import *
from datetime import datetime
#calendar and display in label
#window = Tk()
#window.title("StudyFriendO")
LARGE_FONT= ("Verdana", 24)
class StudyFriendO(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("StudyFriendO") #naming window title
self.geometry('850x830') #size of window
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, HomePage, ToDoPage, TimetablePage): #list of multiple frames of program
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def set_date(self, cont):
#frame: StartPage = self.frames[cont]
#frame.date_label.config(text="Today's date is " + datetime.today().strftime("%B %d, %Y")) #function to get date from
frame2: HomePage = self.frames[HomePage]
frame2.date_label.config(text= datetime.today().strftime("%B %d, %Y")) #function to get date from
class StartPage(tk.Frame): #creating start page
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="StudyFriendO", font = LARGE_FONT, bg="#f2fcff")
label.pack(fill="x")
#place(x=315,y=100)
photo1 = tk.PhotoImage(file=image file) #photo1 is a variable
#label (window, image=photo1, bg="black") .grid(row=0, column=0, sticky=E)
panel = tk.Label(self, image = photo1)
panel.image = photo1
panel.pack(fill="x")
#place(x=270,y=150)
label2 = tk.Label(self, text="Enter today's date and your first name below:", bg="#f2fcff")
label2.pack(fill="x", ipady=20)
#place(x=305, y=400)
cal = Calendar(self, background="#e0f6fc", disabledbackground="white", bordercolor="light blue", headersbackground="light blue", normalbackground="#e0f6fc", foreground="black", normalforeground='black', headersforeground='white', selectmode="day", year=2021, month=8, day=9)
cal.place(relx=0.5, rely=0.5, anchor='center')
#(x=300,y=430) #calendar
nameentry = tk.Entry(self, width=20, bg="white") #text input for users name
nameentry.place(relx=0.5, rely=0.68, anchor='center')
#(x=365, y=635)
caldate = ttk.Button(self, text="Submit",
command=lambda: controller.set_date(StartPage)) #button to get current date
caldate.place(relx=0.5, rely=0.63, anchor='center')
button1 = ttk.Button(self, text="Enter",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.place(relx=0.5, rely=0.73, anchor='center')
#(x=387,y=660)
#self.date_label = tk.Label(self, text="") #label to display date
#self.date_label.pack(pady=20)
self.configure(bg='#f2fcff')
class HomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#label = tk.Label(self, text="Home", font = LARGE_FONT)
#label.pack(pady=10,padx=10)
self.date_label = tk.Label(self, text="") #label to display date
self.date_label.pack(side='top', anchor='ne')
#width of x
tk.Label(self, text ="Name's Planner", fg="white", bg="#c0e3ed", font = LARGE_FONT).pack(fill="x", ipady=30)
#height of Y
tk.Label(self, text="", fg="white", bg="#99cbd8").pack(side="left", fill="y", ipadx=73)
button1 = tk.Button(self, height=2, width=10, bg="#e0f6fc", text="Home", command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.place(x=35, y=145)
button1 = tk.Button(self, height=2, width=10, bg="#e0f6fc", text="To Do", command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
button1.place(x=35, y=225)
#(relx=0.2, rely=0.4, anchor='center')
button1 = tk.Button(self, height=2, width=10, bg="#e0f6fc", text="Timetable", command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
button1.place(x=35, y=305)
photo2 = tk.PhotoImage(image file) #photo1 is a variable
#label (window, image=photo1, bg="black") .grid(row=0, column=0, sticky=E)
panel = tk.Label(self, image = photo2)
panel.image = photo2
panel.place(x=35, y=400)
self.configure(bg='#f7f6f6')
class ToDoPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="To Do", font = LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Home",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="Timetable",
command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="To Do",
command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
button1.pack()
class TimetablePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Timetable", font = LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Home",
command=lambda: controller.show_frame(HomePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="Timetable",
command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
button1.pack()
button1 = ttk.Button(self, text="To Do",
command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
button1.pack()
app = StudyFriendO()
app.mainloop()
You can use .place() instead of .pack(), but better create and place it after the banner:
...
tk.Label(self, text ="Name's Planner", fg="white", bg="#c0e3ed", font = LARGE_FONT).pack(fill="x", ipady=30)
#label to display date with same background color of the above banner
self.date_label = tk.Label(self, text="", bg="#c0e3ed")
# keep placing the label at top right corner
self.date_label.place(relx=1, y=0, anchor='ne')
...

Python 3 tkinter on Raspberry Pi

Been fighting with this for too long wondered is someone might point me in the right direction. I am trying to establish an App that when triggered by a pir sensor attached to the rasberry pi opens a Frame with 7 buttons on it. I want the buttons when pressed to open separate frames in which i want to attach an image. I want the frame to disappear after a given time.
Now i can do this on the start page no problem creating a frame and canvas and loading an image in and then using after with destroy to get rid of it after 7 seconds.
However as soon as i try to bring other frames into the equation i cant seem to get canvases or images to work in the secondary frames. I changed my current approach which i set into clases (care of a tutorial on line) but dead end there as well.
Im not sure but i have the feeling that with classes i have now set up a master slave relationship with the frames meaning i cant customise them individually? I have attached the code im currently playing with that relates to the frames. If someone can advice be much appreciated.
import tkinter as tk
from tkinter import *
LARGE_FONT= ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(100, weight=1)
container.grid_columnconfigure(100, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo, PageThree, PageFour, PageFive, PageSix, PageSeven):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="SMART BIN Tech", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = tk.Button(self, text="NO DRINKS or FOOD WASTE",
command=lambda: controller.show_frame(PageOne))
button.pack()
button2 = tk.Button(self, text="NO PAPER WASTE",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
button3 = tk.Button(self, text="NO TEA BAGS",
command=lambda: controller.show_frame(PageThree))
button3.pack()
button4 = tk.Button(self, text="NO CANS or PLASTICS",
command=lambda: controller.show_frame(PageFour))
button4.pack()
button5 = tk.Button(self, text="LIST of RECYCLABLES",
command=lambda: controller.show_frame(PageFive))
button5.pack()
button6 = tk.Button(self, text="Vending Area Performance",
command=lambda: controller.show_frame(PageSix))
button6.pack()
button7 = tk.Button(self, text="UBS UK RATIO",
command=lambda: controller.show_frame(PageSeven))
button7.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="NO DRINKS or FOOD WASTE", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="NO PAPER WASTE", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
class PageThree(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="NO TEA BAGS", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
class PageFour(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="NO CANS or PLASTICS", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
class PageFive(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="RECYCLABLES", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
class PageSix(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Vending Area Performance", font=LARGE_FONT)
label.pack(pady=10,padx=10)
w = tk.Canvas(self,width=400,height=500)
w.pack
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
class PageSeven(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="UBS UK RATIO", font=LARGE_FONT)
label.pack(pady=10,padx=10)
canvas = tk.Canvas(self,width=100, height=100)
canvas.pack
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.grid(row=2, column=5)
button1.pack()
app = SeaofBTCapp()
app.mainloop()

Tkinter navigation with frames

Hello,
I'm really stuck with this python code. Maybe you can help me?
I want to navigate pages with the buttons. I have three different frames. Then I press on one button, one frame goes on top of others, but do not hide other frames.
If my question for you isn't clear just try my code, and you will see what I mean.
This code from but little modified.
import tkinter as tk
from tkinter import ttk
from tkinter import StringVar
LARGE_FONT= ("Verdana", 12)
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Title")
container = tk.Frame(self, width=768, height=1000)
container.pack(side="top", fill='both' , expand = 1)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
#frame.pack()
frame.grid(row=0, column=0)
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = ttk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = ttk.Button(self, text="Visit Page 1",
command=lambda: controller.show_frame(PageOne))
button.pack()
button2 = ttk.Button(self, text="Visit Page 2",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text="Page One!!!", font=LARGE_FONT)
label.pack(pady=0,padx=100)
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = ttk.Button(self, text="Page Two",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text="Page Two!!!", font=LARGE_FONT)
label.pack(pady=0,padx=0)
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = ttk.Button(self, text="Page One",
command=lambda: controller.show_frame(PageOne))
button2.pack()
phone = StringVar()
home = ttk.Radiobutton(self, text='Home', variable=phone, value='home')
office = ttk.Radiobutton(self, text='Office', variable=phone, value='office')
cell = ttk.Radiobutton(self, text='Mobile', variable=phone, value='cell')
home.pack()
office.pack()
cell.pack()
app = Application()
app.mainloop()
Thank you very much:)
You need to tell all the frames to use up all available space, so that they cover any frames below them. You do that by using the sticky argument:
frame.grid(row=0, column=0, sticky='nsew')
That tells the frame to stick to the north, south, east and west sides; in other words fill all the space in both directions.

Python Tkinter multiple frames with background image

I'm new to Tkinter and I'm struggling with what should be quite a simple bit of python code for my RasPi. My objective is to have a series of frames that all have the same fixed size and background image but have different buttons, labels (on top of the image) and functions. If I include a canvas and an image in the container then I can get my frames (well the first frame at least) to show the background image but I can't add anything to the canvas from the page specific class because I can't reference the canvas anymore?? If I leave the frames blank in the container then add the canvas/image in the page specific class then I can't get the canvas/image to work. This is the code that I'm trying to modify to suit my needs...
import Tkinter as tk
TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, c):
frame = self.frames[c]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame(PageOne))
button2 = tk.Button(self, text="Go to Page Two",
command=lambda: controller.show_frame(PageTwo))
button1.pack()
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame(StartPage))
button.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="This is page 2", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame(StartPage))
button.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()controller.show_frame(PageTwo))
I got it working...
import Tkinter as tk
import ttk
TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
self.attributes("-fullscreen", True)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, c):
frame = self.frames[c]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
logo = tk.PhotoImage(file="/home/pi/Saffi.gif")
BGlabel = tk.Label(self,image=logo)
BGlabel.image = logo
BGlabel.place(x=0,y=0,width=592,height=450)
label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
label.place(x=0,y=0,width=592,height=44)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame(PageOne))
button2 = tk.Button(self, text="Go to Page two",
command=lambda: controller.show_frame(PageTwo))
button3 = tk.Button(self, text="Exit",
command=self.quit)
button1.place(x=100,y=406,width=200,height=44)
button2.place(x=300,y=406,width=200,height=44)
button3.place(x=500,y=406,width=80,height=44)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
logo = tk.PhotoImage(file="/home/pi/Saffi.gif")
BGlabel = tk.Label(self,image=logo)
BGlabel.image = logo
BGlabel.place(x=0,y=0,width=592,height=450)
label = tk.Label(self, text="This is page one", font=TITLE_FONT)
label.place(x=0,y=0,width=592,height=44)
button1 = tk.Button(self, text="Go to Start Page",
command=lambda: controller.show_frame(StartPage))
#button2 = tk.Button(self, text="Go to Page two",
# command=lambda: controller.show_frame(PageTwo))
button3 = tk.Button(self, text="Exit",
command=self.quit)
button1.place(x=100,y=406,width=200,height=44)
button3.place(x=300,y=406,width=200,height=44)
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
logo = tk.PhotoImage(file="/home/pi/Saffi.gif")
BGlabel = tk.Label(self,image=logo)
BGlabel.image = logo
BGlabel.place(x=0,y=0,width=592,height=450)
label = tk.Label(self, text="This is page two", font=TITLE_FONT)
label.place(x=0,y=0,width=592,height=44)
button1 = tk.Button(self, text="Go to Start Page",
command=lambda: controller.show_frame(StartPage))
#button2 = tk.Button(self, text="Go to Page two",
# command=lambda: controller.show_frame(PageTwo))
button3 = tk.Button(self, text="Exit",
command=self.quit)
button1.place(x=100,y=406,width=200,height=44)
button3.place(x=300,y=406,width=200,height=44)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()

Categories