0

I'm creating a basic calculator and this is what I have so far:

from tkinter import *
e = Tk(className="Krishna's Calculator")
e.geometry("460x614")
e.resizable(0,0)

def insert(value):
    var.set(var.get() + value)
    eval(var)
def button(text, width, font, highlightbackground, x, y):
    tkinter_button = Button(e, text=text, width=width, command=lambda: insert(text), font=font, highlightbackground=highlightbackground)
    tkinter_button.place(x=x, y=y)
def clear():
    var.set(" ") 
def equals(var):
    return eval(var)
helvetica = "Helvetica 50"
arial = "Arial 50 bold"

button(text = "÷", width=4, font = helvetica, highlightbackground='#8533ff', x = 344, y = 301)
button(text = "×", width=4, font = helvetica, highlightbackground='#8533ff', x = 344, y = 364)
button(text = "-", width=4, font = helvetica, highlightbackground='#8533ff', x = 344, y = 426)
button(text = "+", width=4, font = helvetica, highlightbackground='#8533ff', x = 344, y = 488)
equal = Button(e, text = "=",width=4, command = equals([insert the text in the label here]), font = "Helvetica 50", highlightbackground='#8533ff')
equal.place(x = 344, y = 551)
aclear = Button(e, text = "AC",width=8, command = clear, font = "Helvetica 50",highlightbackground='#737373')
aclear.place(x = 0, y = 302)
button(text = "%", width=4, font = helvetica,highlightbackground='#737373', x = 228, y = 302)
button(text = ".", width=4, font = arial,highlightbackground='#ffffff', x = 228, y = 550)
button(text = "0", width = 8, font = arial,highlightbackground='#ffffff', x = 0, y = 550)
button(text = "1", width =4, font = arial,highlightbackground='#ffffff', x = 0, y = 488)
button(text = "2", width =4, font = arial,highlightbackground='#ffffff', x = 114, y = 488)
button(text = "3", width=4, font = arial,highlightbackground='#ffffff', x = 228, y = 488)
button(text = "4", width=4, font = arial,highlightbackground='#ffffff', x = 0, y = 426)
button(text = "5", width=4, font = arial,highlightbackground='#ffffff', x = 114, y = 426)
button(text = "6", width=4, font = arial,highlightbackground='#ffffff', x = 228, y = 426)
button(text = "7", width=4, font = arial ,highlightbackground='#ffffff', x = 0, y = 364)
button(text = "8", width=4, font = arial,highlightbackground='#ffffff', x = 114, y = 364)
button(text = "9", width=4, font = arial,highlightbackground='#ffffff', x = 228, y = 364)
var = StringVar()
label = Label(e, textvariable = var,bd=5,width=16, relief = SOLID, font = "Arial 50 ",bg="white", fg="black",activebackground="#bb99ff", height = 5,pady = 3)
label.place(x=0,y=0)
e.mainloop()

When I press the equals button, nothing happens. I created a function called equals and its supposed to evaluate whatever's in the label but as a string. Right now its a stringVar, and I have no idea how to convert a stringVar into a string. Or if there's any other method, that would be helpful.

3
  • 1
    You obviously already know how to retrieve the text of a StringVar, because you did exactly that in insert(). Commented Aug 12, 2022 at 13:33
  • 2
    Does this answer your question? Access StringVar() as a normal string in python Commented Aug 12, 2022 at 13:33
  • 1
    If you don't want downvotes, don't post a duplicate question, especially with a dump of all your code instead of a minimal reproducible example. Take the tour and check out How to Ask. Commented Aug 12, 2022 at 13:39

2 Answers 2

0

To access the contents of a tk.StringVar as a str, you can use <your StringVar name here>.get().

Sign up to request clarification or add additional context in comments.

2 Comments

@Rory what? I literally said you are accessing the contents. As in, accessing the string value.
@Rory that's precisely why I said "<your StringVar name here>.get() because you have to call it on the variable name... how much more obvious could I make it... It was marked as the correct answer so OP clearly understood.
0

You can use the .get() method on a StringVar to get its current text. Conversely, if you have a strng, you can pass it as an argument to the .set() method on the StringVar to set a new text for that StringVar.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.