TKinter 如何进行转换

问题描述 投票:0回答:1

我一直在尝试将摄氏度转换为华氏度和开尔文,但我对 Tkinter 很陌生,还没有找到人来解释它如何进行数学过程以返回新比例的度数,也因为如果只是这样做就太好了问这个代码中有一些西班牙语,我只是把它留在那里,希望有人能够理解它

    import tkinter as tk

root = tk.Tk()
root.title('Conversor de grados')
root.geometry('400x500')
root.resizable(False,False)


 name_label = tk.Label(
 root,
 text = 'Convertir a ºF y K',
 font = ('Arial', 200 ),
 bg = 'blue',
 fg = '#FF0' )


number_label = tk.Label(
    root,
    text = '¿Que temperatura hace?'
    )
number_input = tk.Entry(root)
#number_input = grados introducidos
value = number_input



submit_button1 = tk.Button (root, text = 'convertir a ºF')
output_line = tk.Label(root)
submit_button2 = tk.Button (root, text = 'convertir a K')
output_line = tk.Label(root)

number_label.pack()
number_input.pack()


submit_button1.pack()
submit_button2.pack()

output_line.pack()

def resultadof():
    f = int((value*9/5)+32)
    
def on_submit1():
        """Cuado se pulse el botón"""
         print "resultadof"
            
        
            
        output_line.configure(text=message)
        
def on_submit2():
        """Cuado se pulse el botón"""
    
        number = number_input.get()
    
   
   
        resultadof=(number*9/5)+32
        
        message = (
            f'son {number_input} grados.\n'
        
            )
        output_line.configure(text=message)


if submit_button1.configure(command = on_submit1):
   submit_button2.configure(command = on_submit2)

root.mainloop()

---

- Im trying to turn celsius into kelvin and fahrenheit through tk, i tried using buttons as it seamed the easiest, I wasnt able, the program should just return the degrees on the scale you choose

tkinter
1个回答
0
投票

我已经编辑了你的代码并使用了英语。

import tkinter as tk

首先,我创建了两个函数来计算摄氏度到华氏度和摄氏度到开尔文。两者都有名为摄氏度的参数。

def celsius_to_fahrenheit(celsius):
    fahrenheit = (celsius * 9/5) + 32
    return fahrenheit

def celsius_to_kelvin(celsius):
    kelvin = celsius + 273.15
    return kelvin

convert_Temperature 是

convert_button
的命令函数。它基本上从
number_input
获取值并检查
selected_scale
。之后运行我上面提到的计算函数,具体取决于它们。计算完成后会更新
output_line
.

def convert_temperature():
    try:
        celsius = float(number_input.get())
        if selected_scale.get() == "Fahrenheit":
            result = celsius_to_fahrenheit(celsius)
            unit = "°F"
        elif selected_scale.get() == "Kelvin":
            result = celsius_to_kelvin(celsius)
            unit = "K"
        else:
            result = "Error: Select a scale"
            unit = ""
        output_line.config(text=f"{result:.2f} {unit}")
    except ValueError:
        output_line.config(text="Invalid input. Enter a number.")

root = tk.Tk()
root.title('Temperature Converter')  
root.geometry('400x200')

name_label = tk.Label(
    root,
    text='Convert to °F and K', 
    font=('Arial', 16),  
    bg='blue',
    fg='#FF0'
)
name_label.pack()

number_label = tk.Label(
    root,
    text='Enter temperature in Celsius:'  
)
number_label.pack()

number_input = tk.Entry(root)
number_input.pack()

selected_scale = tk.StringVar()
selected_scale.set("Select scale") 

scale_options = ["Fahrenheit", "Kelvin"]
scale_dropdown = tk.OptionMenu(root, selected_scale, *scale_options)
scale_dropdown.pack()

convert_button = tk.Button(root, text="Convert", command=convert_temperature)
convert_button.pack()

output_line = tk.Label(root)
output_line.pack()

root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.