python - Tkinter example for multiple frames, grid error -
i'm using below example, found on portal. , try add listbox. know can use pack, instead grid, pack quite messy, , grid can more specific.
import tkinter tk title_font = ("helvetica", 18, "bold") class sampleapp(tk.tk): def __init__(self, *args, **kwargs): tk.tk.__init__(self, *args, **kwargs) # container we'll stack bunch of frames # on top of each other, 1 want visible # raised above others container = tk.frame(self) container.pack(side="top", fill="both", expand=true) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} f in (startpage, pageone, pagetwo): frame = f(container, self) self.frames[f] = frame # put of pages in same location; # 1 on top of stacking order # 1 visible. frame.grid(row=0, column=0, sticky="nsew") self.show_frame(startpage) def show_frame(self, c): '''show frame given class''' frame = self.frames[c] frame.tkraise() class startpage(tk.frame): def __init__(self, parent, controller): tk.frame.__init__(self, parent) label = tk.label(self, text="this start page", font=title_font) label.pack(side="top", fill="x", pady=10) button1 = tk.button(self, text="go page one", command=lambda: controller.show_frame(pageone)) button2 = tk.button(self, text="go page two", command=lambda: controller.show_frame(pagetwo)) button1.pack() button2.pack() class pageone(tk.frame): def __init__(self, parent, controller): tk.frame.__init__(self, parent) label = tk.label(self, text="this page 1", font=title_font) label.pack(side="top", fill="x", pady=10) button = tk.button(self, text="go start page", command=lambda: controller.show_frame(startpage)) button.pack() class pagetwo(tk.frame): def __init__(self, parent, controller): tk.frame.__init__(self, parent) label = tk.label(self, text="this page 2", font=title_font) label.pack(side="top", fill="x", pady=10) button = tk.button(self, text="go start page", command=lambda: controller.show_frame(startpage)) button.pack() if __name__ == "__main__": app = sampleapp() app.mainloop()
and example want add pagetwo class. don't want use .pack.
lbox = tk.listbox(self, height=10) lbox.grid(column=0, row=0, padx=5) s = ttk.scrollbar(self, orient="vertical", command=lbox.yview) s.gird(column=1, row=1, sticky=("s","e"))
and getting error
c:\python34\python.exe c:/users/mariusz/onedrive/projekty/python/himan/main.py traceback (most recent call last): file "c:/users/mariusz/onedrive/projekty/python/himan/main.py", line 198, in <module> app = himan() file "c:/users/mariusz/onedrive/projekty/python/himan/main.py", line 44, in __init__ frame = f(container, self) file "c:/users/mariusz/onedrive/projekty/python/himan/main.py", line 132, in __init__ lbox.grid(column=0, row=0, padx=5) file "c:\python34\lib\tkinter\__init__.py", line 2057, in grid_configure + self._options(cnf, kw)) _tkinter.tclerror: cannot use geometry manager grid inside .52613520.52708464 has slaves managed pack process finished exit code 1
you cannot mix pack
, grid
within same container. possible mix them in different containers, i'd highly recommend staying only 1 of these geometry manager.
see question more help.
Comments
Post a Comment