473,605 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter: How to get Label wraplength functionality in Text Box

I've tried quite a few things to get this correct but have hit a
couple of sticking points that I can't figure out. I need to ge the
Text box to function like the 'wraplength' option in a Label.

I've been able to adjust the height of the text by calculating the
number of lines needed to display the text. That's fairly simple. I
know the number of characters in the text and width of the box (which
is static). From that point I can calculate how many lines of display
is needed and resize it.

The problem arises when I use the 'wrap' option of the Text Box so the
words aren't chopped off. Once the wrapping is done there are dead
spaces left at the end of the lines which are ignored when the char
count is done. As a result sometimes the last line is not shown. I can
always just add +1 to the number, but then sometimes I get an empty
line. Space is at a premium in this app, so I have to cut everything
down to use only what's necessary.

So does anyone know how add in those extra spaces to get this to
adjust correctly? (Or if there is another way to get the Text Box to
automatically adjust it's size that I'm not aware of?)

Thanks
Oct 28 '08 #1
4 5569
On 10/28/08, Mudcat <mn******@gmail .comwrote:
I've tried quite a few things to get this correct but have hit a
couple of sticking points that I can't figure out. I need to ge the
Text box to function like the 'wraplength' option in a Label.

I've been able to adjust the height of the text by calculating the
number of lines needed to display the text. That's fairly simple. I
know the number of characters in the text and width of the box (which
is static). From that point I can calculate how many lines of display
is needed and resize it.

The problem arises when I use the 'wrap' option of the Text Box so the
words aren't chopped off. Once the wrapping is done there are dead
spaces left at the end of the lines which are ignored when the char
count is done. As a result sometimes the last line is not shown. I can
always just add +1 to the number, but then sometimes I get an empty
line. Space is at a premium in this app, so I have to cut everything
down to use only what's necessary.

So does anyone know how add in those extra spaces to get this to
adjust correctly? (Or if there is another way to get the Text Box to
automatically adjust it's size that I'm not aware of?)
Are you looking for something like the new "count" command for the
text widget in tk 8.5 ? "count" can count the number of logical lines
(irrespective of wrapping), display lines (counts one for each time a
line wraps) and some other things.

Thanks

--
http://mail.python.org/mailman/listinfo/python-list

--
-- Guilherme H. Polo Goncalves
Oct 28 '08 #2
Sounds like that would work really well. Problem is I can't get it to
work.

....
AttributeError: Text instance has no attribute 'count'
....

I think my usage is correct. I don't have any params at the moment,
but I was just checking the functionality.

numlines = widget.count()

According to the Tk 8.5 documentation it's used just like a normal
command.
WIDGET COMMAND
pathName count ?options? index1 index2
-chars
-displaychars
-displayindices
-displaylines
-indices
-lines
-xpixels
-ypixels
As for the environment, I thought I had everything set up correctly.
I've got the latest stable version of Python 2.6 (r26:66721, Oct 2
2008, 11:35:03). I'm implementing the TTK wrappers to access Tk 8.5.
Although when I check the wrapper I don't see any mods to the Text
Box. I also don't see this option in the Tkinter.py file.

Is there something else I need to add to access this new feature?
On Oct 28, 6:51*pm, "Guilherme Polo" <ggp...@gmail.c omwrote:
>
Are you looking for something like the new "count" command for the
text widget in tk 8.5 ? "count" can count the number of logical lines
(irrespective of wrapping), display lines (counts one for each time a
line wraps) and some other things.
*Thanks
--
*http://mail.python.org/mailman/listinfo/python-list

--
-- Guilherme H. Polo Goncalves
Oct 29 '08 #3
On 10/30/08, Mudcat <mn******@gmail .comwrote:
I'm not sure why my tkinter would not be compiled against 8.5 since I
have the latest version. I assumed that Python 2.6 would have it
without requiring me to do an extra compile.
It is not really python's fault if tkinter is compiled against tcl/tk
8.5 or not. The windows installer for python 2.6 happens to include
tcl/tk 8.5 and tkinter compiled against them, but ubuntu for example
doesn't distribute tkinter compiled against tcl/tk 8.5 at the moment.
>
However I was able to get it working using the code you gave me.
Thanks for that. The only problem is that it seems to simply be
counting newlines (or number of \n). When I use the following:

numlines = widget.count("1 .0", "end", "displaylin es", "lines")
print "Number of lines is ", numlines

I get this:

Number of lines is (153, 1)
The first is the number of displaylines, the second is the number of lines.
>
So that's not actually the number of lines displayed in the box, just
the number of newline chars it finds.
Not really. displaylines returns the number of lines displayed in the
text widget, and lines returns the number of newlines found.
Note that it is important to call "count" only after the text widget
is being displayed, otherwise displaylines won't work correctly (not
with tk 8.5.3 at least).
I couldn't find anything in the
tk documentation that would give me any other options to count lines
differently, or number of lines displayed after wrapping.
Try this and check what you get:
import Tkinter

root = Tkinter.Tk()
text = Tkinter.Text()
text.pack()

def test(event):
print "displaylines:" , text.count("1.0 ", "end", "displaylin es")
print "lines:", text.count("1.0 ", "end", "lines")

text.insert("1. 0", "a" * 81)
text.insert("2. 0", "b\n")
text.bind('<Map >', test)

root.mainloop()
You should have 3 lines displayed but only 2 "real" lines.
--
-- Guilherme H. Polo Goncalves
Oct 30 '08 #4
Awesome...there it goes. I guess my main problem was trying to
evaluate the box before it had been displayed (or all the frame
propagations were finished). The key was getting the <Mapbinding in
there once I got the count functionality to work. After all
that...such a simple function:
def textBoxResize(s elf, event):
widget = event.widget
dispLines = widget.count("1 .0", "end", "displaylin es")
widget.config(h eight=dispLines )
Thanks for the help!
On Oct 30, 9:19*am, "Guilherme Polo" <ggp...@gmail.c omwrote:
On 10/30/08, Mudcat <mnati...@gmail .comwrote:
I'm not sure why my tkinter would not be compiled against 8.5 since I
*have the latest version. I assumed that Python 2.6 would have it
*without requiring me to do an extra compile.

It is not really python's fault if tkinter is compiled against tcl/tk
8.5 or not. The windows installer for python 2.6 happens to include
tcl/tk 8.5 and tkinter compiled against them, but ubuntu for example
doesn't distribute tkinter compiled against tcl/tk 8.5 at the moment.
*However I was able to get it working using the code you gave me.
*Thanks for that. The only problem is that it seems to simply be
*counting newlines (or number of \n). When I use the following:
* * * * numlines = widget.count("1 .0", "end", "displaylines", "lines")
* * * * print "Number of lines is ", numlines
*I get this:
*Number of lines is *(153, 1)

The first is the number of displaylines, the second is the number of lines.
*So that's not actually the number of lines displayed in the box, just
*the number of newline chars it finds.

Not really. displaylines returns the number of lines displayed in the
text widget, and lines returns the number of newlines found.
Note that it is important to call "count" only after the text widget
is being displayed, otherwise displaylines won't work correctly (not
with tk 8.5.3 at least).
I couldn't find anything in the
*tk documentation that would give me any other options to count lines
*differently, or number of lines displayed after wrapping.

Try this and check what you get:

import Tkinter

root = Tkinter.Tk()
text = Tkinter.Text()
text.pack()

def test(event):
* * print "displaylines:" , text.count("1.0 ", "end", "displaylin es")
* * print "lines:", text.count("1.0 ", "end", "lines")

text.insert("1. 0", "a" * 81)
text.insert("2. 0", "b\n")
text.bind('<Map >', test)

root.mainloop()

You should have 3 lines displayed but only 2 "real" lines.

--
-- Guilherme H. Polo Goncalves
Oct 30 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6331
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why is this so? I notice that I can display a GIF image in the central area of a simple menu-bar app as shown below in the first code sample. But, when I set up my app with a class, as shown below in the second code sample, the image disappears. How can I correct this? I'm sure the answer would...
6
17977
by: max(01)* | last post by:
hi people. when i create a widget, such as a toplevel window, and then i destroy it, how can i test that it has been destroyed? the problem is that even after it has been destroyed, the instance still exists and has a tkinter name, so testing for None is not feasible: >>> import Tkinter >>> fin = None >>> fin1 = Tkinter.Toplevel()
5
2046
by: max(01)* | last post by:
hello. the following code: 1 from Tkinter import * 2 3 class MiaApp: 4 def __init__(self, genitore): 5 self.mioGenitore = genitore 6 self.i = IntVar()
5
2993
by: max(01)* | last post by:
hello. i wrote a very simple tkinter demo program that uses menus, buttons, labels, entries, frames and secondary toplevels. it is a python version of a java program made by a colleague. the user can create ("Scrivi") a record with his second name, first name and date of birth, save ("Salva") the record to a file or read in ("Leggi") a previously created file. "Annulla" is to cancel. "Chiudi" is
1
5104
by: vigacmoe | last post by:
Hi all, I'm trying to write a simple tkinter program, then this problem popped up. The followin code will describe the problem. ------------------------------------------ import Tkinter class countdown(Tkinter.Frame):
4
2157
by: MartinRinehart | last post by:
Everything I've read about Tkinter says you create your window and then call its mainloop() method. But that's not really true. This is enough to launch a default window from the console: Google's great, but it has no truth meter. Do I inherit from Frame? Or is that a big mistake. (Both positions repeated frequently.) Do I use Tk() or toplevel()? (Support for both and if a cogent explanation of the differences exists, I didn't find it.)
3
2975
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I guess my question is two-fold. 1) How do I get rid of that window? 2) Any comments in general? I am just learning python (and coding with classes), so I'm sure there are things I should pound into my head before I learn bad habits. Here's the...
1
1690
by: Francesco Bochicchio | last post by:
Il Mon, 18 Aug 2008 12:15:10 +0100, dudeja.rajat ha scritto: Uhm, I don't think you should use the grid manager to obtain a window like that. The grid manager is for equally distributing widgets both horizontally and vertically. And I'm not sure that you can realize that window look with Tkinter. You could get close by horizontally packing each widget row in a frame and then vertically packing the frames in the window. But the look...
1
4890
by: Dude | last post by:
Hi All, I am fairly new to Python programming. I am working on a small Tkinter project and I think I am missing something, maybe you can help. The two lines below is all I have related to the Label widget: lblServer = Tkinter.Label(serverFrame, text='Server:', fg='black', justify='left', relief='groove') lblServer.place_configure(width=200, bordermode='inside')
0
8001
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8424
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6742
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3912
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1537
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1270
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.