[PYTHON] Coordinates of the right end of Label made with tkinter

I want to know the coordinates of the right end of Label made with tkinter.

Please refer to [tkinter] Try using Label etc. for the basic part such as how to arrange Frame and Label.

(This article is a part of Running Label with tkinter [Python]. I have divided it into separate articles for easy search.)

problem


txt=Labeltext
label=ttk.Label(master=root,text=txt,font=("Meiryo",fontsize))
label.place(x=xx,y=yy)

Then, the left end of label is Mochimon xx. Then, what are the coordinates of the right end of the Label display?

answer

label.winfo_reqwidth()+xx

Commentary (useless story)

At first I thought that len (txt) * fontsize (fontsize is in pixels px) could be used. However, with the program introduced in Running Label with tkinter [Python], it wraps before the Label goes to the right end, depending on the character string. I have. missrei.gif

This is due to the fact that it is len (txt). For this implementation iiiii AAAAA Is treated as the same length.

That's where __winfo_reqwidth () __ comes in. According to https://effbot.org/tkinterbook/widget.htm

Returns the “natural” width for this widget. The natural size is the minimal size needed to display the widget’s contents, including padding, borders, etc. This size is calculated by the widget itself, based on the given options. The actual widget size is then determined by the widget’s geometry manager, based on this value, the size of the widget’s master, and the options given to the geometry manager.

In short, it returns the minimum size required to display the specified object (Label in this case) on the Frame. By adding the leftmost coordinate xx of label to this, you can get the rightmost coordinate of the current label. seikourei.gif I was able to judge the right end successfully.


By the way, you can get the height with `winfo_reqheight ()`. That is,

label=ttk.Label(master=root,text=txt,font=("Meiryo",fontsize),foreground="red",background="green")
print(label.winfo_reqwidth())
>>125
print(label.winfo_reqheight())
>>45
txt="Labeltext\nLabeltext"#When displaying over two lines
label=ttk.Label(master=root,text=txt,font=("Meiryo",fontsize),foreground="red",background="green")
print(label.winfo_reqwidth())
>>125#The width does not change
print(label.winfo_reqheight())
>>86#Since it became two lines, it increased. However, ≠ 45*Note that it is 2.

Recommended Posts

Coordinates of the right end of Label made with tkinter
I made you to express the end of the IP address with L Chika
Run Label with tkinter [Python]
Tank game made with python About the behavior of tanks
Life game with Python [I made it] (on the terminal & Tkinter)
Read the coordinates of the plot on the graph with Python-matplotlib (super beginner)
Difference in behavior of transparent Frame made with tkinter in pyinstaller [Python]
I made a program that automatically calculates the zodiac with tkinter
How to crop the lower right part of the image with Python OpenCV
Read the graph image with OpenCV and get the coordinates of the final point of the graph
Try to react only the carbon at the end of the chain with SMARTS
I made a GAN with Keras, so I made a video of the learning process.
I made a mistake in fetching the hierarchy with MultiIndex of pandas
python> print> Redirected only at the end of processing?> Run with -u
Align the size of the colorbar with matplotlib
Check the existence of the file with python
The third night of the loop with for
The second night of the loop with for
Python Basic Course (at the end of 15)
Count the number of characters with echo
Automation of creation of working hours table created at the end of the month with Selenium
Specify the start and end positions of files to be included with qiitap
With LINEBot, I made an app that informs me of the "bus time"
An introduction to cross-platform GUI software made with Python / Tkinter! (And many Try and Error)! (In the middle of writing)
The story of doing deep learning with TPU
About left justification and right justification of Kivy Label
Prepare the execution environment of Python3 with Docker
2016 The University of Tokyo Mathematics Solved with Python
[Python] Get / edit the scale label of the figure
[Note] Export the html of the site with python.
See the behavior of drunkenness with reinforcement learning
GUI image cropping tool made with Python + Tkinter
Increase the font size of the graph with matplotlib
Calculate the total number of combinations with python
Check the date of the flag duty with Python
[Python] I made a Youtube Downloader with Tkinter.
Eliminate the inconveniences of QDock Widget with PySide
Challenge the Tower of Hanoi with recursion + stack
Send Gmail at the end of the process [Python]
Rewrite the name of the namespaced tag with lxml
Fill the browser with the width of Jupyter Notebook
Predict the second round of summer 2016 with scikit-learn
Dump the contents of redis db with lua
Tucker decomposition of the hay process with HOOI
Find out the day of the week with datetime
The basis of graph theory with matplotlib animation
Visualize the behavior of the sorting algorithm with matplotlib
Convert the character code of the file with Python3
Remove specific strings at the end of python
I tried playing with the calculator on tkinter
[Python] Determine the type of iris with SVM
I made a twitter app that decodes the characters of Pricone with heroku (failure)
How to get started with Visual Studio Online ~ The end of the environment construction era ~