我正在使用 Tkinter GUI 模块编写一个小型 Python 计算器。我遇到了一个问题:我无法将整数值准确地输入到变量中。尝试添加两个输入的数字时,会出现错误:TypeError: unsupported operand type(s) for +: 'StringVar' and 'StringVar'. 试图使用相同的IntVar- 相同的错误:TypeError: unsupported operand type(s) for +: 'IntVar' and 'IntVar'。我想学习如何将整数值输入变量并与它们进行算术运算。
编码:
from tkinter import *
root = Tk()
root.title('Calculator Pro')
root.geometry("300x300")
def sum():
print (number1 + number2)
number1 = StringVar()
number2 = StringVar()
number1_entry = Entry(textvariable=number1)
number1_entry.pack()
number2_entry = Entry(textvariable=number2)
number2_entry.pack()
bt = Button(text="Summa", command=sum)
bt.pack()
root.mainloop()
截图(所以,以防万一):

使变量IntVar并取值前求和
要么覆盖类