vindhyachal
Posts: 49
Joined: Mon Jun 22, 2015 6:35 pm

Python error; 'NoneType' object has no attribute 'after'

Sat Jul 11, 2015 4:36 pm

I got error while coding in python.
Need to print same random value again & again.

Code: Select all

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 got 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'

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Python error; 'NoneType' object has no attribute 'after'

Sat Jul 11, 2015 5:13 pm

try changing

Code: Select all

root = App1(None)
to

Code: Select all

root = App1()
You are passing None to the __init__ function as master.

Return to “Beginners”