Addiong Scrollbar到Tkinter Entry或Text小部件(Addiong Scrollbar to Tkinter Entry or Text widgets)

编程入门 行业动态 更新时间:2024-10-25 06:27:01
Addiong Scrollbar到Tkinter Entry或Text小部件(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()

`

有了这个问题,可以编辑文本。

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. Well in the Tkinter Entry widget there is no yScroll behavior. 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.

最满意答案

您似乎无法复制已禁用窗口小部件文本的原因是某些平台上的已禁用窗口小部件无法获得焦点,因此需要关注选择文本。 您可以通过添加绑定来设置焦点鼠标单击来纠正它。

在代码中添加以下两行:

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

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

更多推荐

本文发布于:2023-08-07 22:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466403.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部件   Tkinter   Scrollbar   Addiong   widgets

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!