Home > Blockchain >  How can i make the button print the result of the function
How can i make the button print the result of the function

Time:12-16

so i have coded this unit conversion program, and i think ive done pretty well until i came to the last part. i cant seem to figure out how to get the button to convert my units and actually print them. Here is my code:

The button works fine but mylabel doesnt seem to get the values correctly and i dont know how to make it get the values correctly, who can show me how i can get it to print the values based on the if and else statements.

from tkinter import *
from tkinter import ttk

root = Tk()
root.title("Unit Converter")
root.geometry("800x500")




var1 = Entry(width = 30)
var1.pack()
var1.place(x=300, y = 21)
var1 = IntVar()

lbl = Label(root, text = "Write Number Here: ")
lbl.pack()
lbl.place(x=180, y=20)




list1 = ["Choose Unit", "Kilogram", "Pounds", "Ounces", "Celsius", "Fahrenheit", "Kelvin", "Kilometers", "Miles", "Yards"]
kg_conversion = ["Pounds", "Ounces"]
pound_conversions = ["Kilogram", "Ounces"]
ounces_conversions = ["Kilogram", "Pounds"]

cel_conversions = ["Fahrenheit", "Kelvin"]
far_conversions = ["Celsius", "Kelvin"]
kel_conversions = ["Celsius", "Kelvin"]

km_conversions = ["Miles", "Yards"]
mile_conversions = ["Kilometers", "Yards"]
yard_conversions = ["Kilometers", "Miles"]

def pick_unit(e):
    
    if drop1.get() == "Kilogram":
        drop2.config(value = kg_conversion)
    if drop1.get() == "Pounds":
        drop2.config(value = pound_conversions)
    if drop1.get() == "Ounces":
        drop2.config(value = ounces_conversions)

    if drop1.get() == "Kilometers":
        drop2.config(value = km_conversions)
    if drop1.get() == "Miles":
        drop2.config(value = mile_conversions)
    if drop1.get() == "Yards":
        drop2.config(value = yard_conversions)

    if drop1.get() == "Celsius":
        drop2.config(value = cel_conversions)
    if drop1.get() == "Fahrenheit":
        drop2.config(value = far_conversions)
    if drop1.get() == "Kelvin":
        drop2.config(value = kel_conversions)
    


drop1 = ttk.Combobox(root, value = list1)
drop1.current(0)
drop1.pack()
drop1.place(x=20, y=19)


drop1.bind("<<ComboboxSelected>>", pick_unit)


drop2 = ttk.Combobox(root, values = [" "])
drop2.current(0)
drop2.pack()
drop2.place(x=20, y=50)

clicked = StringVar()
clicked.set("Choose Unit!")




def cel_to_far():
    cel_far = float(float(var1.get()) * 1.8   32)
    return cel_far

def cel_to_kel():
    cel_kel = float(float(var1.get())   273.15)
    return cel_kel

def far_to_cel():
    far_cel = float(float((var1.get()) - 32) * 0.5556)
    return far_cel

def far_to_kel():
    far_kel = float(float((var1.get())   459.67) * 0.5556)
    return far_kel

def kel_to_cel():
    kel_cel = float(float(var1.get()) - 273.15)
    return kel_cel

def kel_to_far():
    kel_far = float(1.8 * (float(var1.get()) - 273.15)   32)
    return kel_far


def km_to_mile():
    km_m = float(float(var1.get()) * 0.62137119)
    return km_m

def km_to_yard():
    km_yard = float(float(var1.get()) * 1093.613298)
    return km_yard

def mile_to_km():
    mile_km = float(float(var1.get()) * 1.609)
    return mile_km

def mile_to_yard():
    mile_yard = float(float(var1.get()) * 1760)
    return mile_yard

def yard_to_km():
    yard_km = float(float(var1.get()) * 0.0009144)
    return yard_km

def yard_to_mile():
    yard_mile = float(float(var1.get()) /  1760)
    return yard_mile


def kg_to_pounds():
    kg_pound = float(float(var1.get()) * 2.2)
    return kg_pound

def kg_to_ounces():
    kg_ounces = float(float(var1.get()) * 35.274)

def pounds_to_kg():
    pound_kg = float(float(var1.get()) / 2.2)
    return pound_kg

def pounds_to_ounces():
    pound_ounces = float(float((var1.get()) * 16))

def ounces_to_kg():
    ounces_kg = float(float(var1.get()) * 0.028349523)
    return ounces_kg

def ounces_to_pounds():
    ounc_pound = float(float(var1.get()) * 0.0625)
    return ounc_pound


if drop1 == "Kilogram":
    drop2 = OptionMenu(root, clicked, "Pounds", "Ounces")
    if drop2 == "Pounds":
        kg_to_pounds()
    elif drop2 == "Ounces":
        kg_to_ounces()
    


def converting():

    if drop1 == "Kilogram" and drop2 == "Pounds":
        kg_to_pounds()
        
    if drop1 == "Kilogram" and drop2 == "Ounces":
        kg_to_ounces()
    if drop1 == "Pounds" and drop2 == "Kilogram":
        pounds_to_kg()
    if drop1 == "Pounds" and drop2 == "Ounces":
        pounds_to_ounces()
    if drop1 == "Ounces" and drop2 == "Kilogram":
        ounces_to_kg()
    if drop1 == "Ounces" and drop2 == "Pounds":
        ounces_to_pounds()
    
    if drop1 == "Kilometers" and drop2 == "Miles":
        km_to_mile()
    if drop1 == "Kilometers" and drop2 == "Yards":
        km_to_yard()
    if drop1 == "Miles" and drop2 == "Kilometers":
        mile_to_km()
    if drop1 == "Miles" and drop2 == "Yards":
        mile_to_yard()
    if drop1 == "Yards" and drop2 == "Kilometers":
        yard_to_km()
    if drop1 == "Yards" and drop2 == "Miles":
        yard_to_mile()

    if drop1 == "Celsius" and drop2 == "Fahrenheit":
        cel_far = float(float(var1.get()) * 1.8   32)
        return cel_far
    if drop1 == "Celsius" and drop2 == "Kelvin":
        cel_to_kel()
    if drop1 == "Fahrenheit" and drop2 == "Celsius":
        far_to_cel()
    if drop1 == "Fahrenheit" and drop2 == "Kelvin":
        far_to_kel()
    if drop1 == "Kelvin" and drop2 == "Celsius":
        kel_to_cel()
    if drop1 == "Kelvin" and drop2 == "Fahrenheit":
        kel_to_far()

   
    mylabel = Label(root, text = converting)
    mylabel.pack()
    mylabel.place(x = 300, y = 100)   

    

mybutton = Button(root, text = "Convert", command = converting)
mybutton.pack()
mybutton.place(x= 200, y = 100)






root.mainloop()

i was expecting that this button will give me the results

CodePudding user response:

You have several problems.

I have changed only the change from Kilogram to Pounds because the rest is exactly the same.

var1 = Entry(width = 30)
var1.pack()
var1.place(x=300, y = 21)
#var1 = IntVar()

Here as you can see I have commented out the IntVar() line as this is not necessary, the entry.get() is how you get the value of the entry.

def kg_to_pounds():
    kg_pound = float(float(var1.get()) * 2.2)
    return str(kg_pound)

The function must return some result if you want to use it later and as you also use it in a label it must be in string format.

def converting():
    if drop1.get() == "Kilogram" and drop2.get() == "Pounds":
        value = kg_to_pounds()
.
.
.

mylabel = Label(root, text = f'{value}')
mylabel.pack()
mylabel.place(x = 300, y = 100) 

Finally, I have stored the value in a variable called value and it is the one we pass to the result label.

enter image description here

  • Related