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