Python Gtk3 - 通过鼠标和courser位置在ScrolledWindow内滚动TextView

时间:2018-02-02 15:06:25

标签: python gtk

我在Gtk.TextView中有一些文字

当我用鼠标选择文本或将光标导航到底边以外时,我无法选择,因为它不会滚动。

所以我认为我需要获得光标位置并将treeview设置为此位置。 单击按钮时鼠标位置并将树视图移动到此位置。

#!/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()

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()

由于

EDIT1

这是我找到的一个非常基本的解决方案,用于将窗口滚动到箭头键改变的光标位置:

0 个答案:

没有答案