我想知道为什么我的scrollbar
对Tkinter
Text
widget
走得太远了。我有一个底部标签,应该填满下面的所有空格,但不知何故scrollbar
超出label
到最后。我不确定究竟是哪个widget
造成这种情况。
import tkinter as 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")
i = self.textwidget.index("@0,0")
while True:
dline= self.textwidget.dlineinfo(i)
if dline is None: break
y = dline[1]
linenum = str(i).split(".")[0]
self.create_text(5,y,anchor="nw", text=linenum)
i = 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 the real tk widget command with the real args
set result [uplevel [linsert $args 0 $widget_command]]
# generate the event for certain 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 the result from the 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()
答案 0 :(得分:2)
这将解决您的问题:
更改
self.vsb = tk.Scrollbar(orient=tk.VERTICAL)
到
编辑:
self.vsb = tk.Scrollbar(master=self, orient=tk.VERTICAL)
答案 1 :(得分:1)
问题是您没有为滚动条指定显式父级,因此其父级将是根窗口。修复非常简单:确保它与文本小部件具有相同的父级:
self.vsb = tk.Scrollbar(self, orient=tk.VERTICAL)