tkinter function outout to text widget

J

Johan Lans

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.
 
A

Alf P. Steinbach

* 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
 
E

eb303

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 -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top