31

我正在尝试将滚动条附加到我的文本字段并且无法这样做。这是代码段:

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)

这段代码在我的文本字段旁边放置了一个非常小的滚动条(非常小,我的意思是您可以看到向上和向下箭头,但中间什么也看不到)。当我的文本字段填满时,我可以滚动它,但是有没有办法至少设置滚动条的高度,使其看起来与文本字段的高度相同?

4

4 回答 4

60

Tkinter具有三个几何管理器packgridplace
通常建议使用包装和网格。

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

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

scrollb = tkinter.Scrollbar(..., command=txt.yview)

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

txt['yscrollcommand'] = scrollb.set

这是一个使用ttk的工作示例:

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()

解决您的Scrollbar较小的部分是sticky='nsew',您可以在此处
阅读 → 。

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


tkinter.scrolledtext模块包含一个名为ScrolledText的,它是一个复合小部件(文本和滚动条)。

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()

这种实现方式值得一看。

于 2012-12-12T05:24:46.227 回答
12

如果您正在使用 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)

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

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

于 2017-12-29T22:47:33.563 回答
3

您可以使用 tkinter 框架,这是非常简单的方法 =>

import tkinter as tk
from tkinter import *
root = tk.Tk()

f = tk.Frame(root)
f.place(x=10, y=20)

scrollbar = Scrollbar(f)
t = tk.Text(f, height=10, width=10, yscrollcommand=scrollbar.set)
scrollbar.config(command=t.yview)
scrollbar.pack(side=RIGHT, fill=Y)
t.pack(side="left")

root.mainloop()
于 2020-10-13T05:21:41.217 回答
0

我相信这会奏效。这是我在我的身上使用的一段代码。您可以删除其他一些选项。我有它们是因为我的主题是黑暗的。

from tkinter import *

window = Tk()
window.title("test for scrollbar")
window.config(pady=50, padx=50, bg="white")


text_box = Text(width=49, height=5, bg="white", highlightthickness=1, foreground="black",
                    insertbackground="black", wrap="word")
    tex_scroll = Scrollbar(orient=VERTICAL,)
    
    tex_scroll.config(command=text_box.yview, )
    text_box["yscrollcommand"] = tex_scroll.set
    
    text_box.grid(column=1, row=4, columnspan=2, sticky="w")
    tex_scroll.grid(column=2, row=4, sticky="nse")

window.mainloop()
于 2022-02-27T14:08:11.907 回答