python - Tkinter Text Scrollbar goes Too Far -
i wondering why scrollbar
goes far tkinter
text
widget
. have bottom label supposed fill space below, yet somehow scrollbar
reaches beyond label
end. not sure widget
causing this.
import tkinter tk import tkinter.filedialog class textlinenumbers(tk.canvas): def __init__(self, *args, **kwargs): tk.canvas.__init__(self, *args, **kwargs) self.textwidget = none def attach(self, text_widget): self.textwidget = text_widget def redraw(self, *args): '''redraw line numbers''' self.delete("all") = self.textwidget.index("@0,0") while true: dline= self.textwidget.dlineinfo(i) if dline none: break y = dline[1] linenum = str(i).split(".")[0] self.create_text(5,y,anchor="nw", text=linenum) = self.textwidget.index("%s+1line" % i) class customtext(tk.text): def __init__(self, *args, **kwargs): tk.text.__init__(self, *args, **kwargs) self.tk.eval(''' proc widget_proxy {widget widget_command args} { # call real tk widget command real args set result [uplevel [linsert $args 0 $widget_command]] # generate event types of commands if {([lindex $args 0] in {insert replace delete}) || ([lrange $args 0 2] == {mark set insert}) || ([lrange $args 0 1] == {xview moveto}) || ([lrange $args 0 1] == {xview scroll}) || ([lrange $args 0 1] == {yview moveto}) || ([lrange $args 0 1] == {yview scroll})} { event generate $widget <<change>> -when tail } # return result real widget command return $result } ''') self.tk.eval(''' rename {widget} _{widget} interp alias {{}} ::{widget} {{}} widget_proxy {widget} _{widget} '''.format(widget=str(self))) self.comment = false class window(tk.frame): def __init__(self, *args, **kwargs): tk.frame.__init__(self, *args, **kwargs) self.bottomlabel() self.createtext() def createtext(self): self.text = customtext(self, bd=0) self.vsb = tk.scrollbar(orient=tk.vertical) self.text.configure(yscrollcommand=self.vsb.set) self.vsb.configure(command=self.text.yview) self.linenumbers = textlinenumbers(self, width=55) self.linenumbers.attach(self.text) self.vsb.pack(side=tk.right, fill=tk.y) self.linenumbers.pack(side="left", fill="y") self.text.pack(side="right", fill="both", expand=true) self.text.bind("<<change>>", self._on_change) self.text.bind("<configure>", self._on_change) def bottomlabel(self): self.label = tk.label(self, text="lorem ipsum", anchor=tk.w) self.label.pack(fill=tk.x, side=tk.bottom) def _on_change(self, event): self.linenumbers.redraw() if __name__ == "__main__": root = tk.tk() root.title("window") window = window(root).pack(side="top", fill="both", expand=true) root.mainloop()
the problem didn't give scrollbar explicit parent, parent root window. fix pretty simple: make sure has same parent text widget:
self.vsb = tk.scrollbar(self, orient=tk.vertical)
Comments
Post a Comment