[Tkinter-discuss] Learning Python/Tkinter and struggling with the text object

Michael Lange klappnase at web.de
Fri Dec 9 13:25:12 EST 2016


Hi,

On Fri, 9 Dec 2016 10:50:45 -0700
"Alan L Dickman" <adickman at us.ibm.com> wrote:

> Michael - This is just one example. I've tried so many version of this 
> program. Some run but don't update the t1 text window. Others throw an 
> error trying to access t1. For example, the version below throws an
> error at t1.grid(). It seems like there must be a simple answer but I
> have found no good examples on the internet. 
> 
> What would you change so that text is inserted into t1 when the start 
> button is clicked?
> 
> from tkinter import *
> 
> class Window(Frame):
>     t1 = Text()

the way you initialize the Text widget here effectiveley makes it into
self.t1 , so you will get an error when you try to grid() the widget,
because there is no t1. You will get the same error when the start()
function is called.
To make sure the code works as expected, you need to take care that the
widget is properly referenced, so it can be accessed later. The way to go
is something like:

     def __init__(self, master=None):
         Frame.__init__(self, master)
         self.t1 = Text()
         self.t1.grid()
         (...)

     def start(self):
         print('I am here')
         self.t1.insert(END, 'some text')


Best regards

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

"Can you imagine how life could be improved if we could do away with
jealousy, greed, hate ..."

"It can also be improved by eliminating love, tenderness, sentiment --
the other side of the coin"
		-- Dr. Roger Corby and Kirk, "What are Little Girls Made
Of?", stardate 2712.4


More information about the Tkinter-discuss mailing list