Search code examples
python-3.xgtk

How force the widget's size in a box with Gtk 3


I'm making a program that is supposed to have a principal textview (see textview) where the user is supposed to write and a console, where the program is supposed to return the errors messages. So the console should be a little smaller than the text editor.
I also have a toolbar on the top of the window for the user to access the tools of my program and for editing the text.
To do that, I created a global box, where I put the tool bar, the textview and the console (which is also a textview object) using the pack_start attribute (self.global_box.pack_start(self.console)))

The toolbar is correctly placed (I put a button so it can have a length, and it works), but the rest of the space is homogeneously taken by the console and the textview, whereas I would like the console to take only a little space of my window.
How can I obligate my console to take only, for example, 20 pixels of my window? or to set a percentage of my window only for the textview?

here is a screenshot of the window, so you can have an idea of what I'm talking about.

EDIT: The question is similar but its the solution didn't work for me. Here is the code:

class Window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self,title='Text editor')
        self.maximize()
        self.global_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.add(self.global_box)

        self.create_textview()
        self.create_console()

    def create_textview(self):
        self.scrolled_window = Gtk.ScrolledWindow()
        self.scrolled_window.set_hexpand(True)
        self.scrolled_window.set_vexpand(True)
        self.global_box.pack_start(self.scrolled_window, True, True, 0)

        self.textview = Gtk.TextView()
        self.textbuffer = self.textview.get_buffer()
        self.scrolled_window.add(self.textview)
        self.tag_found = self.textbuffer.create_tag('found',background='yellow')

    def create_console(self):
        self.console_scrolled_window = Gtk.ScrolledWindow()
        self.console_scrolled_window.set_hexpand(True)
        self.console_scrolled_window.set_vexpand(True)
        self.global_box.pack_start(self.console_scrolled_window, False, False, 0)
        # ^ HERE the first one is expand argument, second one is irrelevant (the fill one)

        self.console = Gtk.TextView()
        self.console_scrolled_window.add(self.console)
        self.console.set_editable(False)

The problem is on the line where there is the comment HERE.


Solution

  • You are setting set_hexpand and set_vexpand to True, which means they will expand. Remove those, and it will work properly.