0

and thank you for listening... and sorry for my bad english. I've a problem with a simple python script and i don't know how to smash my head.

That's the code. Is pretty simple and useless (i'm learning python, but my programming skills are embarissing).

import tkinter as tk

class hello:
    button_state = [0,0,0,0,0,0,0,0,0]
    def __init__(self):
        self.root = tk.Tk()
        self.button = tk.Button(self.root, text=self.button_state[0], 
                                           command=self.check(0))
        self.button.pack()
    def check(self,x):
        if x == 0:
            self.button_state[x] = 1
            self.button.config(text=self.button_state[x])

app = hello()
app.root.mainloop()

and the error:

AttributeError: 'hello' object has no attribute 'button'

i don't know why if i use a label the problem dont' exist. I try and try and i think the error was in the argument call in button command?

Thanks in advance :)

Yanez
  • 1

1 Answers1

0

You use command=self.check(0) in init. In check() you use self.button.config. But there is no this attribute (self.button) in the object. Maybe, this code helps you. Also in hello you use a class attribute button_state - you must understand this: see https://docs.python.org/2/tutorial/classes.html (# mistaken use of a class variable)

import Tkinter as tk

class hello:
    button_state = [0,0,0,0,0,0,0,0,0]
    def __init__(self):
        self.root = tk.Tk()        
        self.button = tk.Button(self.root, text=self.button_state[0], command=self.check(0))
        self.button.config(text=self.button_state[0])
        self.button.pack()

    def check(self,x):
        if x == 0:
            self.button_state[x] = 1

app = hello()
app.root.mainloop()