Preventing textbox from going back to start of textbox when updating it

前端 未结 1 1830
谎友^
谎友^ 2021-01-24 18:59

Is there a way to make the textbox freeze its position as its updating?

I\'m making updating a textbox in TKinter every 1 second but to display some constantly changing

相关标签:
1条回答
  • 2021-01-24 19:27

    If you want the widget to automatically scroll to the end after inserting text, run the command self.output.see("end").

    self.output.insert(END, text_str)
    self.output.see("end")
    

    If the goal isn't to stay at the bottom, but rather to keep the scrollbar where it is, you can query the scrollbar to find it's position and then attempt to reset it. This won't work perfectly if the new data has more or fewer lines than the current data, but there's simply no avoiding that.

    For example:

    def click2(self):
        # get the current position of the text
        yview = self.output.yview()
    
        ...
    
        self.output.insert(END, text_str)
        self.output.yview_moveto(yview[0])
    
        self.root.after(1000, self.click2)
    
    0 讨论(0)
提交回复
热议问题