P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

TkInter Text.insert

Fri Mar 10, 2017 7:12 pm

Hi folks - hoping this is a simple one.

I'm trying to draw a text box on the screen which I can use to display variable data. I've got:

Code: Select all

import tkinter
from tkinter import * (not sure why I need both these lines ...)
.
.
window=tkinter.Tk()
.
.
T=Text(window, height=2, width=20).grid(ow=6, column=6)
T.insert(END,"Hello")

I'll be honest and say I don't really know what I'm doing - these are snippets of code I've got from online tutorials which appear to do what I want.

However, at the very last line I get an error: 'NoneType' object has no attribute 'insert'.

Anyone know what's going on?

Thanks...

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: TkInter Text.insert

Fri Mar 10, 2017 7:23 pm

Apple say... Monkey do !!

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: TkInter Text.insert

Fri Mar 10, 2017 7:30 pm

Funny old thing, that's the one I copied.
That's why I don't understand what's wrong with the T.insert...

Thanks for replying

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: TkInter Text.insert

Fri Mar 10, 2017 9:03 pm

Try this.

Code: Select all

import tkinter
from tkinter import * (not sure why I need both these lines ...)
.
.
window=tkinter.Tk()
.
.
T=Text(window, height=2, width=20)
T.grid(row=6, column=6)
T.insert(END,"Hello")
I've moved the creation of the text widget and placing it in the window using grid in to two separate lines.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: TkInter Text.insert

Sat Mar 11, 2017 12:43 pm

Thanks all,
Breaking it over multiple lines did the trick. I don't know why, but it did ...

The missing 'r' is a typo as I copied the code from a VNC window into the main computer :-)

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: TkInter Text.insert

Sun Mar 12, 2017 9:33 pm

Consider the following example

Code: Select all

T = Button().grid()
This creates an instance of a button and calls it's .grid method and assigns the value returned by grid to T. The grid method return None. So T is none.

Now consider the next example.

Code: Select all

T = Button()
T.grid()
This creates an instance of a button and returns the object to be stored by T.
The next line calls the grid method of object T.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”