Mihir Gadgil
Mihir Gadgil

Reputation: 105

Size of widget in Gtk.Grid

I'm trying to arrange multiple Gtk.Entry in a 9x9 Gtk.Grid. I have set the size of entries with width_chars=1 (yes, I want them small). The problem is that the grid doesn't respect the entry's size and expands it. I tried it using a Gtk.Box instead of Grid (I couldn't arrange it in 9x9) and it did render the entries of 1 width. Program is in python3. Here is my code, what am I doing wrong?

from gi.repository import Gtk
wind=Gtk.Window(title='My App')
wind.connect('delete-event',Gtk.main_quit)
grid=Gtk.Grid()
wind.add(grid)
s=[[0 for x in range(9)] for x in range(9)]
for i,j in [(x,y) for x in range(9) for y in range(9)]:
     s[i][j]=Gtk.Entry(width_chars=1,xalign=0.5,text='0')
     grid.attach(s[i][j],j,i,1,1)
wind.show_all()
Gtk.main()

This outputs this: Program output

Edit2: After adding this question, I came to know that the grid inherits the 'expand' property from its children. So I have tried setting the halign and hexpand properties of the entries to False, but it still produces the same result.
I also read about Glade and decided to try to create the same layout through it. But it still produces the same output. Is there no way to stop the widgets / grid / window from expanding like that?

Edit3: I was on ubuntu gnome 15.10 when I originally asked and answered this question. Right now I am on regular ubuntu 14.04 and I noticed something interesting. I tried the same program and this time it turned out that I didn't need either of the above mentioned commands to make it work. It would be very helpful if someone explained why does that happen.

Upvotes: 3

Views: 2187

Answers (1)

Mihir Gadgil
Mihir Gadgil

Reputation: 105

The solution turned out to be setting a default size for the window and setting its hexpand value to False.

wind.set_default_size(200,200)
wind.set_hexpand(False)

Upvotes: 1

Related Questions