python - Create a fix button with scrollbar in Tkinter -
i run example scrollbar based here. want add button (prev) frame , when scrollbar moves down, button still stay there, while other content moves along scrollbar.
here code:
import tkinter tk class example(tk.frame): def __init__(self, root): tk.frame.__init__(self, root) self.canvas = tk.canvas(root, borderwidth=0, background="#ffffff") self.frame = tk.frame(self.canvas, background="#ffffff") self.vsb = tk.scrollbar(root, orient="vertical", command=self.canvas.yview) self.canvas.configure(yscrollcommand=self.vsb.set) self.vsb.pack(side="right", fill="y") self.canvas.pack(side="left", fill="both", expand=true) self.canvas.create_window((4,4), window=self.frame, anchor="nw", tags="self.frame") self.frame.bind("<configure>", self.onframeconfigure) self.populate() def populate(self): button = tk.button(self.frame, text='prev') button.grid(row=0, columns=1) row in range(1,100): tk.label(self.frame, text="%s" % row, width=3, borderwidth="1", relief="solid").grid(row=row, column=0) t="this second column row %s" %row tk.label(self.frame, text=t).grid(row=row, column=1) def onframeconfigure(self, event): '''reset scroll region encompass inner frame''' self.canvas.configure(scrollregion=self.canvas.bbox("all")) if __name__ == "__main__": root=tk.tk() example(root).pack(side="top", fill="both", expand=true) root.mainloop()
can me solve problem? many thanks!
you pack()
prefer use grid()
what need place button in root on row 0 , place class frame on row 1 of root. should fix problem.
when using grid()
need manage weights of rows , columns growing , shrinking behavior when resize window. can rowconfigute()
, columnconfigute()
. program need configure weights of root.
to button stay @ left can use sticky = "w"
inside of grid()
chose side stick to.
take @ below code:
import tkinter tk class example(tk.frame): def __init__(self, root): tk.frame.__init__(self, root) self.canvas = tk.canvas(root, borderwidth=0, background="#ffffff") self.frame = tk.frame(self.canvas, background="#ffffff") self.vsb = tk.scrollbar(root, orient="vertical", command=self.canvas.yview) self.canvas.configure(yscrollcommand=self.vsb.set) self.vsb.grid(row = 1, column = 1, sticky = "nsew") self.canvas.grid(row = 1, column = 0, sticky = "nsew") self.canvas.create_window((4,4), window=self.frame, anchor="nw", tags="self.frame") self.frame.bind("<configure>", self.onframeconfigure) self.populate() def populate(self): button = tk.button(root, text='prev') button.grid(row=0, columns=1, sticky = "w") row in range(1,100): tk.label(self.frame, text="%s" % row, width=3, borderwidth="1", relief="solid").grid(row=row, column=0) t="this second column row %s" %row tk.label(self.frame, text=t).grid(row=row, column=1) def onframeconfigure(self, event): '''reset scroll region encompass inner frame''' self.canvas.configure(scrollregion=self.canvas.bbox("all")) if __name__ == "__main__": root=tk.tk() root.columnconfigure(0, weight = 1) root.rowconfigure(1, weight = 1) example(root).grid(row = 1) root.mainloop()
Comments
Post a Comment