Python 如何编写 tkinter “scrolledtext” 模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17657212/
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
How to code the tkinter "scrolledtext" module
提问by Bill Waters
The code below produces an ugly but functional example of using a scroll bar in a text widget and results in a couple of questions. Note: this is done using Python 3 on a windows box.
下面的代码生成了一个在文本小部件中使用滚动条的丑陋但功能强大的示例,并导致了几个问题。注意:这是在 Windows 框上使用 Python 3 完成的。
The scroll bar that appears is attached to the frame and although it scrolls the text box contents, I would prefer that it was attached to the text widget itself. I have been unable to get this to happen.
There are a number of references to a Tkinter module called "scrolledtext" that is supposed to be a much better mechanism for adding scrollbars to text boxes but, I have not found any examples of how to import it and call it that I am able to get to work (probably need an example).
出现的滚动条附加到框架上,虽然它滚动文本框内容,但我更希望它附加到文本小部件本身。我一直无法做到这一点。
有许多对名为“scrolledtext”的 Tkinter 模块的引用,它应该是一种更好的向文本框添加滚动条的机制,但是,我还没有找到任何关于如何导入和调用它的示例我能够开始工作(可能需要一个例子)。
frame1 = tk.Frame(win,width=80, height=80,bg = '#808000')
frame1.pack(fill='both', expand='yes')
scrollbar = Scrollbar(frame1)
scrollbar.pack(side=RIGHT, fill=Y)
editArea = Text(frame1, width=10, height=10, wrap=WORD, yscrollcommand=scrollbar.set)
editArea.pack()
scrollbar.config(command=editArea.yview)
editArea.place(x=10,y=30)
回答by Peter Varo
You were right, you can use the ScrolledText
widget from tkinter.scrolledtext
module, like this:
您说得对,您可以使用模块中的ScrolledText
小部件tkinter.scrolledtext
,如下所示:
import tkinter as tk
import tkinter.scrolledtext as tkst
win = tk.Tk()
frame1 = tk.Frame(
master = win,
bg = '#808000'
)
frame1.pack(fill='both', expand='yes')
editArea = tkst.ScrolledText(
master = frame1,
wrap = tk.WORD,
width = 20,
height = 10
)
# Don't use widget.place(), use pack or grid instead, since
# They behave better on scaling the window -- and you don't
# have to calculate it manually!
editArea.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
# Adding some text, to see if scroll is working as we expect it
editArea.insert(tk.INSERT,
"""\
Integer posuere erat a ante venenatis dapibus.
Posuere velit aliquet.
Aenean eu leo quam. Pellentesque ornare sem.
Lacinia quam venenatis vestibulum.
Nulla vitae elit libero, a pharetra augue.
Cum sociis natoque penatibus et magnis dis.
Parturient montes, nascetur ridiculus mus.
""")
win.mainloop()
And there you go:
然后你去:
回答by Bryan Oakley
While using the ScrolledText widget might save you a few lines of code, it's not doing anything you can't do yourself. Doing it yourself will help remove some of the mystery.
虽然使用 ScrolledText 小部件可能会为您节省几行代码,但它不会做任何您自己无法完成的事情。自己动手将有助于消除一些谜团。
You're actually doing everything almost correct. The mistake you're making is that you should have your text widget fill it's container completely rather than being just a tiny part of it.
你实际上做的一切几乎都是正确的。你犯的错误是你应该让你的文本小部件完全填充它的容器,而不是只是它的一小部分。
The standard way to do this is like this:
这样做的标准方法是这样的:
container = tk.Frame(...)
text = tk.Text(container, ...)
scrollbar = tk.Scrollbar(container, ...)
scrollbar.pack(side="right", fill="y")
text.pack(side="left", fill="both", expand=True)
That's all there is to it. Now, no matter how big your container gets due to window resizes, etc, the scrollbar will always appear attached to the text widget. You then treat this whole group of container, text widget and scrollbar as a single widget when adding it to your whole GUI.
这里的所有都是它的。现在,无论您的容器由于窗口大小调整等而变大,滚动条将始终显示为附加到文本小部件。然后,将整组容器、文本小部件和滚动条添加到整个 GUI 时,将其视为单个小部件。
Note that you can use grid here as well. Pack is easier if you only have a vertical scrollbar. If you have both horizontal and vertical scrollbars, grid makes a little more sense.
请注意,您也可以在此处使用 grid。如果您只有垂直滚动条,则打包更容易。如果您同时拥有水平和垂直滚动条,则网格更有意义。
To complete the illusion, you can set the borderwidth of the text widget to zero, and set the borderwidth of the containing frame to 1 with a relief of sunken, and the scrollbar will appear to be "in" the text widget.
要完成这种错觉,您可以将文本小部件的边框宽度设置为零,并将包含框架的边框宽度设置为 1 并带有凹陷的浮雕,滚动条将出现在文本小部件的“内部”。
Here is a complete working example that looks more-or-less like your example:
这是一个完整的工作示例,看起来或多或少像您的示例:
import Tkinter as tk
win = tk.Tk()
win.configure(background="#808000")
frame1 = tk.Frame(win,width=80, height=80,bg = '#ffffff',
borderwidth=1, relief="sunken")
scrollbar = tk.Scrollbar(frame1)
editArea = tk.Text(frame1, width=10, height=10, wrap="word",
yscrollcommand=scrollbar.set,
borderwidth=0, highlightthickness=0)
scrollbar.config(command=editArea.yview)
scrollbar.pack(side="right", fill="y")
editArea.pack(side="left", fill="both", expand=True)
frame1.place(x=10,y=30)
win.mainloop()
Personally I don't recommend doing global imports, and I don't recommend using place, but I wanted to keep this as close to your original as possible.
就我个人而言,我不建议进行全局导入,也不建议使用 place,但我想让它尽可能接近您的原始版本。