简体   繁体   中英

PyGTK: Odd Table sizing behavior in a ScrolledWindow

I am noticing some strange behavior from a gtk.Table. Try running this code:

import gtk

class Scrollbar:
    def __init__(self):
        window = gtk.Window()
        window.set_default_size(200, 200)

        button1 = gtk.Button("Button 1")
        button2 = gtk.Button("Button 2")

        table = gtk.Table(20, 20)
        for i in xrange(20):
            for j in range(20):
                table.attach(gtk.Label("%d, %d" % (i, j)), i, i+1, j, j+1, 
                    gtk.FILL | gtk.SHRINK, gtk.FILL | gtk.SHRINK)

        viewport = gtk.Viewport()
        viewport.add(table)

        vadjust = viewport.get_vadjustment()
        hadjust = viewport.get_hadjustment()        

        vscroll = gtk.VScrollbar(vadjust)
        hscroll = gtk.HScrollbar(hadjust)

        superTable = gtk.Table(2, 2, False)
        superTable.attach(viewport, 0, 1, 0, 1, gtk.FILL | gtk.EXPAND, gtk.FILL | gtk.EXPAND)
        superTable.attach(vscroll, 1, 2, 0, 1, gtk.FILL | gtk.SHRINK, gtk.FILL | gtk.SHRINK)
        superTable.attach(hscroll, 0, 1, 1, 2, gtk.FILL | gtk.SHRINK, gtk.FILL | gtk.SHRINK)

        window.add(superTable)

        window.connect("destroy", lambda w: gtk.main_quit())
        window.show_all()

Scrollbar()
gtk.main()

When I try this, the minimum size of the window becomes large enough to accommodate the whole Table without scrolling, which becomes impractical for large Tables. If I use a ScrolledWindow this doesn't happen. Why? (This question has to do with an application with more complicated scrolling behavior, where I can't just use a ScrolledWindow)

您可以添加viewport.set_size_request(x,y),如果表变得大于视口大小,则将激活滚动。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM