python - Tkinter canvas not scrolling -
tkinter experts, i'm having trouble getting canvas scroll. second gui, , i've done similar before, don't know i'm doing wrong. i'd appreciate can offer.
here's minimal version of i'm trying do. i'm using python 3.4.3 on windows 10.
import tkinter tk import tkinter.font tk_font import tkinter.ttk ttk import random def get_string_var(parent, value=''): var = tk.stringvar(parent) var.set(value) return var class summaryframe(ttk.frame): def __init__(self, parent, **kwargs): ttk.frame.__init__(self, parent, **kwargs) var_names = ['label_'+str(num) num in range(1, 20)] self.vars = {} name in var_names: self.vars[name] = get_string_var(self) self._add_summary_labels(self, self.vars, 1) @staticmethod def _add_summary_labels(frame, vars, start_row): current_row = start_row name in vars: tk.label(frame, text=name, anchor=tk.n+tk.w).grid(row=current_row, column=0, sticky=tk.n+tk.s+tk.w+tk.e) text_label = tk.label(frame, wraplength=200, textvariable=vars[name], anchor=tk.n+tk.w, justify=tk.left) text_label.grid(row=current_row, column=1, sticky=tk.w) current_row += 1 def set_summary_fields(self, info): name in info: if name in self.vars: self.vars[name].set(info[name]) class outerframe(ttk.frame): def __init__(self, parent, **kwargs): ttk.frame.__init__(self, parent, **kwargs) self.canvas = tk.canvas(self) scrollbar = ttk.scrollbar(self, orient=tk.vertical, command=self.canvas.yview) self.canvas.configure(yscrollcommand=scrollbar.set) self.summary = summaryframe(self.canvas) self.summary.pack(fill=tk.both, expand=1) self.canvas.pack(side=tk.left, fill=tk.both, expand=1) scrollbar.pack(side=tk.left, fill=tk.y, expand=1) label_text = {} in range(1, 20): label_text['label_'+str(i)] = "information " * random.randint(1, 20) self.set_fields(label_text) def set_fields(self, info): self.summary.set_summary_fields(info) self.canvas.configure(scrollregion=(1, 1, self.summary.winfo_width(), self.summary.winfo_height())) if __name__ == "__main__": root = tk.tk() frame = outerframe(root) frame.pack(fill=tk.both, expand=1) root.mainloop()
the scrollbar should change when contents of inner summaryframe expands, doesn't. remains grayed out , inoperable. doing wrong?
short answer: configuring scrollregion 1 pixel 1 pixel.
you configuring scrollregion
based on width , height of self.summary
, doing before window has chance mapped screen. width , height, therefore, both one.
you need wait window drawn before computing width , height of window.
Comments
Post a Comment