2011-04-21 139 views
1

我有一個程序,是假設創建一個新的Toplevel窗口小部件,然後添加一個框架。頂級和框架類都已經單獨創建,所以當它們被自動調用我的窗口填充時。但是,單擊該按鈕時,頂層窗口彈出,但框架將自身添加到主窗口,而不是新的頂級Tkinter框架添加到主窗口

主窗口代碼

類主窗口(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類

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

這是打開ne的功能W的是假設窗口與幀填補自身

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) 

,這裏的類是框架類的初始化

class EditFamilyWindow(Tkinter.Frame): 

def __init__(self,student): 

    Tkinter.Frame.__init__(self) 

回答

1

底線是,您的框架必須創建爲特定頂層的後代。您不能將幀從一個頂層重新映射到另一個頂層。因此,爲了讓代碼正常工作,您必須先創建頂層,然後創建您的框架並告訴它要創建哪個頂層。

+0

但是,我在editstudentwindow類中創建了頂層。然後從那裏我將幀添加到頂層。或者至少我以爲我做了 – Brandon 2011-04-25 15:39:48

+0

@Brandon:你說這個框架出現在錯誤的頂層。這不會意外發生,你的代碼正在把它放在那裏。這應該是微不足道的跟蹤 - 在創建框架的地方放置一個打印語句,並查看它的頂層窗口是什麼。 – 2011-04-25 17:44:58

+0

editstudentwindow類創建頂層作爲自身,然後創建框架,然後將其放置在自身內部,但由於某些原因,框架出現在主窗口中而不是頂層 – Brandon 2011-04-25 18:08:16

相關問題