简体   繁体   English

将滚动条添加到Tkinter条目或文本小部件

[英]Addiong Scrollbar to Tkinter Entry or Text widgets

I am well aware that a scrollbar can be added to a Text widget. 我很清楚可以将滚动条添加到“文本”小部件中。 but my problem is i want it read only.The only way i can do is by making the state=DISABLED, but this will block my text, hence cant copy the text. 但是我的问题是我希望它是只读的。我唯一的方法是使state = DISABLED,但这将阻止我的文本,因此无法复制文本。 Well in the Tkinter Entry widget there is no yScroll behavior. 在Tkinter Entry小部件中,没有yScroll行为。 Any idea how i can get these things worked? 知道我如何使这些东西起作用吗? Any help is appreciated. 任何帮助表示赞赏。

Right now i am using this for Text 现在我正在使用此文本

` `

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

` `

with this the problem is that the text can be edited. 这样做的问题是可以编辑文本。

The reason you can't seem to copy the text of a disabled widget is that the disabled widget on some platforms does not get focus, and focus is required to select text. 您似乎无法复制已禁用小部件的文本的原因是,在某些平台上,已禁用小部件无法获得焦点,因此选择文本时需要聚焦。 You can rectify that by adding a binding to set the focus on a mouse click. 您可以通过添加绑定将焦点设置在鼠标单击上来纠正此问题。

Add the following two lines to your code: 将以下两行添加到您的代码中:

txt.configure(state="disabled")
txt.bind("<1>", lambda event: txt.focus_set())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM