Tkinter在当前窗口中添加/删除“windows”

时间:2013-11-11 21:17:53

标签: python tkinter

基本上下面是我现在所拥有的。问题是这些按钮中的每一个都打开一个新窗口,我希望在右上角我有当前的功能,如显示添加课程,然后当我点击“添加时间”时,添加课程将被删除并添加时间显示。有没有办法可以为每个“编辑”字典操作执行此操作,并且还在此窗口的右下角打印了“最新”表格?感谢。

   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)

1 个答案:

答案 0 :(得分:1)

工作示例 - 我希望这是你所期望的。

enter image description here enter image description here

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()