A
The tkinter control elements can be hidden in several ways. Let's see two of them:Methods .grid_forget() or .pack_forget() (depending on which method of accommodation has been used) and then reoccupied by the same method. The inconvenience of this method is that, for example, when placed with help .pack() The re placed control element will be added the last one (e.g., if the elements were placed above the top down, the view will be "stop" from the middle by .pack_forget()and then added to the bottom by .pack) When located .grid() You can clearly indicate where to reset the view after it's hidden, but this place needs to be scrutinized or somewhere to remember.Method .grid_remove() - works only when it's available. .grid(), however, after the hidden and posting .grid() Without parameters, the control element will return exactly to the place where it came from.The above information is taken from the response https://stackoverflow.com/a/10268076/4752653 https://stackoverflow.com/users/7432 Once we've got the stripes of the cloth inside the freima with help. .grid()We'll use the second way of hiding.When you see a class Text He wants to report that his contents have changed, and we need to rewrite the stripes of the cable, he causes the functions specified through the parameters. xscrollcommand and yscrollcommand♪ It's usually a method. .set() strips.Method .set() Accepts two parameters: the starting and end of the racing strip (in a format with a floating point from the point of view) 0.0 before 1.0to the line, i.e. from '0.0' before '1.0') When the contents are entirely satisfactory, the parameters are equal respectively '0.0' - the beginning, and '1.0' - It's over.In our implementation, we do not specify methods .set() Directly hand over its function verifying the parameters for equality '0.0' and '1.0'And when you're at the same time, you're gonna hide the strip or you're gonna look at it.import tkinter as tk
import tkinter.ttk as ttk
class CustomText(tk.Frame):
def _scrollbar_switcher(self, scrollbar, first, last):
scrollbar.set(first, last)
if first == '0.0' and last == '1.0':
scrollbar.grid_remove()
elif not scrollbar.grid_info():
scrollbar.grid()
def __init__(self, parent, *args, **kwargs):
super().__init__(parent)
yscrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL)
xscrollbar = ttk.Scrollbar(self, orient=tk.HORIZONTAL)
self._text = tk.Text(self, *args,
xscrollcommand=lambda first, last: self._scrollbar_switcher(xscrollbar, first, last),
yscrollcommand=lambda first, last: self._scrollbar_switcher(yscrollbar, first, last),
**kwargs)
yscrollbar['command'] = self._text.yview
xscrollbar['command'] = self._text.xview
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=1)
self._text.grid(row=0, column=0, sticky='NSEW')
yscrollbar.grid(row=0, column=1, sticky='NS')
xscrollbar.grid(row=1, column=0, sticky='EW')
def write(self, s):
self._text.insert(tk.END, s)
def clear(self):
self._text.delete(0.0, tk.END)
root = tk.Tk()
text = CustomText(root, wrap=tk.NONE)
text.pack(expand=1)
Просто для демонстрации циклически вносим много символов горизонтально, потом вертикально
def vertical_text():
text.clear()
text.write('*\n' * 100)
root.after(2000, horizontal_text)
def horizontal_text():
text.clear()
text.write('*' * 100)
root.after(2000, vertical_text)
vertical_text()
tk.mainloop()
Disadvantages: The size of the patrimony is reduced on its width/high (described on the size of the window below).Update: Put the box down. visible To the scrubbers, added the definition that the view would be visible from the result returned by the method .grid_info() (if not packed in a cell, returned empty vocabulary).