Setting the max width of an Entry in pixels

The Entry object has a method for setting the max width in characters. Although this really does produce an Entry which will only hold the given number of characters, it is about twice too wide.

I would like the entry to be physically no larger than what would barely hold the maximum number of characters, or even less.

One way to do this would be to set the physical width to be no more than some maximum number of pixels, although i do not know how to do this. (So this would set two limits on the size of the Entry, with the smaller of the two winning in case of a conflict: the number of characters, and the number of pixels.)

Maybe there are other ways?

Thanks in advance for any info.

1 Like

Hey there ! If I understand your question right, you want to set the width of an entry to a fixed value ?

The first and direct way should be to use width-request property. Did you try it ?
Also I suggest you to give a read of an excellent HIG article Visual Layout. It tells you about effective packing of widgets to make awesome UIs.

1 Like

width-request is about the minimum size. We don’t have a generic way to restrict the maximum size of widgets.


Thanks Arun for your reply, and the link to the visual layout page.

The visual layout page is nice, and if it had software support it would be awesome ---- it would be exactly what i need. (By “software support”, i mean a program that could be used to synthesize the 3rd figure on the page, and then emit the code that would render the second figure on the page. That would be truly awesome, but i’ll bet a cookie that no such program exists.)

In the absence of such a program, i would like to follow the visual layout page manually: I would like to specify the left edge of the Entry is to be at (e.g.) exactly 20 pixels from the page edge, and the right edge at (e.g.) 30 pixels, and that the Entry itself will take a maximum of (say) 7 characters. If for whatever reason 7 characters would not fit, then the Entry would become scrollable or through some other means allow the user to get to the start or end of the text that they want to enter.

As it is now, i’m just living with the gigantic Entries that result even when i want only a few digits — they seem to be nearly twice as large as necessary. It would of course be possible in principle for me to write my own widget, some subclass of DrawingArea, that would accept keydown events and maybe use pango text to draw the text, but that would seem to be a crazy, ambitious, and error-prone project. :slight_smile:

1 Like

Thanks Matthias for the info.

I would humbly and respectfully ask for such a means to be added some day.

If it is, then it would be useful to allow an Entry to contain its maximum amount of characters specified even if the software estimate of the pixel width would not seem to allow it: it is possible that the characters might all have a narrow footprint, for example, and one ought to be able to scroll to the end or beginning of an Entry.

The reason behind this is the theory that its wrong to think about pixels when handling text. Text is made of characters.

1 Like

Thanks Matthias for explaining the reasoning.

I do appreciate the work that the gnome team puts into this, including thinking through what the right API would be.

I would point out though, that if the Entry weren’t so large compared with the characters in it, there would be less motive to want to clip it using pixels :slight_smile: :slight_smile:

Thanks again for the clarification.

Entry size is mostly a theme issue.

GTK4 splits out the actual text editing part of GtkEntry into its own widget, called GtkText, so you can more easily do your single-line editing widgets tailored to your situation.

1 Like

OK, thanks Matthias. I’ll live with the Entry as it is until GtkText comes downstream to me. :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.