1

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

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'
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
vindhyachal
  • 69
  • 1
  • 7
  • 2
    It looks like you are never initializing `master` to anything since you are passing `None` to the `__init__` method. What type is `master` supposed to be? – Engineero Jul 11 '15 at 17:11

2 Answers2

4

You are setting your master variable to None, as you are creating the App1 object by passing parameter as None in the following line -

root = App1(None)

And hence, when you call - self.master.after(1 , self.update_reading) , you are getting the AttributeError , as self.master is None.

I am not sure what you want to achieve but you can try setting self.master to something other than None (whatever you intended it to be).

2

The error is self-explanatory : self.master is None. It comes from the line

 root = App1(None)

If you want a timer, see this question for instance :

self.root.after(1 , self.update_reading)
Patrick
  • 566
  • 3
  • 7