Omit text widget from scroll when content changes

I have a text widget with a scroll list that looks something like this:

self.myWidget = Text(root) self.myWidget.configure(state=DISABLED) self.myWidget.pack() self.myWidgetScrollbar = Scrollbar(root, command=self.myWidget.yview) self.myWidget.configure(yscrollcommand=self.myWidgetScrollbar.set) self.myWidgetScrollbar.pack(side=LEFT,fill=Y) 

The text widget is updated 4 times per second:

 self.myWidget.configure(state=NORMAL) self.myWidget.delete(1.0, END) self.myWidget.insert(END, "\n".join(self.listWithStuff)) self.myWidget.configure(state=DISABLED) 

The problem is that when I try to scroll, it continues to scroll me up (maybe 4 times per second). I assume this is due to the fact that all content is deleted.

How can I prevent it from automatically scrolling, or perhaps backward when content is changed?

+3
source share
2 answers

You can save the position and set it after the update, as Tcl code does:

 proc update_view {ws data} { # store position set pos [$s get] $w configure -state normal -yscrollcommand {} $w delete 1.0 end $w insert end $data $w configure -state disabled -yscrollcommand [list $s set] $s get $s set {*}$pos } 

The Tkinter code will look something like this:

 def update_view(self, data): pos = self.myWidgetScrollbar.get() self.myWidget.configure(yscrollcommand=None, state=NORMAL) self.myWidget.delete(1.0, END) self.myWidget.insert(END, data) self.myWidget.configure(yscrollcommand=self.myWidgetScrollbar.set, state=DISABLED) self.myWidgetScrollbar.get() self.myWidgetScrollbar.set(pos) 

Not sure why get () is required between them, perhaps for a forced search, but it works.

+2
source

You can go to the bottom of the widget after each update using the yview method of the Text widget. In addition, in order to prevent user frustration when you try to scroll, you can do a simple check to make sure that the scroll bar is already at the bottom (aka the user does not scroll).

 if self.myWidgetScrollbar.get() == 1.0: self.myWidget.yview(END) 
+1
source

Source: https://habr.com/ru/post/905905/


All Articles