gpt4 book ai didi

python - Tkinter 文本滚动条走得太远

转载 作者:行者123 更新时间:2023-11-30 23:07:50 33 4
gpt4 key购买 nike

我想知道为什么我的 scrollbar 对于我的 Tkinter Text 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()

最佳答案

这将解决您的问题:

改变self.vsb = tk.Scrollbar(orient=tk.VERTICAL)

编辑:

self.vsb = tk.Scrollbar(master=self, orient=tk.VERTICAL)

关于python - Tkinter 文本滚动条走得太远,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32043228/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com