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

Discussion in 'Programmer's Corner' started by Vindhyachal Takniki, Jul 11, 2015.

  1. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    348
    6
    I got error while coding in python.
    Need to print same random value again & again.

    Code (Text):
    1. from Tkinter import *
    2. import Tkinter
    3. from random import randint
    4.  
    5. class App1(Tkinter.Tk):
    6.    
    7.     def __init__(self, master):
    8.         Tkinter.Tk.__init__(self,master)
    9.         self.master = master
    10.  
    11.         self.label = Label(self , text = "Voltage" , font = ("Helvetica",32))
    12.         self.label.grid(row = 0)
    13.  
    14.         self.reading_label = Label(self,text = '0.0' , font = ("Helvetica",110))
    15.         self.reading_label.grid(row = 1)
    16.         self.update_reading()
    17.  
    18.  
    19.     def update_reading(self):
    20.         value = randint(0,9)
    21.         reading_str = "{:.2f}".format(value)
    22.         self.reading_label.configure(text = reading_str)
    23.         self.master.after(1 , self.update_reading)
    24.  
    25.  
    26. root = App1(None)
    27. root.title("Option")
    28. root.geometry("320x240")
    29. root.mainloop()
    30.  

    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'
     
  2. tjohnson

    Active Member

    Dec 23, 2014
    614
    121
    Here is an abbreviated version of your code:
    Code (Python):
    1. class App1(Tkinter.Tk):
    2.     def __init__(self, master):
    3.         Tkinter.Tk.__init__(self,master)
    4.         self.master = master
    5.  
    6. root = App1(None)
    You are passing the value None to the __init__ method of class App1. So in line 9 of your code:
    self.master = master

    you are defining self.master as None, which is why you can't call self.master.after in your update_reading() function.
     
  3. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    348
    6
    sry I am starter in python.
    I am trying to figure out how to correct it. Can u tell me how to correct it
     
  4. tjohnson

    Active Member

    Dec 23, 2014
    614
    121
    Try changing your code to:
    Code (Python):
    1. from Tkinter import *
    2. from random import randint
    3.  
    4. class App1(Frame):
    5.  
    6.     def __init__(self, master):
    7.         Frame.__init__(self,master)
    8.         self.master = master
    9.  
    10.         self.label = Label(self , text = "Voltage" , font = ("Helvetica",32))
    11.         self.label.grid(row = 0)
    12.  
    13.         self.reading_label = Label(self,text = '0.0' , font = ("Helvetica",110))
    14.         self.reading_label.grid(row = 1)
    15.         self.update_reading()
    16.  
    17.  
    18.     def update_reading(self):
    19.         value = randint(0,9)
    20.         reading_str = "{:.2f}".format(value)
    21.         self.reading_label.configure(text = reading_str)
    22.         root.after(1 , self.update_reading)
    23.  
    24.  
    25. root = Tk()
    26. app = App1(root)
    27. app.title("Option")
    28. app.geometry("320x240")
    29. app.mainloop()
    I haven't tested this, but I think it should work.
     
  5. Vindhyachal Takniki

    Thread Starter Member

    Nov 3, 2014
    348
    6
    thanks for help. I have figured it out.
     
Loading...