Warriors Path
Warriors Path

Reputation: 11

'int' object has no attribute 'set'

I'm working on a project where you click a button and it changes an int and writes it to the screen. My issues is that when I try to set a new value to the int it comes back with an AttributeError.

def busy():
    unit_status.set(7)

Everything else is working except for that one line, and I can't for the life of me figure out why.

Upvotes: 0

Views: 1940

Answers (2)

egghead jr
egghead jr

Reputation: 111

While this thread is a bit old, I dont think LouieC's response fully answered the OP's concern.

LouieC mentions that set is a built-in class, which is correct. But it is likely Warrior's Path was looking for the values, since he wanted to write them to the screen.

IF he didnt ask, then I am asking based upon an observation in the following code, adapted from the geeksforgeeks.org explanation. My point is addressed in the comments, particularly at the end.

Notice when LouieC's technique is applied it seems to incorrectly overwrite the entirety of IntVar.

# importing tkinter module
from tkinter import *
 
# creating Tk() variable
# required by Tkinter classes
master = Tk()
 
# Tkinter variables
# initialization using constructor
intvar = IntVar(master, value = 25, name ="2")
strvar = StringVar(master, "Hello !")
boolvar = BooleanVar(master, True)
doublevar = DoubleVar(master, 10.25)

print(intvar) # This prints the NAME, not the value....  the name is auto-assigned by python
print(strvar) # if not explicity declared...
print(boolvar)
print(doublevar)

print(intvar.get()) # This prints the VALUE, not the name....
print(strvar.get())
print(boolvar.get())
print(doublevar.get())


# But now watch what happens...


intvar = 1


print(intvar)
print(intvar.get())

# What's interesting here is...  print(intvar.get()) worked at line 20...

and yet now it generates the following error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-61984dfda0fb> in <module>
     26 intvar = 1
     27 print(intvar)
---> 28 print(intvar.get())

AttributeError: 'int' object has no attribute 'get'

If one runs a type test, in the first case, around line 20:

print(type(intvar))

One will get:

<class 'tkinter.IntVar'>

But if one runs the same type test after LouieC's reassignment, one will get:

<class 'int'>

That's why I said the reassignment doesnt work right.

The OP's question still seems to be open.

Upvotes: 1

LouieC
LouieC

Reputation: 708

This is not how you reassign a variable of type integer; you want:

unit_status = 7

set is a built-in class in Python; official docs here

A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. (For other containers see the built-in dict, list, and tuple classes, and the collections module.)

Upvotes: 0

Related Questions