I am creating a Tkinter application which has multiple frames. When you click the add button it takes you to another frame. You then press the add assessment button which has the command display
. In this command, it packs a progress bar and button, which has another command step
, onto the HomePage
. I have defined step
above the display
command, however when I run the program an error occurs saying step
is not defined. I believe this is due to the multiple frames but I am not sure how to fix it.
import calendar
import tkinter as tk
from tkinter import ttk
from tkcalendar import *
from datetime import datetime
from datetime import date
from time import strftime
from datetime import timedelta, datetime, date
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=''),
'assessment': tk.StringVar(value=''),
'due_date': tk.StringVar(value='')
}
self.frames = {}
for F in (HomePage, AddPage): #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(HomePage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def step(frame2):
my_progress['value']+= 5
def display(self, cont):
frame1: HomePage = self.frames[HomePage]
frame1.my_progress = ttk.Progressbar(frame1, orient='horizontal', length=300, mode='determinate')
frame1.my_progress.place(x=460, y=460, anchor='center')
frame1.my_button = tk.Button(frame1, text="Progress", command=step)
frame1.my_button.pack(pady=20)
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)
add_button = ttk.Button(self, text="Add", command=lambda: controller.show_frame(AddPage))
add_button.place(relx=0.8, rely=1, anchor='se')
class AddPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
add_button = ttk.Button(self, text="Add Assesment", command=lambda: controller.display(HomePage))
add_button.place(relx=0.5, y=560, anchor='center')
app = StudyFriendO()
app.mainloop()
self.step
. It's a member of the class. – Tim Roberts