Search code examples
pythonpython-3.xtkintersettkinter-label

AttributeError: 'str' object has no attribute 'set' (Tkinter)


I'm doing the calculator by tkinter and having trouble with the button. When I clicked, it show the error like this:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
    return self.func(*args)
  File "C:\Users\ADMIN\Desktop\SSCalc\calc.py", line 19, in <lambda>
    btn1 = tk.Button(root,text='1',height=0,width=5,font=fontBtn,command=lambda:pressBtn(1))
  File "C:\Users\ADMIN\Desktop\SSCalc\calc.py", line 12, in pressBtn
    mathValue.set(mathValue)
AttributeError: 'str' object has no attribute 'set'

And here is my code:

import tkinter as tk
from tkinter import font as tkFont
from tkinter import StringVar, Entry, Button
from tkinter.ttk import *
import math
root = tk.Tk()
root.title("Simple Calculator")
mathValue = ""
def pressBtn(number):
    global mathValue
    mathValue+=str(number)
    mathValue.set(mathValue)
def mainCalc():
    mathValue = StringVar()
    fontBtn = tkFont.Font(family="Helvetica",size=15,weight='bold')
    inputMath = Label(root,textvariable=mathValue,relief='sunken')
    inputMath.config(text="Enter Your Calculation...", width=50)
    inputMath.grid(columnspan=4,ipadx=100,ipady=15) 
    btn1 = tk.Button(root,text='1',height=0,width=5,font=fontBtn,command=lambda:pressBtn(1))
    btn1.grid(row=1,column=0)
    btn2 = tk.Button(root,text='2',height=0,width=5,font=fontBtn,command=lambda:pressBtn(2))
    btn2.grid(row=1,column=1)
mainCalc()
root.mainloop()

Can anyone find a way to fix this error for me? Thanks!


Solution

  • There are 2 issues in your code:

    • Doing mathValue+=str(number) creates a local variable called mathValue that is a string.
    • global mathValue on top of that line turns it into a global variable.

    Thus .get() doesn't work on a string object.

    The following code works:

    import tkinter as tk
    from tkinter import font as tkFont
    from tkinter import StringVar, Entry, Button
    from tkinter.ttk import *
    import math
    root = tk.Tk()
    root.title("Simple Calculator")
    mathValue = ""
    
    def pressBtn(number):
        mathValue.set(mathValue.get() + str(number))
    
    def mainCalc():
        global mathValue
        mathValue = StringVar()
        fontBtn = tkFont.Font(family="Helvetica",size=15,weight='bold')
        inputMath = Label(root,textvariable=mathValue,relief='sunken')
        inputMath.config(text="Enter Your Calculation...", width=50)
        inputMath.grid(columnspan=4,ipadx=100,ipady=15) 
        btn1 = tk.Button(root,text='1',height=0,width=5,font=fontBtn,command=lambda:pressBtn(1))
        btn1.grid(row=1,column=0)
        btn2 = tk.Button(root,text='2',height=0,width=5,font=fontBtn,command=lambda:pressBtn(2))
        btn2.grid(row=1,column=1)
    
    mainCalc()
    root.mainloop()
    

    I have made 2 corrections to this code:

    • I have placed the global mathValue line in mainCalc. This makes the StringVar into a global variable.
    • And I have replaced the 2 lines in pressBtn with mathValue.set(mathValue.get() + str(number)). Here, mathValue.get() gets the previously stored value in mathValue (if any, else, retuns '' if no value is present) and + str(number) appends the new value. Finally mathValue.set sets the fresh value.