我有一个程序,假设创建一个新的Toplevel小部件,然后添加一个框架。顶层和框架类都已单独创建,因此当它们被调用时,我的窗口会自动填充。但是当单击按钮时,会弹出Toplevel窗口,但框架会将其自身添加到主窗口而不是新的Toplevel
主窗口代码
类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()
这是StudentFinderWindow类
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)
答案 0 :(得分:1)
底线是,您的框架必须创建为特定顶层的后代。您无法将帧从一个顶层重新定位到另一个顶层。因此,为了使您的代码工作,您必须首先创建顶层,然后创建您的框架并告诉它在哪个顶层创建。