Sunday 15 January 2012

python - Create scrollbar only when text length greater than text area -



python - Create scrollbar only when text length greater than text area -

here simple code:

from tkinter import * tkinter import ttk rootwin = tk() roomtext = text(rootwin) roomtext.pack(side = 'left', fill = "both", expand = true) rtas = ttk.scrollbar(roomtext, orient = "vertical", command = roomtext.yview) rtas.pack(side = "right" , fill = "both") roomtext.config(yscrollcommand = rtas.set) rootwin.mainloop()

as such, default scrollbar appears straight away. how possible create scrollbar appear 1 time text entered greater text area?

so when run code, first, scrollbar must not show. when plenty text entered scrollbar shows (i.e. text in roomtext longer roomtext area).

maybe code looking (changed pack grid i'm more familiar it... should able revert if want):

from tkinter import * tkinter import ttk rootwin = tk() roomtext = text(rootwin) roomtext.grid(column=0, row=0) def create_scrollbar(): if roomtext.cget('height') < int(roomtext.index('end-1c').split('.')[0]): rtas = ttk.scrollbar(rootwin, orient = "vertical", command = roomtext.yview) rtas.grid(column=1, row=0, sticky=n+s) roomtext.config(yscrollcommand = rtas.set) else: rootwin.after(100, create_scrollbar) create_scrollbar() rootwin.mainloop()

it checks if needs create scrollbar 10 times every second. additional changes can create remove scrollbar when no longer needed (text short):

from tkinter import * tkinter import ttk rootwin = tk() roomtext = text(rootwin) roomtext.grid(column=0, row=0) rtas = ttk.scrollbar(rootwin, orient = "vertical", command = roomtext.yview) def show_scrollbar(): if roomtext.cget('height') < int(roomtext.index('end-1c').split('.')[0]): rtas.grid(column=1, row=0, sticky=n+s) roomtext.config(yscrollcommand = rtas.set) rootwin.after(100, hide_scrollbar) else: rootwin.after(100, show_scrollbar) def hide_scrollbar(): if roomtext.cget('height') >= int(roomtext.index('end-1c').split('.')[0]): rtas.grid_forget() roomtext.config(yscrollcommand = none) rootwin.after(100, show_scrollbar) else: rootwin.after(100, hide_scrollbar) show_scrollbar() rootwin.mainloop()

python python-3.x tkinter

No comments:

Post a Comment