如何将滚动条附加到文本小部件?

jwe*_*ter 17 python tkinter

我可能过度思考了这一点,但由于某种原因,我似乎无法弄清楚这一点.我试图将滚动条附加到我的文本字段,但无法这样做.这是代码段:

self.scroller = Scrollbar(self.root)
self.scroller.place(x=706, y=121)
self.outputArea = Text(self.root, height=26, width=100) 
self.outputArea.place(x=0, y=120)
self.scroller.config(command=self.outputArea.yview)
self.outputArea.config(state=DISABLED, yscrollcommand = self.scroller.set)
Run Code Online (Sandbox Code Playgroud)

这段代码在我的文本字段旁边放置一个非常小的滚动条(非常小,我的意思是你可以看到向上和向下箭头,但两者之间没有任何东西).当我的文本字段填满时,我可以用它滚动,但有没有办法至少设置滚动条的高度,使它看起来与文本字段的高度相同?

Hon*_*Abe 43

Tkinter有三个几何管理器:,网格地点.
通常建议使用包装和网格.

您可以使用网格管理器的 选项
滚动条放在" 文本"小部件旁边.

Scrollbar小部件的命令选项设置为Text的yview方法.

scrollb = tkinter.Scrollbar(..., command=txt.yview)
Run Code Online (Sandbox Code Playgroud)

Text小部件的yscrollcommand选项设置为Scrollbar的set方法.

txt['yscrollcommand'] = scrollb.set
Run Code Online (Sandbox Code Playgroud)

这是一个有效的例子:

import tkinter
import tkinter.ttk as ttk

class TextScrollCombo(ttk.Frame):

    def __init__(self, *args, **kwargs):

        super().__init__(*args, **kwargs)

    # ensure a consistent GUI size
        self.grid_propagate(False)
    # implement stretchability
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

    # create a Text widget
        self.txt = tkinter.Text(self)
        self.txt.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)

    # create a Scrollbar and associate it with txt
        scrollb = ttk.Scrollbar(self, command=self.txt.yview)
        scrollb.grid(row=0, column=1, sticky='nsew')
        self.txt['yscrollcommand'] = scrollb.set

main_window = tkinter.Tk()

combo = TextScrollCombo(main_window)
combo.pack(fill="both", expand=True)
combo.config(width=600, height=600)

combo.txt.config(font=("consolas", 12), undo=True, wrap='word')
combo.txt.config(borderwidth=3, relief="sunken")

style = ttk.Style()
style.theme_use('clam')

main_window.mainloop()
Run Code Online (Sandbox Code Playgroud)

这将解决您的部分滚动条是小的sticky='nsew',
你可以阅读→ 这里点击这里.

现在对你有所帮助的东西是,不同的Tkinter小部件可以在同一个程序中使用不同的几何管理器,只要它们不共享同一个父级.


另外值得一提的是ScrolledText模块(改名tkinter.scrolledtext在Python3).它包含一个类,也称为ScrolledText,它是一个复合小部件(Text&Scrollbar).

import tkinter
import tkinter.scrolledtext as scrolledtext

main_window = tkinter.Tk()

txt = scrolledtext.ScrolledText(main_window, undo=True)
txt['font'] = ('consolas', '12')
txt.pack(expand=True, fill='both')

main_window.mainloop()
Run Code Online (Sandbox Code Playgroud)

  • 使用`tkinter.scrolledtext`解决方案,更容易:) (3认同)
  • 我会改变"通常建议在地方"到"*总是*建议在地方".地方很有用,但仅适用于极少数情况. (2认同)

Mik*_*eyB 10

如果您正在使用 INPUT 框,那么使用该scrolledtext功能是一种方便的方式。我花了4个多小时才找到它。你不喜欢 tkinter 吗?

需要注意的两件事...额外的导入需要 import tkinter.scrolledtext as tkscrolled 并且您使用设置默认值insert并使用读取值get(更糟糕的命名)

这段代码是使我的 20 个字符宽 x 10 行文本输入框工作的核心。

import tkinter.scrolledtext as tkscrolled
import tkinter as tk

default_text = '1234'
width, height = 20,10
TKScrollTXT = tkscrolled.ScrolledText(10, width=width, height=height, wrap='word')

# set default text if desired
TKScrollTXT.insert(1.0, default_text)
TKScrollTXT.pack(side=tk.LEFT)
Run Code Online (Sandbox Code Playgroud)

一旦达到调用中定义的高度,滚动条就会出现。它们变灰并很好地融入背景。它工作得很好..... 一旦你找出正确的电话。

我希望这与您的问题有关!