7

I'm trying to create a non-resizable dialog with a label in it. This label has a lot of text, so I want it to wrap without making the dialog ridiculously wide.

For some reason, I can't find out what it takes to get GTK to allow this to happen. I can't even find a way of setting a max-width on the dialog, which would be great.

Here's an running example of what I mean:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from gi.repository import Gtk

class DialogExample(Gtk.Dialog):

    def __init__(self, parent):
        Gtk.Dialog.__init__(self, "My Dialog", parent, 0,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OK, Gtk.ResponseType.OK))

        self.set_default_size(150, 100)
        self.set_resizable(False)

        label = Gtk.Label("This is a dialog to display additional information, with a bunch of text in it just to make sure it will wrap enough for demonstration purposes")
        label.set_line_wrap(True)

        box = self.get_content_area()
        box.add(label)
        self.show_all()

class DialogWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Dialog Example")

        self.set_default_size(250, 200)


        button = Gtk.Button("Open dialog")
        button.connect("clicked", self.on_button_clicked)

        self.add(button)

    def on_button_clicked(self, widget):
        dialog = DialogExample(self)
        response = dialog.run()

        if response == Gtk.ResponseType.OK:
            print "The OK button was clicked"
        elif response == Gtk.ResponseType.CANCEL:
            print "The Cancel button was clicked"

        dialog.destroy()

win = DialogWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Dan
  • 3,665
  • 1
  • 31
  • 39

2 Answers2

10

I solved this (besides setting the line wrap to True) putting the Gtk.Label inside a Gtk.Table, using the FILL and SHRINK flags and setting a fixed width for the label. Something like this:

label = Gtk.Label("This is a dialog to display additional information, with a bunch of text in it just to make sure it will wrap enough for demonstration purposes")
label.set_line_wrap(True)
label.set_size_request(250, -1) # 250 or whatever width you want. -1 to keep height automatic

table = Gtk.Table(1, 1, False)
table.attach(label, 0, 1, 0, 1, Gtk.AttachOptions.SHRINK | Gtk.AttachOptions.FILL)

That should do the trick

satanas
  • 706
  • 7
  • 11
  • 1
    Digging up a little bit I realized what is the problem. When you create the window/dialog and this hasn't been showed by the first time you label will not have a parent size reference, so Gtk assign as much space as it can to the label and then set the parent width, resulting in a huge window. To avoid this behavior, set the preferred width for the parent and do a show, this way Gtk computes the parents geometry and label will have a real reference of the parent's size. I did it this way and now everything is working like a charm. – satanas Dec 25 '12 at 21:48
  • 1
    hi @satanas - any chance you can describe a bit further how you "set the preferred width for the parent and do a show"? I've got the same issue; the Gtk.Table option is deprecated since v3.4 so I'm looking for a non-deprecated solution. TIA – fossfreedom May 09 '15 at 10:27
1

Since 2.6 you can use: label.set_max_width_chars(50)

This is not depreciated at this time. :)

See here: https://developer.gnome.org/Labels/

Technoloft
  • 622
  • 4
  • 18