tkinter function outout to text widget

eb303 eric.brunel.pragmadev at gmail.com
Mon May 31 05:07:09 EDT 2010


On May 29, 10:51 pm, Johan Lans <johan.l... at 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 -



More information about the Python-list mailing list