我目前正在使用 Tkinter 和 python 创建一个温度转换器,我有一个函数,里面有一个组合框。
当从组合框中选择华氏度或开氏度并输入数字+单击“转换”时,无论您选择什么起始温度,显示的温度都是华氏度和开氏度的温度。
这让我相信组合框无法识别更改并且始终保持为“摄氏度”。
有人建议我使用类,但不确定如何使用。
任何帮助将不胜感激。
这是我的代码:
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()
您的代码不起作用的原因是dropdownBoxTempValue
永远不会改变。它只会是Celsius
。因此,您只能获得Fahrenheit
和kelvin
作为输出。试试打印dropdownBoxTempValue
,你会发现的
而且您不需要函数(或命令),storedTemperature
因为它存储温度
取而代之的是,获取 的值variable
,以便代码工作 -
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()
而且您使用了“巨大”的变量名称,这使得调试变得困难。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句