0

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

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

1 回答 1

1

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

在此处输入图像描述 在此处输入图像描述

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()
于 2013-11-12T05:49:10.517 回答