Why does set_position fail in this code

251 views Asked by At

When I build this same ui in Glade (the program) the resulting code is flawless, when I code it by hand everything works except one thing, the set_position(670) is ignored and the resulting window comes up at about 1" square on my screen.

My goal is to get the left hand pane to instanciate at 670 px wide. I am not concerned about locking the width.

I have tried placing the call to set_position(670):

  • where it is shown now
  • 2 lines further down
  • After the self.top.show_all()
  • Just before the self.top.show_all()
  • Just after call to pack1

Searching google has lead to no usefull results, and there are no useful answers here that I have found so far.

The pyGtk site indicates that the call can be placed almost anywhere. The gtk site lists no restrictions. Most things I have found seem to indicate it is due to the resize/shrink attributes of one of the other widgets, but after numerous attempts at altering the values of everything in the left pane, I am still at a loss. (Note that value changes were tried one or two at a time.)

import gtk
import gobject


class Source(gobject.GObject):

    def _emit_signal(self, button, value):
        self.emit('choice_changed', self, value)

    def __init__(self):
        self.__gobject_init__()
        # packing treee
        # top
        #   main
        #     inst_align
        #       inst_window
        #         instructions
        #     opt_align
        #       opt_buttons
        #         opt_0 (through opt_5)
        #         doit_box
        #           doit
        self.top = gtk.Alignment(0.0, 0.0, 1.0, 1.0)
        self.main = gtk.HPaned()
        self.main.set_position(670)
        self.top.add(self.main)
        # Build left side components
        self.inst_align = gtk.Alignment(0.0, 0.0, 1.0, 1.0)
        self.inst_window = gtk.ScrolledWindow(None, None)
        self.inst_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.instructions = gtk.TextView(None)
        self.instructions.get_buffer().set_text(
            "Very long (1200 char) string snipped.")
        #pack pane 1
        self.inst_window.add(self.instructions)
        self.inst_align.add(self.inst_window)
        self.main.pack1(self.inst_align, True, False)
        # build right side components
        self.opt_align = gtk.Alignment(1.0, 0.0, 0.0, 0.0)
        self.opt_buttons = gtk.VButtonBox()
        self.opt_buttons.set_layout(gtk.BUTTONBOX_START)
        self.opt_buttons.set_homogeneous(True)
        self.opt_0 = gtk.RadioButton(
            None,
            "Label 0",
            True)
        self.opt_1 = gtk.RadioButton(
            self.opt_0,
            "Label 1",
            True)
        self.opt_2 = gtk.RadioButton(
            self.opt_0,
            "Label 2",
            True)
        self.opt_3 = gtk.RadioButton(
            self.opt_0,
            "label 3",
            True)
        self.opt_4 = gtk.RadioButton(
            self.opt_0,
            "Label 4",
            True)
        self.opt_5 = gtk.RadioButton(
            self.opt_0,
            "Label 5",
            True)
        self.doit_box = gtk.HButtonBox()
        self.doit = gtk.Button("Do It !", None, True)
        self.doit.expand = False
        self.doit.fill = False
        self.doit.set_alignment(0.5, 0.0)
        # Put the buttons in the box and while were at it, bind their
        # released, and pressed options
        for i in range(0, 6):
            s = "self.opt_" + str(i)
            self.opt_buttons.pack_start( eval(s), True, False, 0 )
            s1 = s + '.connect("released", self._emit_signal, i)'
            s2 = s + '.connect("pressed", self._emit_signal, i)'
            exec(s1)
            exec(s2)

        self.doit_box.pack_start(self.doit, True, False, 0)
        self.opt_buttons.pack_start(self.doit_box, True, False, 0)
        self.opt_align.add(self.opt_buttons)
        self.main.pack2(self.opt_align, True, False)

        self.top.show_all()




gobject.type_register(Source)
gobject.signal_new('choice_changed',
                   Source,
                   gobject.SIGNAL_RUN_FIRST,
                   gobject.TYPE_NONE, (Source, gobject.TYPE_INT))


if __name__ == "__main__":
    def cb_test(widget, event, value):
        print "widget is", widget
        print "Value is ", value
        return

    test = Source()
    win = gtk.Window()
    win.add(test.top)
    win.show_all()
    test.connect('choice_changed', cb_test)
    gtk.main()
1

There are 1 answers

0
elya5 On BEST ANSWER

You have to set a window with greater than the position of the HPane divider. Here is a stripped down example:

import gtk

class Source(gtk.Window):

    def __init__(self):
        super(Source, self).__init__()
        label1 = gtk.Label("The first label")
        label2 = gtk.Label("The second label")

        paned = gtk.HPaned()
        paned.add1(label1)
        paned.add2(label2)
        paned.set_position(600)

        self.add(paned)
        self.set_size_request(800, 400)  # needs sufficient width here
        self.connect('delete-event', gtk.main_quit)
        self.show_all()


if __name__ == "__main__":
    test = Source()
    gtk.main()