文本小部件上的Python Tkinter ttk滚动条未缩放

2024-10-03 17:22:30 发布

您现在位置:Python中文网/ 问答频道 /正文

只是在学习特金特和发现一些奇怪的行为。我的垂直滚动条是浅灰色的,它包含一个深灰色的矩形,在滚动条上下移动以显示进度,当这个代码第一次执行时,没有深灰色的进度矩形。当我按下进度条的向下箭头时,会出现深灰色框,但它会一直保持在滚动条的顶部,直到我一直滚动到文本的底部。只有这样滚动条才能决定框中文本的数量,深灰色的进度矩形才会跳到滚动条的底部,并从此处开始正常工作。我在一台Windows机器上,用python3在Jupyter笔记本上执行这个操作。你知道吗

有没有什么方法可以初始化滚动条,让它知道它要处理多少文本?你知道吗

 from tkinter import *
 from tkinter import ttk

 root = Tk()
 root.title('Twitter Timeline')

 f = ttk.Frame(root, padding = '3 3 12 12')
 f.grid(column=0, row=0, sticky=(N,W,E,S))
 root.columnconfigure(0, weight=1)
 root.rowconfigure(0, weight=1)

 t = Text(f, width= 100,  wrap='word')
 t.grid(column = 0, row = 2, sticky="nsew")
 t.insert(INSERT, "Begin " + "This is a test " * 800 + " END")

 s = ttk.Scrollbar(root, orient=VERTICAL, command=t.yview)
 s.grid(column=1, row=0, sticky='nsew')
 t['yscrollcommand'] = s.set

 for child in f.winfo_children(): 
     child.grid_configure(padx=5, pady=5)

 root.mainloop()

Tags: from文本importchildtkintercolumnrootgrid
1条回答
网友
1楼 · 发布于 2024-10-03 17:22:30

天哪,这真是个难关。当你的文本只有一行很长的时候,滚动条的反应会很奇怪。尝试插入带换行符的文本:

t.insert(INSERT, "Begin " + "This is a test \n" * 800 + " END")

它会像一个魅力。你知道吗

这似乎是grid()的问题,因为如果您使用pack()来代替它,也可以使用一条长线。。。我不知道为什么。你知道吗

相关问题 更多 >