Author Topic: Python error; 'NoneType' object has no attribute 'after'  (Read 2362 times)

0 Members and 1 Guest are viewing this topic.

Offline Vindhyachal.taknikiTopic starter

  • Frequent Contributor
  • **
  • Posts: 488
Python error; 'NoneType' object has no attribute 'after'
« on: July 11, 2015, 04:57:49 pm »
I got error while coding in python.
Need to print same random value again & again.

Code: [Select]
from Tkinter import *
import Tkinter
from random import randint

class App1(Tkinter.Tk):
   
    def __init__(self, master):
        Tkinter.Tk.__init__(self,master)
        self.master = master

        self.label = Label(self , text = "Voltage" , font = ("Helvetica",32))
        self.label.grid(row = 0)

        self.reading_label = Label(self,text = '0.0' , font = ("Helvetica",110))
        self.reading_label.grid(row = 1)
        self.update_reading()


    def update_reading(self):
        value = randint(0,9)
        reading_str = "{:.2f}".format(value)
        self.reading_label.configure(text = reading_str)
        self.master.after(1 , self.update_reading)


root = App1(None)
root.title("Option")
root.geometry("320x240")
root.mainloop()


I gor error message:
Traceback (most recent call last):
File "/voltmeter.py", line 26, in <module>
root = App1(None)
File "/voltmeter.py", line 16, in __init__
self.update_reading()
File "/voltmeter.py", line 23, in update_reading
self.master.after(1 , self.update_reading)
AttributeError: 'NoneType' object has no attribute 'after'
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 12353
  • Country: us
Re: Python error; 'NoneType' object has no attribute 'after'
« Reply #1 on: July 11, 2015, 06:25:00 pm »
    def update_reading(self):
        value = randint(0,9)
        reading_str = "{:.2f}".format(value)
        self.reading_label.configure(text = reading_str)
        self.master.after(1 , self.update_reading)

I gor error message:
Traceback (most recent call last):
File "/voltmeter.py", line 26, in <module>
root = App1(None)
File "/voltmeter.py", line 16, in __init__
self.update_reading()
File "/voltmeter.py", line 23, in update_reading
self.master.after(1 , self.update_reading)
AttributeError: 'NoneType' object has no attribute 'after'

Presumably you have narrowed it down this far?

I don't know enough about what you are doing to say why 'after' is not defined. Where is 'master' defined?
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Python error; 'NoneType' object has no attribute 'after'
« Reply #2 on: July 11, 2015, 07:16:26 pm »
Have you even tried to follow through your code? ???

Code: [Select]
root = App1(None)     # here None is "master"

class App1(Tkinter.Tk):
    def __init__(self, master):    # you passed in None for master here
        self.master = master       # so now self.master is None
        ...
        self.update_reading()

    def update_reading(self):
        ...
        self.master.after(1 , self.update_reading)    # self.master is still None here...

AttributeError: 'NoneType' object has no attribute 'after'

"'NoneType' object" (None) indeed has no attribute 'after'. You can't say None.after()... You might want to try passing in something other than None...
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline bson

  • Supporter
  • ****
  • Posts: 2441
  • Country: us
Re: Python error; 'NoneType' object has no attribute 'after'
« Reply #3 on: July 11, 2015, 07:27:38 pm »
tk=Tkinter.Tk()
root=App1(tk)

See:
http://zetcode.com/gui/tkinter/introduction/

BTW, as a convention, 'root' refers to the root window, which is your display background.  It's what's (conceptually) returned by Tk().  Your app is a child of this, and it would make sense to call it something other than 'root'.  Also, as the examples on the link above show it's generally referred to as an application's parent window, so calling it "parent" is the most straightforward (compared to "master").
« Last Edit: July 11, 2015, 07:36:18 pm by bson »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf