两个Gtk TextView小部件与共享的滚动条 [英] Two Gtk TextView widgets with shared Scrollbar

查看:488
本文介绍了两个Gtk TextView小部件与共享的滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有两个并列的TextView小部件一侧,与单一滚动一起滚动。

I want to have two TextView widgets side by side which are scrolled together with a single scrollbar.

我可以将两个TextView的部件在横向盒,然后将它们添加一个视口,然后到ScrolledWindow。然而,这是行不通的我多么希望。从滚动条滚动将工作。但在TextView中发生的行为不会改变滚动位置。 (箭头键,向上翻页,向下翻页等),我也不能编程的方法修改与TextView.ScrollToMark等其他TextView的滚动方式滚动。

I can put both TextView widgets in a Hbox and then add them to a Viewport and then to a ScrolledWindow. However this will not work how I want. Scrolling from the scrollbar will work. But actions occurring in the TextView wont change the scroll position. (arrow keys, page up, page down etc) and I also can't programatically change the scrolling with TextView.ScrollToMark and other other TextView scrolling methods.

我怎么能有两个TextView的部件共用一个滚动条,并有在TextView中的更新操作滚动?

How can I have two TextView widgets share a scrollbar and have actions in the TextView's update the scrolling?

推荐答案

关键是使用ScrolledWindow代替一视口,并以具有ScrolledWindows之间共享的调整。视口将只在滚动一个widget是太大,不适合在容器中。另一方面,在ScrolledWindow链接其调整到TextView的调整,这是由光标的移动的影响。

The key is to use a ScrolledWindow instead of a Viewport, and to have the adjustments shared between the ScrolledWindows. The Viewport will just scroll over a widget which is too large to fit in the container. On the other hand, the ScrolledWindow links its adjustments to the TextView adjustments, which are influenced by cursor movement.

我设法得到此通过链接ScrolledWindows之间的调整工作,隐藏自己的滚动条,然后把它们放在一个大ScrolledWindow,仅仅控制所有的子窗口滚动条(未示出)。

I managed to get this to work by linking the adjustments between ScrolledWindows, hiding their scrollbars, and then putting them in one big ScrolledWindow that just controls all the subwindow scrollbars (which are not shown).

下面是在Python的完整代码解决方案。你应该能够很容易适应此为C#。

Below is the full code solution in Python. You should be able to adapt this easily to C#.

#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk

if __name__ == "__main__":
    window = gtk.Window()

    box = gtk.HBox()

    textview1 = gtk.TextView()
    textview2 = gtk.TextView()

    hadjustment = None
    vadjustment = None

    for textview in (textview1, textview2):
        sw = gtk.ScrolledWindow()
        # don't show the scrollbars on these sub-scrolledwindows
        sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
        sw.add(textview)

        # use the first scrolledwindow's adjustments
        if hadjustment is None:
            hadjustment = sw.get_hadjustment()
        else:
            sw.set_hadjustment(hadjustment)
        if vadjustment is None:
            vadjustment = sw.get_vadjustment()
        else:
            sw.set_vadjustment(vadjustment)

        box.pack_start(sw, padding=5)

        buffer = textview.get_buffer()
        buffer.set_text("If a widget has native scrolling abilities,\n"
                        " it can be added to the gtk.ScrolledWindow\n"
                        "with the gtk.Container.add() method. If a\n"
                        "widget does not, you must first add the\n"
                        "widget to a gtk.Viewport, then add the\n"
                        "gtk.Viewport to the scrolled window. The\n"
                        "convenience method add_with_viewport() does\n"
                        "exactly this, so you can ignore the presence\n"
                        "of the viewport.")

    # this scrolled window will encompass the rest and have scrollbars
    main_sw = gtk.ScrolledWindow(hadjustment, vadjustment)
    main_sw.set_policy(gtk.POLICY_ALWAYS, gtk.POLICY_ALWAYS)
    main_sw.add(box)
    window.add(main_sw)

    window.show_all()
    gtk.main()

这篇关于两个Gtk TextView小部件与共享的滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆