Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Gtk3 - Scroll TextView inside of ScrolledWindow by mouse and courser position

Tags:

python

gtk

I have some text in a Gtk.TextView

When I select the text with my mouse or navigate the cursor beyond the bottom edge I cannot se my selection because it does not scroll.

So I think I need to get cursor position and set treeview to this position. And mouse position while button is clicked and move treeview to this position.

  • https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeView
  • https://lazka.github.io/pgi-docs/Gtk-3.0/classes/ScrolledWindow
  • https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Widget
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class ScrolledWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(200, 200)
        self.connect("destroy", Gtk.main_quit)

        self.scrolledwindow = Gtk.ScrolledWindow()

        self.add(self.scrolledwindow)

        box = Gtk.Box()
        self.scrolledwindow.add(box)

        textview = Gtk.TextView()
        box.pack_start(textview, True, True, 10)
        
        textview.connect("move-cursor", self.move_cursor_event) # arrow keys
        textview.connect("key-press-event", self.key_press_event) # keyboard input
        textview.connect("button-press-event", self.button_press_event) # mouse click 
        
        textbuffer = textview.get_buffer()
        
        text = 50*"Hello World\n"
        
        textbuffer.set_text(text, len(text))
        
    def move_cursor_event(self, tree_view, step, count, extend_selection):
        print "move_cursor_event", step, count, extend_selection
        
    def key_press_event(self, widget, event):
        print "key_press_event", event
        
    def button_press_event(self, widget, event):
        print "button_press_event", event.x, event.y
        position = self.scrolledwindow.get_vadjustment()
        position.set_value(350) 
        self.scrolledwindow.set_vadjustment(position)  #scroll to the middle of the scolled window
        

window = ScrolledWindow()
window.show_all()

Gtk.main()

Thanks

edit1

here is a very basic solution I found for scrolling the window to the cursor position changed by arrow keys:

def move_cursor_event(self, tree_view, step, count, extend_selection):
    rect = tree_view.get_cursor_locations()
    self.scroll_y(rect.strong.y)

def scroll_y(self, ypos):
    vadj = self.scrolled_window.get_vadjustment()
    vadj.set_value(ypos-80) 
    self.scrolled_window.set_vadjustment(vadj)
like image 962
oxidworks Avatar asked Feb 02 '18 15:02

oxidworks


1 Answers

The problem here is that textview is put inside a box, which is then put inside self.scrolledwindow. When the TextView is inside a Box, it prevents the ScrolledWindow from recognizing that the TextView needs to be scrolled. Placing the TextView directly in the ScrolledWindow should fix this problem. Here is what the code should look like:

#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class ScrolledWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(200, 200)
        self.connect("destroy", Gtk.main_quit)

        self.scrolledwindow = Gtk.ScrolledWindow()

        self.add(self.scrolledwindow)

        textview = Gtk.TextView()
        self.scrolledwindow.add(textview)
        
        textbuffer = textview.get_buffer()
        
        text = 50*"Hello World\n"
        
        textbuffer.set_text(text, len(text))  #scroll to the middle of the scolled window
        
window = ScrolledWindow()
window.show_all()

Gtk.main()
like image 100
Sylvester Kruin Avatar answered Oct 27 '22 05:10

Sylvester Kruin