Measure the height of a string in Tkinter Python?

2024/5/20 11:24:06

I need the height in pixel of a string in a Tkiner widget. It is the text in a row of a Listbox.

I know I can measure the width of a string with tkinter.font.Font.measure. But how can I get the height? A similar question thematised only the width but not the height.

Answer
tkf.Font(font=widget['font']).metrics('linespace')

gives the height in pixels of a given widget's font:

try:                        # In order to be able to import tkinter forimport tkinter as tk    # either in python 2 or in python 3import tkinter.font as tkf
except ImportError:import Tkinter as tkimport tkFont as tkfif __name__ == '__main__':root = tk.Tk()widget = tk.Label(root, text="My String")widget.pack()print(tkf.Font(font=widget['font']).metrics('linespace'))tk.mainloop()

Also note that the height of a one line string isn't dependent on its length, but only on its font in a particular environment.

https://en.xdnf.cn/q/73122.html

Related Q&A

pandas filter by multiple columns NULL

I have a pandas dataframe like:df = pd.DataFrame({Last_Name: [Smith, None, Brown], First_Name: [John, None, Bill],Age: [35, 45, None]})And could manually filter it using:df[df.Last_Name.isnull() & …

Closed IPython Notebook that was running code

How it works? I got some code running in an IPython Notebook. Some iterative work. Accidentally I closed the browser with the running Notebook, but going back to the IPython Dashboard I see that this …

os.popen().read() - charmap decoding error

I have already read UnicodeDecodeError: charmap codec cant decode byte X in position Y: character maps to <undefined>. While the error message is similar, the code is completely different, becau…

multiprocessing numpy not defined error

I am using the following test code:from pathos.multiprocessing import ProcessingPool as Pool import numpydef foo(obj1, obj2):a = obj1**2b = numpy.asarray(range(1,5))return obj1, bif __name__ == __main_…

Convert mp4 sound to text in python

I want to convert a sound recording from Facebook Messenger to text. Here is an example of an .mp4 file send using Facebooks API: https://cdn.fbsbx.com/v/t59.3654-21/15720510_10211855778255994_5430581…

pandas map one column to the combination of two columns

I am working with a DataFrame which looks like thisList Numb Name 1 1 one 1 2 two 2 3 three 4 4 four 3 5 fiveand I am trying to compute…

Pyspark command not recognised

I have anaconda installed and also I have downloaded Spark 1.6.2. I am using the following instructions from this answer to configure spark for Jupyter enter link description hereI have downloaded and …

Sliding window in Python for GLCM calculation

I am trying to do texture analysis in a satellite imagery using GLCM algorithm. The scikit-image documentation is very helpful on that but for GLCM calculation we need a window size looping over the im…

Keras multi-label image classification with F1-score

I am working on a multi-label image classification problem with the evaluation being conducted in terms of F1-score between system predicted and ground truth labels.Given that, should I use loss="…

Thread safe locale techniques

Were currently writing a web application based on a threaded python web server framework (cherrypy) and would like to simultaneously support users from multiple locales.The locale module doesnt appear …