How to display in a Label the result of 2 other Labels in Python

Asked

Viewed 169 times

-3

I am creating the following budget program with Python and Tkinter.

#  Copyright (c) Todos os Direitos Reservados a Daniel Heringer
import locale
locale.setlocale(locale.LC_MONETARY, locale= 'PT-BR')

from tkinter import Tk, Label, Button, Entry
janela = Tk ( )
ha = 0
valor = 0
comb = 0
dist = 0
pl = 0
gastos2 = 0

#Calculo do Valor da Medição

def alqueire_click():
    global valor
    medida = float ( ed.get ( ) )
    hb = medida
    lb["text"] = (f'{hb * 4.84:.2f}')
    ha = hb * 4.84
    if ha > 500:
        valor = 33.5 * ha
    if ha < 500:
        valor = 33.5 * ha
    if ha < 400:
        valor = 33.61 * ha
    if ha < 350:
        valor = 34 * ha
    if ha < 300:
        valor = 34.4 * ha
    if ha < 250:
        valor = 37 * ha
    if ha < 200:
        valor = 40.62 * ha
    if ha < 160:
        valor = 46.66 * ha
    if ha < 120:
        valor = 58.75 * ha
    if ha < 80:
        valor = 60 * ha
    if ha < 70:
        valor = 61.66 * ha
    if ha < 60:
        valor = 64 * ha
    if ha < 50:
        valor = 67.5 * ha
    if ha < 40:
        valor = 73.33 * ha
    if ha < 30:
        valor = 85 * ha
    if ha < 20:
        valor = 109 * ha
    if ha < 10:
        valor = 150 * ha
    if ha < 5:
        valor = 800
    if ha < 2:
        valor = 700
    if ha < 1:
        valor = 600
    valorreal = locale.currency(valor, grouping=True)
    lb2["text"] = (valorreal)

def Hectares_click():
    medida = float ( ed.get ( ) )
    hb = medida
    lb["text"] = (f'{hb:.2f}')
    ha = hb
    if ha > 500:
        valor = 33.5 * ha
    if ha < 500:
        valor = 33.5 * ha
    if ha < 400:
        valor = 33.61 * ha
    if ha < 350:
        valor = 34 * ha
    if ha < 300:
        valor = 34.4 * ha
    if ha < 250:
        valor = 37 * ha
    if ha < 200:
        valor = 40.62 * ha
    if ha < 160:
        valor = 46.66 * ha
    if ha < 120:
        valor = 58.75 * ha
    if ha < 80:
        valor = 60 * ha
    if ha < 70:
        valor = 61.66 * ha
    if ha < 60:
        valor = 64 * ha
    if ha < 50:
        valor = 67.5 * ha
    if ha < 40:
        valor = 73.33 * ha
    if ha < 30:
        valor = 85 * ha
    if ha < 20:
        valor = 109 * ha
    if ha < 10:
        valor = 150 * ha
    if ha < 5:
        valor = 800
    if ha < 2:
        valor = 700
    if ha < 1:
        valor = 600
    lb2["text"] = (f"{valor:.2f}")

def Litros_click():
    medida = float ( ed.get ( ) )
    hb = medida
    lb["text"] = (f'{hb * 0.0605:.2f}')
    ha = hb * 0.0605
    if ha > 500:
        valor = 33.5 * ha
    if ha < 500:
        valor = 33.5 * ha
    if ha < 400:
        valor = 33.61 * ha
    if ha < 350:
        valor = 34 * ha
    if ha < 300:
        valor = 34.4 * ha
    if ha < 250:
        valor = 37 * ha
    if ha < 200:
        valor = 40.62 * ha
    if ha < 160:
        valor = 46.66 * ha
    if ha < 120:
        valor = 58.75 * ha
    if ha < 80:
        valor = 60 * ha
    if ha < 70:
        valor = 61.66 * ha
    if ha < 60:
        valor = 64 * ha
    if ha < 50:
        valor = 67.5 * ha
    if ha < 40:
        valor = 73.33 * ha
    if ha < 30:
        valor = 85 * ha
    if ha < 20:
        valor = 109 * ha
    if ha < 10:
        valor = 150 * ha
    if ha < 5:
        valor = 800
    if ha < 2:
        valor = 700
    if ha < 1:
        valor = 600
    lb2["text"] = (f"{valor:.2f}")

def MetrosQ_click():
    medida = float ( ed.get ( ) )
    hb = medida
    lb["text"] = (f'{hb / 10000:.2f}')
    ha = hb / 10000
    if ha > 500:
        valor = 33.5 * ha
    if ha < 500:
        valor = 33.5 * ha
    if ha < 400:
        valor = 33.61 * ha
    if ha < 350:
        valor = 34 * ha
    if ha < 300:
        valor = 34.4 * ha
    if ha < 250:
        valor = 37 * ha
    if ha < 200:
        valor = 40.62 * ha
    if ha < 160:
        valor = 46.66 * ha
    if ha < 120:
        valor = 58.75 * ha
    if ha < 80:
        valor = 60 * ha
    if ha < 70:
        valor = 61.66 * ha
    if ha < 60:
        valor = 64 * ha
    if ha < 50:
        valor = 67.5 * ha
    if ha < 40:
        valor = 73.33 * ha
    if ha < 30:
        valor = 85 * ha
    if ha < 20:
        valor = 109 * ha
    if ha < 10:
        valor = 150 * ha
    if ha < 5:
        valor = 800
    if ha < 2:
        valor = 700
    if ha < 1:
        valor = 600
    lb2["text"] = (f"{valor:.2f}")

def gastos_click():
    comb = float ( lb3.get ( ) )
    dist = float ( lb4.get ( ) )
    pl = float( lb5.get ( ) )
    gastos2 = (pl / dist) * comb
    lb6["text"] = (f"{gastos2:.2f}")




ed = Entry ( janela )
ed.place ( x=130, y = 60)
ed.config( width="25", borderwidth="2", justify="center")

ed1 = Label (janela, text="Insira a Área", font="Arial 12 bold", foreground="#aaaa7f")
ed1.place ( x=25, y=57 )
ed1.config(bg="#3b3b3b")

bl = Button ( janela, width=20, text="Alqueires", command=alqueire_click)
bl.place ( x=50, y=450 )

b2 = Button ( janela, width=20, text="Hectares", command=Hectares_click)
b2.place ( x=220, y=450 )

b3 = Button ( janela, width=20, text="Litros", command=Litros_click)
b3.place ( x=50, y=500 )

b4 = Button ( janela, width=20, text="Metros Quadrados", command=MetrosQ_click)
b4.place ( x=220, y=500 )

lb = Label ( janela )
lb.place ( x=130, y=180 )
lb.config(background="#aaaa7f", relief="sunken", font="Arial 12 bold", width="15")

lbv = Label (janela, text="Hectares", font="Arial 12 bold", foreground="#aaaa7f")
lbv.place ( x=50, y=180 )
lbv.config(bg="#3b3b3b")

lb2 = Label (janela)
lb2.place (x=130, y=150)
lb2.config(background="#aaaa7f", relief="sunken", font="Arial 12 bold", width="15")

lbv2 = Label (janela, text="Valor", font="Arial 12 bold", foreground="#aaaa7f")
lbv2.place ( x=80, y=150 )
lbv2.config(bg="#3b3b3b")

lb3 = Entry (janela)
lb3.place (x=130, y=220)
lb3.config( width="25", borderwidth="2", justify="center")

#Gasto Combustivel

lbv3 = Label (janela, text="Combustivel", font="Arial 12 bold", foreground="#aaaa7f")
lbv3.place ( x=20, y=220 )
lbv3.config(bg="#3b3b3b")

lb4 = Entry (janela)
lb4.place (x=130, y=250)
lb4.config( width="25", borderwidth="2", justify="center")

lbv4 = Label (janela, text="Consumo", font="Arial 12 bold", foreground="#aaaa7f")
lbv4.place ( x=40, y=250 )
lbv4.config(bg="#3b3b3b")

lb5 = Entry (janela)
lb5.place (x=130, y=280)
lb5.config( width="25", borderwidth="2", justify="center")

lbv5 = Label (janela, text="Distância", font="Arial 12 bold", foreground="#aaaa7f")
lbv5.place ( x=40, y=280 )
lbv5.config(bg="#3b3b3b")

lb6 = Label (janela)
lb6.place (x=130, y=310)
lb6.config(background="#852a2a", relief="sunken", font="Arial 12 bold", width="15")

lbv6 = Label (janela, text="Gasto Comb.", font="Arial 12 bold", foreground="#aaaa7f")
lbv6.place ( x=20, y=310 )
lbv6.config(bg="#3b3b3b")

b5 = Button ( janela, width=20, text="Calcular Gastos", command=gastos_click)
b5.place ( x=133, y=340 )

# Lucro Liquído
lucroliquido = Label (janela, text="Lucro Liquído", font="Arial 12 bold", foreground="#aaaa7f")
lucroliquido.place ( x=400, y=310 )
lucroliquido.config(bg="#3b3b3b")

lucroliquidovalor = Label (janela)
lucroliquidovalor.place (x=520, y=310)
lucroliquidovalor.config(background="#8AAB32", relief="sunken", font="Arial 12 bold", width="15")



janela.title ( "Orçamento de Agrimensura" )
janela.geometry ( "1360x768" )
janela.config(bg="#3b3b3b")
janela.mainloop ( )

And on the Net Profit Label I would like to take the result of the Value Labels and subtract them to get a net profit result. But I’m not getting him to display that result. How could I do that?

1 answer

0


Are you doing this project with functions only? I recommend doing it with POO, creating class and using an instance object (self) to be able to work with the objects of other methods. Accessing objects using only functions, is not possible in this case.

  • I changed it already , I made the interface now all in Qt Designer and I’m just giving functions to buttons, see : https://github.com/danielheringers/pyqttest/blob/master/test Checkbox, combo box and generate budget are already ready, missing now know how to take all information including names and Cpf and generate a printable budget file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.