From: Johan Lans on
Hi
I'm totally new on python and I'm doing an assignement where I'm doing
a class that manipulates a text. The program is also supposed to have
a GUI, for which I have used tkinter.
So far I have entry widgets for file names and buttons, its all
working like I want it to.
What is missing is a way to output the changes to the text. I was
thinking that a text-widget would be suitable. Is there a reasonably
easy way to do this?
I tried inserting a string to the textwidget and letting the class
method change this string, but the inserted string isn't updated in
the text-widget.
Would be very happy for any hints.
From: Alf P. Steinbach on
* Johan Lans, on 29.05.2010 22:51:
> Hi
> I'm totally new on python and I'm doing an assignement where I'm doing
> a class that manipulates a text. The program is also supposed to have
> a GUI, for which I have used tkinter.
> So far I have entry widgets for file names and buttons, its all
> working like I want it to.
> What is missing is a way to output the changes to the text. I was
> thinking that a text-widget would be suitable. Is there a reasonably
> easy way to do this?
> I tried inserting a string to the textwidget and letting the class
> method change this string, but the inserted string isn't updated in
> the text-widget.

If that is a direct Python string, then you're not changing the string. Python
strings are immutable. So, then you're at most changing which string a variable
or attribute is referring to.

However, if it is some Tkinter thing (I seem to recall that Tkinter offers some
automatic update magic via something-something), then I don't know.


> Would be very happy for any hints.

Just update the widget whenever you change the text.


Cheers & hth.,

- Alf


--
blog at <url: http://alfps.wordpress.com>
From: eb303 on
On May 29, 10:51 pm, Johan Lans <johan.l...(a)apspektakel.com> wrote:
> Hi
> I'm totally new on python and I'm doing an assignement where I'm doing
> a class that manipulates a text. The program is also supposed to have
> a GUI, for which I have used tkinter.
> So far I have entry widgets for file names and buttons, its all
> working like I want it to.
> What is missing is a way to output the changes to the text. I was
> thinking that a text-widget would be suitable. Is there a reasonably
> easy way to do this?
> I tried inserting a string to the textwidget and letting the class
> method change this string, but the inserted string isn't updated in
> the text-widget.
> Would be very happy for any hints.

You won't be able to do exactly what you want with the text widget.
There is a possibility to have to auto-updating in the GUI indeed, but
it can only be made via other widgets than the text.

Here is an example of what you can do:
--------
from Tkinter import *
root = Tk()
var = StringVar()
var.set('aaa')
lbl = Label(root, textvariable=var)
lbl.pack(side=LEFT)
def next():
var.set(''.join(chr(ord(c) + 1) for c in var.get()))
Button(root, text='Next', command=next).pack()
root.mainloop()
--------

As you can see, I'm using a Label here. This should be enough if the
text you want to display is read-only. The Label will also adapt its
size if there are several lines in your text. The biggest limitation
is probably that you can't make it scrollable (not easily, at least…).

HTH
- Eric -