简体   繁体   English

如何将 stringVar 转换为 tkinter 中的字符串?

[英]How do I convert a stringVar to a string in tkinter?

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.我创建了一个名为 equals 的 function ,它应该评估 label 中的任何内容,但作为字符串。 Right now its a stringVar, and I have no idea how to convert a stringVar into a string.现在它是一个 stringVar,我不知道如何将 stringVar 转换为字符串。 Or if there's any other method, that would be helpful.或者如果有任何其他方法,那将是有帮助的。

You can use the .get() method on a StringVar to get its current text.您可以在 StringVar 上使用.get()方法来获取其当前文本。 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.相反,如果你有一个字符串,你可以将它作为参数传递给 StringVar 的.set()方法,为该 StringVar 设置一个新文本。

To access the contents of a tk.StringVar as a str , you can use <your StringVar name here>.get() .要将tk.StringVar的内容作为str访问,您可以使用<your StringVar name here>.get()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM