How do I get a combo box working in a nested function?

I’m currently creating a temperature converter using Tkinter and python and I have a function which has a combo box in it.

When selecting Fahrenheit or Kelvin from the combo box, and inputting a number + clicking ‘CONVERT’ the temperatures that are displayed are that of Fahrenheit and Kelvin no matter what starting temperature you select.

This leads me to believe the combo box isn’t recognizing the change and is always staying as ‘Celsius’.

I have been adviced to use classes but am unsure of how to use so.

Any help will be greatly appreciated.

here is my code:

from tkinter import *
from tkinter import ttk

def tempConverter():
    temperatureConverter = Tk()
    temperatureConverter.geometry('650x650')


    dropDownBoxTempValue = "Celsius"

    def storedTemperature(setTemp):
        global dropDownBoxTempValue
        dropDownBoxTempValue = setTemp

    def convert():
        temperature = enterTemperature.get()
        if dropDownBoxTempValue == 'Celsius':
            f = (float(temperature) * 9 / 5) + 32
            temperatureLabel['text'] = f'{round(f, 2)}° Fahrenheit'
            k = (float(temperature)) + 273.15
            temperatureLabel2['text'] = f'{round(k, 2)}° Kelvin'

        elif dropDownBoxTempValue == 'Fahrenheit':
            c = (float(temperature) - 32) * 5 / 9
            temperatureLabel['text'] = f'{round(c, 2)}° Celsius'
            k = (float(temperature) - 32) * 5 / 9 + 273.15
            temperatureLabel2['text'] = f'{round(k, 2)}° Kelvin'

        elif dropDownBoxTempValue == 'Kelvin':
            c = (float(temperature) - 273.15)
            temperatureLabel['text'] = f'{round(c, 2)}° Celsius'
            f = (float(temperature) - 273.15) * 9 / 5 + 32
            temperatureLabel2['text'] = f'{round(f, 2)}° Fahrenheit'

    inputNumber = StringVar()
    variable = StringVar()

    enterTemperature = Entry(temperatureConverter, textvariable=inputNumber, font='Helvetica 14')
    enterTemperature.pack()
    temperatureLabel = Label(temperatureConverter, text="")
    temperatureLabel.pack()
    temperatureLabel2 = Label(temperatureConverter, text="")
    temperatureLabel2.pack()

    dropDownList = ["Celsius", "Fahrenheit", "Kelvin"]
    temperaturedropDown = OptionMenu(temperatureConverter, variable, *dropDownList, command=storedTemperature)
    temperaturedropDown.pack()
    variable.set(dropDownList[0])

    convertButton = Button(temperatureConverter, text="CONVERT", command=convert)
    convertButton.pack()

    temperatureConverter.mainloop()

tempConverter()

Answer

The reason your code doesn’t work is that the dropdownBoxTempValue never changes. It will only be Celsius. Therefore, you get only Fahrenheit and kelvin as output. Try printing dropdownBoxTempValue, you will find it out

And you do not need the function (or command) storedTemperature because it stores the temperature

Instead of that, get the value of variable, so that the code works –

from tkinter import *
from tkinter import ttk

def tempConverter():
    temperatureConverter = Tk()
    temperatureConverter.geometry('650x650')


    dropDownBoxTempValue = "Celsius"
    
    def convert():
        temperature = enterTemperature.get()
        unit = variable.get() # Here
        #print(dropDownBoxTempValue) Try this
        
        if unit == 'Celsius':
            f = (float(temperature) * 9 / 5) + 32
            temperatureLabel['text'] = f'{round(f, 2)}° Fahrenheit'
            k = (float(temperature)) + 273.15
            temperatureLabel2['text'] = f'{round(k, 2)}° Kelvin'

        elif unit == 'Fahrenheit':
            c = (float(temperature) - 32) * 5 / 9
            temperatureLabel['text'] = f'{round(c, 2)}° Celsius'
            k = (float(temperature) - 32) * 5 / 9 + 273.15
            temperatureLabel2['text'] = f'{round(k, 2)}° Kelvin'

        elif unit == 'Kelvin':
            c = (float(temperature) - 273.15)
            temperatureLabel['text'] = f'{round(c, 2)}° Celsius'
            f = (float(temperature) - 273.15) * 9 / 5 + 32
            temperatureLabel2['text'] = f'{round(f, 2)}° Fahrenheit'

    inputNumber = StringVar()
    variable = StringVar()

    enterTemperature = Entry(temperatureConverter, textvariable=inputNumber, font='Helvetica 14')
    enterTemperature.pack()
    temperatureLabel = Label(temperatureConverter, text="")
    temperatureLabel.pack()
    temperatureLabel2 = Label(temperatureConverter, text="")
    temperatureLabel2.pack()

    dropDownList = ["Celsius", "Fahrenheit", "Kelvin"]
    temperaturedropDown = OptionMenu(temperatureConverter, variable, *dropDownList)
    temperaturedropDown.pack()
    variable.set(dropDownList[0])

    convertButton = Button(temperatureConverter, text="CONVERT", command=convert)
    convertButton.pack()

    temperatureConverter.mainloop()

tempConverter()

And you have used “huge”` variables names, it makes it difficult to debug.