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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13832720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:42:24  来源:igfitidea点击:

How to attach a Scrollbar to a Text widget?

pythontkinter

提问by jwebster

I am trying to attach a scrollbar to my Text field and have been unable to do so. Here is the segment of code:

我试图将滚动条附加到我的文本字段,但一直无法这样做。这是代码段:

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)

This code is placing a very small scrollbar right next to my text field (and by very small, I mean that you can see the up and down arrows but nothing in between). I can scroll with it when my text field fills up, but is there a way to at the very least set the height of the scrollbar so that it appears to be the same height as the text field?

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

采纳答案by Honest Abe

Tkinterhas three geometry managers: pack, grid, and place.
Pack and grid are usually recommended over place.

Tkinter具有三个几何管理器packgridplace
包和网格通常推荐在地方。

You can use the grid manager'srowand columnoptions
to position the Scrollbarnext to the Textwidget.

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

Set the Scrollbarwidget's commandoption to the Text's yviewmethod.

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

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

Set the Textwidget's yscrollcommandoption to the Scrollbar's setmethod.

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

txt['yscrollcommand'] = scrollb.set

Here's a working example that makes use of ttk:

这是一个使用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()

The part that will address your Scrollbarbeing small is sticky='nsew',
which you can read about → here.

解决您的滚动条很小的部分是sticky='nsew'
您可以在此处阅读 → 。

Something that will be helpful for you to learn right now is that different Tkinterwidgets can use different geometry managerswithin the same program as long as they do not share the same parent.

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



The tkinter.scrolledtextmodule contains a class called ScrolledTextwhich is a compound widget (Text & Scrollbar).

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

The way this is implementedis worth taking a look at.

实现方式值得一看。

回答by MikeyB

If you're working with an INPUT box, then a handy way using the scrolledtextfunction. It took me 4+ hours to find it. Don't ya love tkinter?

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

Two things to note... The additional import required import tkinter.scrolledtext as tkscrolled and you set default value using insertand read the value using get(more terrible naming)

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

This bit of code was central to making my 20 character wide by 10 lines text input box to work.

这段代码是使我的 20 个字符宽乘 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)

The scroll bars show up once reaching the height as defined in the call. They are grayed out and blend into the background well. It works great..... once you figure out the correct calls to make.

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

I hope this is relevant to your question!

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