Py学习  »  Python

Python/Tkinter-总和标签

gdn • 5 年前 • 1371 次点击  

我正在开发一个应用程序来计算税务计算的计算基数(v_result.set和v_result2.set)。我想在vúresult3.set中同时添加这两个,但它给出了错误。见下文:

File "C:\Users\TESTES\Desktop\teeeeeeeeeeeeeeee.py", line 30, in calc
    v_result3.set(float(v_result + v_result2))
TypeError: unsupported operand type(s) for +: 'DoubleVar' and 'DoubleVar'

请遵循以下完整代码:

from tkinter import *

root = Tk()
root.geometry('350x350')


l_label = Label(root, text='Receita 1')
l_label.place(x=10, y=10)
e_entry = Entry(root)
e_entry.place(x=100, y=10)
l_label2 = Label(root, text='Receita 2')
l_label2.place(x=10, y=40)
e_entry2 = Entry(root)
e_entry2.place(x=100, y=40)
# ---
v_result = DoubleVar()
l_rst = Label(root, textvariable=v_result)
l_rst.place(x=10, y=100)
v_result2 = DoubleVar()
l_rst2 = Label(root, textvariable=v_result2)
l_rst2.place(x=10, y=140)
v_result3 = DoubleVar()
l_rst3 = Label(root, textvariable=v_result3)
l_rst3.place(x=10, y=220)


def calc():
        v_result.set(float(e_entry.get()) * 10 / 100)
        v_result2.set(float(e_entry2.get()) * 10 / 100)
        v_result3.set(float(v_result + v_result2))






bt = Button(root, text='Calc', command=calc)
bt.place(x=10, y=180)


root.mainloop()

你能帮帮我吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55929
 
1371 次点击  
文章 [ 1 ]  |  最新文章 5 年前
AKX
Reply   •   1 楼
AKX    5 年前

你需要使用 DoubleVar.get() 要访问包含的值:

def calc():
    v_result.set(float(e_entry.get()) * 10 / 100)
    v_result2.set(float(e_entry2.get()) * 10 / 100)
    v_result3.set(float(v_result.get() + v_result2.get()))

def calc():
    # Read...
    v1 = round(float(e_entry.get()), 1)
    v2 = round(float(e_entry2.get()), 1)

    # Compute...
    result = v1 + v2

    # Write...
    v_result.set(v1)
    v_result2.set(v2)
    v_result3.set(result)