2016-11-13 47 views
1

所以我正在寫一個小的應用程序進行管理,我得到這個錯誤:裏面不能使用Geometry Manager Grid。已經有奴隸的包

cannot use geometry manager grid inside . which already has slaves managed by pack

import tkinter as tk 
from tkinter import ttk 



LARGE_FONT = ("Times New Roman", 16) 
NORMAL_FONT = ("Times New Roman", 12) 



def popup(title, string): 
    popup = tk.Tk() 
    popup.geometry('300x100') 
    popup.wm_title(title) 

    label = ttk.Label(popup, text=string) 
    label.pack(pady=10) 

    b1 = ttk.Button(popup, text='Okay', command=lambda:popup.destroy()) 
    b1.pack(pady=10) 

class FECapp(tk.Tk): 

    def __init__(self, *args, **kwargs): 

     tk.Tk.__init__(self, *args, **kwargs) 
     container = tk.Frame(self) 
     container.pack(side='top', fill='both', expand=True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     tk.Tk.iconbitmap(self, default='icon.ico') 
     tk.Tk.wm_title(self, 'Family Entertainment Center Management System') 

     menu = tk.Menu(container) 

     file = tk.Menu(menu) 
     file.add_command(label='Login', command=lambda:popup('Not supported', 'This function is not supported yet.')) 
     file.add_separator() 
     file.add_command(label='Save Changes', command=lambda:popup('Not supported', 'This function is not supported yet.')) 
     file.add_separator() 
     file.add_command(label='Exit', command=lambda:exit()) 
     menu.add_cascade(label='File', menu=file) 

     navigate = tk.Menu(menu) 
     navigate.add_command(label='Home', command=lambda:self.show_frame(StartPage)) 
     navigate.add_command(label='Employee List', command=lambda:self.show_frame(Page1)) 
     menu.add_cascade(label='Navigate', menu=navigate) 

     tk.Tk.config(self, menu=menu) 

     self.frames = {} 
     for F in (StartPage, Page1): 
      frame = F(container, self) 
      self.frames[F]=frame 
      frame.grid(row=0, column=0, sticky='nsew') 

     self.show_frame(StartPage) 

    def show_frame(self, cont): 

     frame = self.frames[cont] 
     frame.tkraise() 

class StartPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 

     self.grid_rowconfigure(0, weight=1) 
     self.grid_columnconfigure(0, weight=1) 

     label1 = ttk.Label(text="Oops!", font=LARGE_FONT) 
     #label1.pack(side='top', fill='y', expand=True) 
     label1.grid(row=0, column=0, sticky='ns') 

     label1 = ttk.Label(text="There doesn't seem to be anything here right now.", font=NORMAL_FONT) 
     #label1.pack(side='top',fill='y', expand=True) 
     label1.grid(row=1, column=0, sticky='ns') 

class Page1(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 

     listbox = MultiListbox(self,[('Row 1', 1, 'Item1'),('Row 2', 2, 'Item2'),('Row 3', 3, 'Item3'),('Row 4', 4, 'Item4')],['Row', 'Row (int)', 'Christmas List']) 
     listbox.pack() 


#Since tkinter has no multicolumn listbox, I suppose I'll make my own... 
class MultiListbox(ttk.Frame): 
    def __init__(self, master, data, headings): 
     ttk.Frame.__init__(self, master) 
     self.data=data 

     self.tree = ttk.Treeview(self, columns=headings, show='headings') 
     for head in headings: 
      self.tree.heading(head, text=head) 
     self.tree.pack() 



app = FECapp() 
app.geometry('1280x720') 
app.mainloop() 

它在起始頁.__的init __()得到挑剔的,我開始添加標籤使用網格,但我無法看到我在StartPage或甚至在StartPage中使用的任何地方。我錯過了什麼嗎?

+0

可能重複的[不能使用幾何管理器包裏面](https://stackoverflow.com/questions/23584325/cannot-use-geometry-manager-pack-inside) –

回答

2

頁面中的小部件有錯誤的父項。您沒有指定父級,因此它默認爲根窗口,並且您在根窗口中使用pack

+0

謝謝,不能相信我錯過了,因爲太長。現在效果很好。 – Vedvart1