1

I was wondering how do the geometry() function values in tkinter come to play with the height value of for example a scrolled text? How can I convert the units to work with each other?

For example: If I want to open a scrolled text box underneath my tkinter window with a click of a button, I know I need then to change my window's geometry() (height) value, but I don't know by how much exactly.

(In the following example I randomly added 100 to the geometry value in the function open. But I want a more specific value that translates to the 7 of the height value of the scrolled text)

from tkinter import *
from tkinter import scrolledtext

def open():
    root.geometry(f'{frame_width}x{frame_height+100}')
    st.pack(side="bottom")

frame_width = 900
frame_height = 500

root = Tk()
root.geometry(f'{frame_width}x{frame_height}')
root.configure(bg='white')
root.resizable(False, False)

open_st = Button(root, text="OPEN SCROLLED TEXT", command= open)
open_st.pack()

st = scrolledtext.ScrolledText(root, width=frame_width, height=7)

root.mainloop()
queuetzi
  • 61
  • 5
  • Please include a [mre] to your question and state clearly what you have tried and in which form it has failed. – Thingamabobs Nov 05 '22 at 14:31
  • @Thingamabobs I added an example. It works, nothing failed, I just would like to understand how do the values of the different functions (geometry() and ScrolledText()) play with each other – queuetzi Nov 05 '22 at 14:42
  • @queuetzi I still don't know what you are trying to do and what your actual question is. Currently I could interpret your question in very different ways and so would my answer might differ drastically from your actual question. – Thingamabobs Nov 05 '22 at 14:46
  • @Thingamabobs my question is: it's very obvious that the 100 does not equal 7.. However on the screen height-wise they seem kind of the same, so obviously the function geometry() takes a different value unit than the function ScrolledText()! How do I convert the units into each other? – queuetzi Nov 05 '22 at 14:52
  • Got you, I'll provide an answer for you. – Thingamabobs Nov 05 '22 at 14:52
  • For a text box, the width and height are in characters and lines correspondingly, whereas those for `geometry()` are in pixels. Converting 7 lines to pixels depends on many parameters like font size and space between lines. More easy way is just get the *height* using `.winfo_height()` which returns the height of a widget in pixels. – acw1668 Nov 05 '22 at 15:12

1 Answers1

1

Widgets that contains text usually provide their width and height in character space. This means tkinter takes the average width and height of your font and multiplies it with the given number in width and height.

There are mainly two ways to deal with that, either tk.Font.measure or metrics, if you want to convert characters to pixel or the much more comfortable way by just asking the widget for it's size via winfo. Happily your case fits for the latter.

The alternate code would looks like this:

def open_text():
    st.pack(side="bottom")
    width, window_height = root.winfo_width(), root.winfo_height()
    requested_text_height = st.winfo_reqheight()
    new_height = window_height + requested_text_height
    root.geometry(f'{width}x{new_height}')

Please note that I have named your function differently. Cause you redefined open in the global namespace of your module and this could lead to unintended behavior. In addition I wonder why you want to do it like this and not just let the geometry managers do their job?

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • Thank you I'll try this. My Idea is that I want to add an Info box at the bottom of my window, that the user can choose if they want it to be opened or closed. If I just leave it for the geometry managers, the info box will clash with my other frames. So my solution is to resize the height of the window to make it look like as if "the info box was added bellow the window" and not on it, if that makes sense! – queuetzi Nov 05 '22 at 15:35
  • @queuetzi got you. I just wasn't sure if you try to reinvent the wheel for your application. Let me know, if something is unclear or produces not the intended result. Otherwise, consider to accept this answer in return. – Thingamabobs Nov 05 '22 at 15:39