简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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