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

boB Stepp robertvstepp at gmail.com
Fri Dec 9 10:58:48 EST 2016

On Thu, Dec 8, 2016 at 12:00 PM, Alan L Dickman <adickman at us.ibm.com> wrote:
> Thanks in advance for any help! I'm just learning Python and Tkinter. Can anyone suggest why this program does not insert 'some text' into text box t1 when the start button is clicked? No errors are thrown when I run the program in PyCharm

I think your issue is that your list "t1" is not doing anything for
you.  Inside your __init__ method, "t1" and "self.t1" are not
referring to the same thing.  Your Text widget is bound to "t1", not
the list.  I am a relative newbie in tkinter, but apparently "0" is
not an allowed index.  "insert" inserts before the given index, which
in your case needs to be "END".

> from tkinter import *
> class Window(Frame):
>    t1 = []

I don't see what this is doing.  It will not be the same as t1 in your
method below.

>    def __init__(self, master=None)
>        Frame.__init__(self, master)
>        self.master = master
>        self.master.title("My Title")
>        StartButton = Button(self, text="Start", command=self.start, bg='#006ba6', fg="white")
>        StartButton.grid(row=0, columnspan=3, sticky=(N, S, W, E))
>        Label(self, text="Your Name").grid(row=1, sticky=(N, S, W, E))
>        t1 = Text(self, height=2)
>        t1.grid(row=1, column=1, sticky=(N, S, W, E))
>        self.t1.insert( 0, 'some text')

This needs to drop the "self" and use an appropriate index.  It should
be:  "t1.insert(END, 'some text')".

>        quitButton = Button(self, text="Quit", command=self.client_quit, bg='#006ba6', fg="white")
>        quitButton.grid(row=5, columnspan=3, sticky=(N, S, W, E))
>        self.grid(sticky=(N, S, W, E))
>    def start(self):
>        self.t1.insert(0, 'some text')
>    def client_quit(self):
>        exit()
> root = Tk()
> root.geometry()
> app = Window(root)
> root.mainloop()



More information about the Tkinter-discuss mailing list