1

I'm working through the GTK+3 tutorials, and all the examples I have worked through--as well as code I've written using code as well as glade--produce a window larger than necessary.

Does anyone have a suggestion for what I'm doing wrong, or how to go about fixing it?

Here's a simple example:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title = 'Hello World')

        self.box = Gtk.Box(spacing=6)
        self.add(self.box)

        self.button1 = Gtk.Button(label = 'Hello')
        self.button1.connect('clicked', self.on_button1_clicked)
        self.box.pack_start(self.button1, True, True, 0)

        self.button2 = Gtk.Button(label = 'Goodbye')
        self.button2.connect('clicked', self.on_button2_clicked)
        self.box.pack_start(self.button2, True, True, 0)

    def on_button1_clicked(self, widget):
        print('Hello')

    def on_button2_clicked(self, widget):
        print('Goodbye')

win = MyWindow()
win.connect('delete-event', Gtk.main_quit)
win.show_all()
Gtk.main()

Here is what my code produces: MyCode

And here is what it is supposed to look like according to the tutorial

Tutorial

Ivan Kolesnikov
  • 1,787
  • 1
  • 29
  • 45
user45878
  • 21
  • 5
  • Can you give a pointer to the tutorial? – Willem Van Onsem Aug 06 '17 at 23:07
  • https://python-gtk-3-tutorial.readthedocs.io/en/latest/layout.html#boxes – user45878 Aug 06 '17 at 23:25
  • Also, I should mention, if I manually resize the window to its smallest possible size, it looks just like the example. The same with the rest of the programs I've written. It's just when I type the code as it's presented, the window appears oversized everytime, instead of taking up the least space the contents require. – user45878 Aug 06 '17 at 23:27

2 Answers2

1

I was able to fix this problem by using either:

self.set_default_size(50,25)

or

win.set_default_size(50,25)

Thaks for the help!

user45878
  • 21
  • 5
0

Your window manager is different or has different settings from the tutorial. Anyway I would try this code which sets a size request for the window:

    self.set_size_request(100, 50)
theGtknerd
  • 3,647
  • 1
  • 13
  • 34
  • This doesn't work--I can't set a size that will be smaller than what I get without specifying size. I can make it larger--(500, 500) works just like one would expect. edit: Also I'm using Gnome 3.2 for a window manager, for what that's worth. – user45878 Aug 07 '17 at 20:04
  • How about `self.set_default_width(100)` and `self.set_default_height(50)`? – theGtknerd Aug 07 '17 at 22:01
  • Your two suggestions don't work, but I used ipython to get a dir of the Gtk.Window class and I found the solution (which I feel stupid for not doing in the first place), but the answer is: self.set_default_size(x,y) – user45878 Aug 08 '17 at 10:12