[英]Tkinter Scrollbar in frame with multiple textframes
Question 题
How can I have a scrollbar which moves an entire Tkinter frame? 我如何有一个滚动条来移动整个Tkinter框架? Note: I am using Python 2.7.3.
注意:我正在使用Python 2.7.3。
Code and Explanation 代码和解释
I have this code for defining the scrollbar 我有用于定义滚动条的代码
scrollbar = Scrollbar(soeg)
scrollbar.pack(side=RIGHT, fill="y")
And this code for defining the textframes 和此代码用于定义文本框架
h = 0
s = 0
for i in dom_nodup:
abc = dom_nodup[h]
text = Text(soeg, bg="brown", fg="white", height="10", width="60")
text.insert(INSERT, "%s \n" % abc[0])
text.insert(END, "%s \n\n\n" % abc[1])
text.pack()
h += 1
s += 1
A new textframe is created for each text entity for later easier overview (planning on having a button to show/hide the input). 将为每个文本实体创建一个新的文本框架,以便日后更轻松地查看(计划有一个按钮来显示/隐藏输入)。
The scrollbar is present but is not functional 滚动条存在但不起作用
I would recommend that you use the ScrolledText
widget. 我建议您使用
ScrolledText
小部件。 It automatically adds a scrollbar to each text widget, and has the same arguments as Text
. 它会自动向每个文本小部件添加滚动条,并具有与
Text
相同的参数。 Here is a brief example of how to do it. 这是一个简单的例子。
from Tkinter import * #Import the Tkinter module
from ScrolledText import ScrolledText #import the scrolled text module
message = "I \n am \n scroll \n able. \n\n\n\n\n\n Yes I am!"
class Application(Frame): #Create a frame for the widgets
def __init__(self, master): #initialize the grid and widgets
Frame.__init__(self,master)
self.grid()
self.widgets()
def widgets(self):
self.mytext = ScrolledText(self, width = 10) #Creates the widget
self.mytext.grid() #Places it
root = Tk()
root.title("My Text Example")
#make my screen dimensions work
root.geometry("500x1000")
app = Application(root)
root.mainloop()
For more information, please see the Tkinterbook and this question . 有关更多信息,请参见Tkinterbook和此问题 。
To make a scrollbar functional you must do two things: you must tell it which scrollable widget to scroll, and you must tell the scrollable widget which scrollbar to update with the current position. 要使滚动条正常工作,您必须做两件事:必须告诉它要滚动哪个滚动小部件,并且必须告诉可滚动小部件哪个滚动条要更新当前位置。
scrollbar.configure(command=text.yview)
text.configure(yscrollcommand=scrollbar.set)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.