1
votes

I have a program that is suppose to create a new Toplevel widget and then add a frame to it. Both the toplevel and frame classes are already created separately so when they are called my window fills automatically. But when the button is clicked the Toplevel window pops up but the frame adds itself to the main window and not the new Toplevel

Main window code

class MainWindow(Tkinter.Tk):

def __init__(self,version):
    Tkinter.Tk.__init__(self)  # creates the main window
    self.title('Awana Control ' + version) # names the main window

    topframe = Tkinter.Frame(self)
    topframe.grid(column=0,row=0)

    openNewFamilyWindowButton = Tkinter.Button(topframe, text='Add a new family',command=partial(newWindow,'NewFamilyWindow'))
    openNewFamilyWindowButton.grid(row=0, column=0)

    #openEditStudentWindowButton = Tkinter.Button(topframe, text='edit current repository', command=partial(newWindow,'studentFinderWindow'))
    #openEditStudentWindowButton.grid(row=0, column=1)

    openTotalsWindowButton = Tkinter.Button(topframe, text='Total Shipping Orders', command=partial(newWindow,'totalsWindow'))
    openTotalsWindowButton.grid(row=0, column=1)

    openTotalsWindowButton = Tkinter.Button(topframe, text='Lists By Grade', command=partial(newWindow,'gradeList'))
    openTotalsWindowButton.grid(row=0, column=2)

    #bottomframe = Tkinter.Frame(self)
    #bottomframe.grid(column=0,row=1)

right here is when it adds the student finder window class to the main window. This class is responsible for opening the new toplevel windows

    StudentFinderWindow().grid(column=0,row=1)


    self.mainloop()

here is the StudentFinderWindow class

class StudentFinderWindow(Tkinter.Frame):

def __init__(self):
    Tkinter.Frame.__init__(self) # Create Window

    ##### window attributes
    #self.title('Edit Families') #sets window title

    ##### puts stuff into the window

    # text
    editStudentInfoLabel = Tkinter.Label(self,text='Select the family from the list below or search for one in the search box provided')
    editStudentInfoLabel.grid(row=0, column=0)

    # entry box
    self.searchRepositoryEntry = Tkinter.Entry(self)
    self.searchRepositoryEntry.grid(row=1, column=0)

    # list box
    self.searchResults = Tkinter.Listbox(self,selectmode='SINGLE')
    self.searchResults.grid(row=2, column=0)

    # create a vertical scrollbar to the right of the listbox
    #yscroll = Tkinter.Scrollbar(self, command=self.searchResults.yview, orient=Tkinter.VERTICAL)
    #yscroll.grid(row=0, column=1, sticky=Tkinter.N+Tkinter.S)
    #self.searchResults.configure(yscrollcommand=yscroll.set)
    # search results initial updater
    self.getStudentList()
    for student in self.studentList:
        self.searchResults.insert(Tkinter.END, student)

    ##### event handler 
    self.searchResults.bind('<Double-Button-1>',self.editStudentWindowInit )
    self.searchRepositoryEntry.bind('<KeyRelease>', self.updateSearch)

def updateSearch(self, event):
    parameters = self.searchRepositoryEntry.get()
    parameters = parameters.lower()
    length = len(parameters)
    self.searchResults.delete(0, Tkinter.END)
    for i in self.studentList:
        if i[0:length].lower() == parameters:
            self.searchResults.insert(Tkinter.END, i)


def getStudentList(self):
    global fileDirectory # gets the directory that all the files are in
    fileList = os.listdir(fileDirectory) # makes a list of files from the directory
    self.studentList = [] #  makes a new list
    for file in fileList: # for loop that adds each item from the file list to the student list
        if file[-3:] == 'txt':
            self.studentList.append(file[:-4])

This is the function that opens the new window which is suppose to fill itself with a frame

def editStudentWindowInit(self,mevent):
    index = self.searchResults.curselection()
    student = self.searchResults.get(index)
    editStudentWindow = EditStudentWindow(student)

Here is the class that is opened

class EditStudentWindow(Tkinter.Toplevel):
    def __init__(self,student):

        Tkinter.Toplevel.__init__(self)
        self.title('Edit Family Info')

        Tkinter.Button(self,text='Edit Info',command=partial(self.fillInfo,student)).grid(column=0,row=0)
        Tkinter.Button(self,text='Edit Orders',command=partial(self.fillOrder,student)).grid(column=1,row=0)

        self.fillInfo(student)

    def fillInfo(self,student):

        try:
            self.order.grid_forget()
        except:
            pass
        self.info = EditFamilyWindow(student)
        self.info.grid(column=0,row=1)

    def fillOrder(self,student):
        try:
            self.info.grid_forget()
        except:
            pass
        self.order = EditShippingWindow(student)
        self.order.grid(column=0,row=1)

and here is the init of the frame class

class EditFamilyWindow(Tkinter.Frame):

def __init__(self,student):

    Tkinter.Frame.__init__(self) 
1

1 Answers

1
votes

The bottom line is, your frame must be created as a descendant of a specific toplevel. You can't reparent a frame from one toplevel to another. So, for your code to work you must create the toplevel first, and then create your frame and tell it which toplevel to be created in.