Pergunta

I am using Tkinter to create my GUI in python and I have run in to a resizing issue. I am using the grid layout mechanism, and my width of my text widgets are set to 100, is there any way to calculate the number of characters that can fit across the screen? So that I can set the width to this number on start up and "stretch" my text element to this number. Is there some known number where a character is like 10px across then we could just divide the width of the screen resolution by 10 and thats the number that will fit across the screen?

Foi útil?

Solução

Strictly, you should try to make your GUI adapt to the environment instead of the other way round. Thus, you should make your text widgets either have appropriate wrapping (character-wrap or word-wrap) or turn off wrapping and put a horizontal scrollbar on them. With that done, you can then maximise the overall window and things will (well, should; it depends on getting the layout instructions right) just adapt.

But if you want to do it the other way, get the font that you want to do the measuring with (because all fonts are different, and the font size matters a lot obviously) and use its measure method to work out just how wide things are. Ideally you'll measure the real text (this matters a lot with a variable width font) but if you don't know it, you can instead make a string of repeated 0 characters and measure that. (I suggest 0 as that's the “default” character that Tk uses internally in situations where it doesn't have a better choice available, and it's between i and M in width in most fonts; you might as well use the same idea.)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top