我有一个程序,应该创建一个新的顶级小部件,然后向其添加一个框架。顶层类和框架类都已单独创建,因此当调用它们时,我的窗口会自动填充。但是,当单击按钮时,会弹出“顶层”窗口,但框架将其自身添加到主窗口,而不是新的“顶层”
主窗口代码
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()
这是 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])
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)
这是打开的类
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)
最佳答案
底线是,您的框架必须创建为特定顶层的后代。您无法将框架从一个顶层重新设置为另一个顶层。因此,为了让您的代码正常工作,您必须首先创建顶层,然后创建框架并告诉它要在哪个顶层中创建。
关于python - Tkinter 框架将自身添加到主窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5747152/