python - tkinter - Weighted Canvas not Filling Empty Space -
canvas takes whole screen can seen green. canvasframe has 2 rows, second of scrolledtext widgets on. second row has been weighted not filling screeen green area should yellow. how the second row fill empty space , scrolled text widgets expand vertically.
p.s. innerframe looks unecessary in example it's there because i've removed other widgets in frame simplicity's sake.
code:
import tkinter tk tkinter import ttk import tkinter.scrolledtext tks class program(tk.tk): def __init__(self, *args, **kwargs): tk.tk.__init__(self, *args, **kwargs) tk.tk.iconbitmap(self, default = "") tk.tk.wm_title(self, "") 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 (page, other): frame = f(container, self) self.frames[f] = frame frame.grid(row = 0, column = 0, sticky = "nsew") self.show_frame(page) def show_frame(self,cont): frame = self.frames[cont] frame.tkraise() class page(tk.frame): def __init__(self, parent, controller): tk.frame.__init__(self, parent) innerframe = tk.frame(self, bg="red") innerframe.place(relx=.5, rely=.5, anchor="c", relwidth=1.0, relheight=1.0) innerframe.grid_rowconfigure(1, weight=1) innerframe.grid_columnconfigure(0, weight=1) frameone = tk.frame(innerframe, bg="green") frameone.grid(row=1, sticky="nswe") self.canvas = tk.canvas(frameone, bg="green") canvasframe = tk.frame(self.canvas, bg="yellow") canvasframe.pack() canvasframe.grid_rowconfigure(1, weight=1) scrollbar = tk.scrollbar(frameone, orient="horizontal", command=self.canvas.xview) self.canvas.configure(xscrollcommand=scrollbar.set) scrollbar.pack(side="bottom", fill="x") self.canvas.pack(fill="both", expand=true) self.canvas.create_window((0, 0), window=canvasframe, anchor="nw") x in range(5): pagename = tk.label(canvasframe, text = "unit name") pagename.grid(row=0, column=x, sticky="nsew") text = tks.scrolledtext(canvasframe, width=50) text.grid(row=1, column=x ,sticky="nsew") canvasframe.bind("<configure>", self.onframeconfigure) def onframeconfigure(self, event): '''reset scroll region encompass inner frame''' self.canvas.configure(scrollregion=self.canvas.bbox("all")) class other(tk.frame): def __init__(self, parent, controller): tk.frame.__init__(self, parent) app = program() app.state('zoomed') app.mainloop()
your canvasframe
has no reason expand fill canvas. you've put widgets in it, , frame shrink fit widgets.
the common solution add binding <configure>
event of canvas, height of canvas, , use canvas itemconfigure
method adjust height of frame same height of canvas.
class page(...): def __init__(...): ... self.frame_id = self.canvas.create_window(...) ... self.canvas.bind("<configure>", self.oncanvasconfigure) def oncanvasconfigure(self, event): # might need subtract little account borders, # padding, etc. self.canvas.itemconfigure(self.frame_id, height=event.height)
Comments
Post a Comment