Tkinter Frame 添加到主窗口

1 投票
1 回答
2399 浏览
提问于 2025-04-16 16:11

我有一个程序,它应该创建一个新的顶层窗口(Toplevel widget),然后在里面添加一个框架(frame)。这两个类已经分别创建好了,所以当它们被调用时,我的窗口会自动填充。但是当我点击按钮时,顶层窗口弹出来了,但框架却加到了主窗口里,而不是新的顶层窗口。

主窗口的代码

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)

在这里,它把学生查找窗口类添加到主窗口。这个类负责打开新的顶层窗口。

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


    self.mainloop()

这是学生查找窗口类

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

这是打开新窗口的函数,它应该用一个框架来填充新窗口。

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

这里是打开的类

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)

这里是框架类的init部分

class EditFamilyWindow(Tkinter.Frame):

def __init__(self,student):

    Tkinter.Frame.__init__(self) 

1 个回答

1

简单来说,你的框架(frame)必须是在一个特定的顶层窗口(toplevel)里面创建的。你不能把一个框架从一个顶层窗口移动到另一个顶层窗口。所以,为了让你的代码正常工作,你必须先创建顶层窗口,然后再创建框架,并告诉它要在哪个顶层窗口里面创建。

撰写回答