2
votes

I am trying to put an entry widget with a label in one row using a horizontal box. However, the label expands vertically. It takes the initial height of the window and does not allow to shrink below it. I have tried all possible combinations of fill, expand, alignment etc. but it does not change.

How to fix it?

Expanded label

#!/usr/bin/env python3

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

class MyWindow(Gtk.Window):

    def __init__(self):
        super(MyWindow, self).__init__()

        self.init_ui()

    def init_ui(self):    


        hbox = Gtk.Box(Gtk.Orientation.HORIZONTAL, 6)
        hbox.set_homogeneous(True)
        hbox.set_baseline_position(Gtk.BaselinePosition.CENTER)

        entry = Gtk.Entry()
        entry.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(6400, 6400, 6440))
        entry.connect("key-release-event", self.on_key_release)

        hbox.pack_start(entry, False, False, 10)

        self.label = Gtk.Label("Hello")
        self.label.set_width_chars(15)
        self.label.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(40000, 40000, 40000))
        hbox.pack_start(self.label, False, False, 10)

        self.add(hbox)

        self.set_title("Entry")
        self.set_size_request(350, 250)

        self.connect("destroy", Gtk.main_quit)
        self.show_all()

    def on_key_release(self, widget, event):
        self.label.set_text(widget.get_text())


win = MyWindow()
Gtk.main()
1

1 Answers

2
votes

Add another vertical box before the horizontal box:

#!/usr/bin/env python3

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

class MyWindow(Gtk.Window):

    def __init__(self):
        super(MyWindow, self).__init__()

        self.init_ui()

    def init_ui(self):    



        hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL, spacing = 6)
        hbox.set_homogeneous(True)
        hbox.set_baseline_position(Gtk.BaselinePosition.CENTER)

        entry = Gtk.Entry()
        entry.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(6400, 6400, 6440))
        entry.connect("key-release-event", self.on_key_release)

        hbox.pack_start(entry, False, False, 10)

        self.label = Gtk.Label("Hello")
        self.label.set_width_chars(15)
        self.label.modify_bg(Gtk.StateType.NORMAL, Gdk.Color(40000, 40000, 40000))
        hbox.pack_start(self.label, False, False, 10)

        vbox = Gtk.Box(orientation = Gtk.Orientation.VERTICAL)
        vbox.pack_start(hbox, False, False, 0)
        self.add(vbox)

        self.set_title("Entry")
        self.set_size_request(350, 250)

        self.connect("destroy", Gtk.main_quit)
        self.show_all()

    def on_key_release(self, widget, event):
        self.label.set_text(widget.get_text())


win = MyWindow()
Gtk.main()

As a side note, using non-keyword arguments does not always work properly in Python3 and Gtk, instead of hbox = Gtk.Box(Gtk.Orientation.HORIZONTAL, 6), use hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL, spacing = 6)