Home > Mobile >  How do I convert a stringVar to a string in tkinter?
How do I convert a stringVar to a string in tkinter?

Time:08-12

Dont downvote please I only started coding Tkinter very recently and I don't want to not be able to ask another question for a day!

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.

Thanks for your help!

CodePudding user response:

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

CodePudding user response:

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.

  • Related