Reputation: 141
Basically below is what I have right now. The problem is that each of these buttons opens a new window and I would prefer that in the top right I have the current function such as add course shown and then when I click the "add time" the add course would be removed and the add time be shown. Is there a way I could do this for each of the "editing" dict operations and also have an "up-to-date" table printed in the bottom right of this window? Thanks.
class menu():
def __init__(self, master):
self.master = master
self.table = {}
self.createButtons()
# creates buttons with alignment and their functions
def createButtons(self):
load = Button(self.master, text = "Load Table",
command = self.loadTable)
load.grid(row =0, column =0, pady = 30)
course = Button(self.master, text = "Add Course",
command = self.addCourse)
course.grid(row =1, column =0, pady = 30)
time = Button(self.master, text = "Add Time",
command = self.addTime)
time.grid(row =2, column =0, pady = 30)
reset = Button(self.master, text = "Reset Time",
command = self.resetTime)
reset.grid(row =3, column =0, pady = 30)
comment = Button(self.master, text = "Change Comment",
command = self.changeComment)
comment.grid(row =4, column =0, pady = 30)
view = Button(self.master, text = "View Table",
command = self.viewTable)
view.grid(row =5, column =0, pady = 30)
def addCourse(self):
#creates addCourse window and text boxes for input
toplevel = Toplevel()
toplevel.title('Add Course')
Label (toplevel, text='Enter the course name').grid()
courseBox = Entry(toplevel, width=10)
courseBox.grid()
label =Label (toplevel, text='Enter the hours per week spent on course')
label.grid(padx=10,pady=10)
weekHoursBox = Entry(toplevel, width=10)
weekHoursBox.grid()
#function to accept input from boxes into dict
def callback():
course = courseBox.get()
weekHours = weekHoursBox.get()
string = "0 "+ str(weekHours) + " "
self.table[course] = string
enterB = Button(toplevel, text='Enter Information', command = callback)
enterB.grid(pady=10)
Upvotes: 0
Views: 280
Reputation: 142681
Working example - I hope it is what you expected.
from Tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.master = master
self.table = {}
self.rightFrame = None # for first "grid_forget()"
self.createButtons()
#-----------------------------
def run(self):
self.master.mainloop()
#-----------------------------
# creates buttons with alignment and their functions
def createButtons(self):
self.menuFrame = Frame(self.master)
self.menuFrame.grid(row=0, column=0)
load = Button(self.menuFrame, text="Load Table", command=self.loadTable)
load.grid(row=0, column=0, pady=30)
course = Button(self.menuFrame, text="Add Course", command=self.addCourse)
course.grid(row=1, column=0, pady=30)
time = Button(self.menuFrame, text="Add Time", command=self.addTime)
time.grid(row=2, column=0, pady=30)
reset = Button(self.menuFrame, text="Reset Time", command=self.resetTime)
reset.grid(row=3, column=0, pady=30)
comment = Button(self.menuFrame, text="Change Comment", command=self.changeComment)
comment.grid(row=4, column=0, pady=30)
view = Button(self.menuFrame, text="View Table", command=self.viewTable)
view.grid(row=5, column=0, pady=30)
#-----------------------------
def loadTable(self):
# remove previous content
if self.rightFrame:
self.rightFrame.grid_forget()
# create new content
self.rightFrame = Frame(self.master)
self.rightFrame.grid(row=0, column=1, sticky=N)
title = Label(self.rightFrame, text='load Table')
title.grid(row=0, column=0)
#-----------------------------
def addCourse(self):
# remove previous content
if self.rightFrame:
self.rightFrame.grid_forget()
# create new content
self.rightFrame = Frame(self.master)
self.rightFrame.grid(row=0, column=1, sticky=N)
#creates addCourse window and text boxes for input
title = Label(self.rightFrame, text='Enter the course name')
title.grid(row=0, column=0)
courseBox = Entry(self.rightFrame, width=10)
courseBox.grid(row=1, column=0)
label = Label (self.rightFrame, text='Enter the hours per week spent on course')
label.grid(row=2, column=0)
weekHoursBox = Entry(self.rightFrame, width=10)
weekHoursBox.grid(row=3, column=0)
#function to accept input from boxes into dict
def callback():
course = courseBox.get()
weekHours = weekHoursBox.get()
string = "0 "+ str(weekHours) + " "
self.table[course] = string
enterB = Button(self.rightFrame, text='Enter Information', command=callback)
enterB.grid(row=4, column=0)
#-----------------------------
def addTime(self):
# remove previous content
if self.rightFrame:
self.rightFrame.grid_forget()
# create new content
self.rightFrame = Frame(self.master)
self.rightFrame.grid(row=0, column=1, sticky=N)
title = Label(self.rightFrame, text='Add Time')
title.grid(row=0, column=0)
#-----------------------------
def resetTime(self):
# remove previous content
if self.rightFrame:
self.rightFrame.grid_forget()
# create new content
self.rightFrame = Frame(self.master)
self.rightFrame.grid(row=0, column=1, sticky=N)
title = Label(self.rightFrame, text='Reset Time')
title.grid(row=0, column=0)
#-----------------------------
def changeComment(self):
# remove previous content
if self.rightFrame:
self.rightFrame.grid_forget()
# create new content
self.rightFrame = Frame(self.master)
self.rightFrame.grid(row=0, column=1, sticky=N)
title = Label(self.rightFrame, text='Change Comment')
title.grid(row=0, column=0)
#-----------------------------
def viewTable(self):
# remove previous content
if self.rightFrame:
self.rightFrame.grid_forget()
# create new content
self.rightFrame = Frame(self.master)
self.rightFrame.grid(row=0, column=1, sticky=N)
title = Label(self.rightFrame, text='View Table')
title.grid(row=0, column=0)
#----------------------------------------------------------------------
Application(Tk()).run()
Upvotes: 1