如何对tkinter的"scrolledtext"进行编码.模块 [英] How to code the tkinter "scrolledtext" module
问题描述
下面的代码产生了一个丑陋但实用的示例,该示例在文本小部件中使用滚动条,并引发了几个问题.注意:这是在Windows框上使用Python 3完成的.
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.
-
出现的滚动条被附加到框架上,尽管它滚动了文本框的内容,但我更希望它被附加到文本小部件本身.我一直无法做到这一点.
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.
Tkinter模块有许多引用,称为"scrolledtext",应该是将滚动条添加到文本框中的一种更好的机制,但是,我还没有找到任何有关如何导入和调用它的示例.我能够上班(可能需要一个例子).
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).
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)
推荐答案
您是对的,您可以使用tkinter.scrolledtext
模块中的ScrolledText
小部件,如下所示:
You were right, you can use the ScrolledText
widget from tkinter.scrolledtext
module, like this:
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()
然后去:
这篇关于如何对tkinter的"scrolledtext"进行编码.模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!