[英]Addiong Scrollbar to Tkinter Entry or Text widgets
我很清楚可以将滚动条添加到“文本”小部件中。 但是我的问题是我希望它是只读的。我唯一的方法是使state = DISABLED,但这将阻止我的文本,因此无法复制文本。 在Tkinter Entry小部件中,没有yScroll行为。 知道我如何使这些东西起作用吗? 任何帮助表示赞赏。
现在我正在使用此文本
`
root=Tk()
txt = Text(root, height=5, width=55)
scr = Scrollbar(root)
scr.config(command=txt.yview)
txt.config(yscrollcommand=scr.set)
txt.pack(side=LEFT)
txt.insert(INSERT, "hello world\nhello world\n hello world\n hello world\n hello world\n hello world\n hello world\n hello world\n hello world\n hello world\n")
txt.insert(END,"\n")
scr.pack(side="right", fill="y", expand=False)
txt.pack(side="left", fill="both", expand=True)
root.mainloop()
`
这样做的问题是可以编辑文本。
您似乎无法复制已禁用小部件的文本的原因是,在某些平台上,已禁用小部件无法获得焦点,因此选择文本时需要聚焦。 您可以通过添加绑定将焦点设置在鼠标单击上来纠正此问题。
将以下两行添加到您的代码中:
txt.configure(state="disabled")
txt.bind("<1>", lambda event: txt.focus_set())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.