Is there a way I can get the dimensions of the contents of a Tk.Text widget. For example with a canvas I can do canvas.bbox('all') to give me the dimensions of the contents. This is crucial to my program so how is this possible with a text widget?
I want to be able to disable a scrollbar when there is not enough contents for the scrollbar to be active.
import tkinter as tk
def getDims():
print('How do I print the dimensions of the text box here')
root=tk.Tk()
text=tk.Text(root)
text.pack(fill='both')
var='How can I find the dimensions of this text inside the tkinter textbox widget in pixels ?'
text.insert('end',var)
b=tk.Button(root,text='Get Dimensions!',command=getDims)
You can use the count method to get the number of pixels between the first and last characters. This method takes two indexes, and then one or more strings representing what you want to count: chars, displaychars, displayindices, displaylines, indices, lines, xpixels, or ypixels. The return value is a tuple containing the count of each requested item.
For example, to count the pixels from the top of the first character to the bottom of the last character you could do this:
ypixels = text.count("1.0", "end", "ypixels")[0]
Another way to determine if you should show the scrollbar is to call the yview method. If it returns (0.0, 1.0), that tells you that the entire text is visible. If any portion is scrolled off of the screen, that function will return something different.
Related
colors = Label(
text="Surprise",
foreground="Bisque",
background="Hot Pink",
width=150,
height=100
)
colors.pack()
Pycharm showing me to remove the attribute height it is not an option
There are a few things to note here. First: in order to make a label, you need to specify which window it should go in, so the first parameter will normally be root. Second: the height option is for the number of lines of text not the number of pixels, this setting you are trying to use will be very very big (same for width). Normally you shouldn't try to set the size of a label in this way because in most cases it will just automatically go to the correct size for the text you put inside of it. Instead, if you want a specific number of pixels, you should create a Frame widget with the specific size and put the Label widget inside of the frame widget
I have a 525x650 window geometry in tkinter. I want to display the list items in multiple spaces such that they cover all the space in width and move to next line as the width space is full. But instead, all items are getting printed on a single line. When I use the code to print each item in a new line, it exceed the height of the main window.
def throat_symp():
#empty_label.config(text=(throat_symplist)) #For printing on same line
empty_label.config(text=("\n".join(throat_symplist))) enter image description here #For printing on seperate line
print(throat_symplist)
You can configure your Label with wraplength=525 to tell it to word-wrap the text at the given width.
Or, instead of a Label, you can use a Text (with state=DISABLED to prevent editing); this will automatically wrap at the widget's width, and is much easier to add scrolling if the list is still too tall to fit in the window.
is there any method to identify row/column in tkinter.Text widget in tkinter? I mean something like methods identify_row(event.y) and identify_column(event.x).
I want to highlight the line bellow cursor and I need it to work with disabled Text widget.
I was thinking of getting the height and count the line number from the current y coord and the height of the line, but I thought, there might be a better way.
So, is there?
Thank you for any advice.
Found it, it works for me:
def __onLClicked(self, event):
linestart = self.index("#{0},{1} linestart".format(event.x, event.y))
lineend = self.index("{} lineend".format(linestart))
self.tag_remove("current_line", 1.0, "end")
self.tag_add("current_line", linestart, lineend)
Only one detail, it unfortunately doesn't higlight all lines the same, because each line has different length. If anyone knows how to highlight full 'width' (the length of the longes line I guess) for every line without adding extra spaces...that'd be great!
So I have a list of strings.
Something like this
Answers=["House - Maja","Boat - Paat","Plane - Lennuk"] etc.
I want to display them in a tkinter window, the list lenght is not a constant. I can't do it with "listbox", because I also want to add buttons there.
EDIT:
I got it!
label = ttk.Label(root, text="\n".join(map(str, yourlist)))
I am trying to create a roguelike using the Text widget.
I have figured out a few things, namely that I can set the size of the widget using width and height options and that I can find the pixel height or width of said widget. However, what I want to do is have the widget resizable (pack(expand="yes", fill="both")) but be able to refresh the displayed text on a resize. Is there a way to get the character dimensions when the widget is running without resorting to winfo_width() and math based on pixel dimensions of characters?
I've run into that exact same problem a couple times jaccarmac, and to my knowledge there is no way to find the width of a string of characters. Really the only way is to use the winfo_ commands: width, height, geometry. However, it kind of sounds like you just want to make sure that all of the text is displayed if you change the label and add more text. If that is the case, you don't have to worry about it. That should all be taken care of by the widgets themselves. If you don't see them expanding to show all of your label, that usually means one of the widgets containing that label is not set to expand (either using expand=YES with .pack, or columnconfigure(i, weight=1) for .grid).
A final thought; in the pack arguments make sure it's YES, and not "yes". That uppercase YES is not a string, but a variable name defined by Tkinter.
There is no way to automatically get the width in characters, but it's easy to calculate, assuming you're using a fixed width font. One way to do this is to use the font_measure method of a font object. Use font_measure to get the width of a '0' (or any other character for that matter; I think tk users zero internally, not that it matters with a fixed width font), then use this in your calculations.
This is an old question, but after doing some research I've found that there actually is a way to get height/width info directly without maths or playing with font widths using the Text widget's cget() method:
text_widget = tk.Text()
width_in_char = text_widget.cget('width')
height_in_char = text_wdiget.cget('height')
Since the Text widget stores its height and width configuration in characters, simply querying for those parameters will give you what you're looking for.