使用 Tkinter 将温度转换为摄氏度、华氏度和开尔文的温度转换器 [英] Temperature converter that converts temp into Celsius, Fahrenheit and Kelvin using Tkinter

查看:73
本文介绍了使用 Tkinter 将温度转换为摄氏度、华氏度和开尔文的温度转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个程序,通过获取用户的有效输入,分别使用 if、elif 和 else 提供转换后的温度,将温度转换为摄氏度、华氏度和开尔文.我刚开始学习 tkinter,所以对它不太了解,请帮助我更正源代码.

I want to create a program that converts temperature into Celsius, Fahrenheit and Kelvin by taking a valid input from the user which provides the converted temp respectively using if, elif, else. I just started learning tkinter so got no much idea about it, Kindly help me correct the source code.

import tkinter as tk
root = tk.Tk()
root.geometry("250x170") 
root.title('Temperature Converter')
frame=tk.Frame(root)
frame.pack()
l=tk.Label(root, text = "HELLO!!! Welcome to Temperature Converter") 
l.config(font =("Courier", 14)) 
l.pack()
t="Enter 1 To Convert Temperature Into Celsius.""\n"+"Enter 2 To Convert Temperature Into Fahrenheit.""\n"+"Enter 3 To Convert Temperature Into Kelvin."
l1=tk.Label(root, text = t,justify="left")
l1.config(font =("Courier", 14)) 
l1.pack(side="top",anchor="nw")  
def printtext():
    global e
    string = e.get() 
    print(string)  
e = tk.Entry(root,font =("Courier", 14))
e.pack(ipady=3,ipadx=3,anchor="center")
e.focus_set()

b = tk.Button(root,text='okay',font =("Courier", 13),command=printtext,anchor="center")
b.pack(side='top')
def Celsius():
    Fah=float(raw_input('Enter the temp in Fahrenheit')
    Celsius=(get(Fah)-32.0)*5.0//9.0)
    clabel=tk.Label(root,Celsius,text="Successfully Converted into Celsius")
    clabel.pack()
def Fahrenheit():
    Cel=float(raw_input('Enter the temp in Celsius')
    Fahrenheit=(get(Cel)*9.0//5.0+32)
    Flabel=tk.Label(root,Cel,text="Successfully Converted into Fahrenheit")
    Flabel.pack()
def Kelvin():
    Ces=float(raw_input('Enter the temp in Celsius')
    Kelvin=(get(Ces)+273)
    Klabel=tk.Label(root,Cel,text="Successfully Converted into Kelvin")
    Klabel.pack()  
def on_button():
    if e.get() == 1:
        Celsius()
    elif e.get() == 2:
        Fahrenheit()
    elif e.get() == 3:
        Kelvin()
    else: 
        tkinter.messagebox.showerror(title=None, message= "Invalid Choice")
root.mainloop()

推荐答案

我已经写了一些代码,但重要的是你尽量去理解它.我使用单选按钮找出哪个是主要温度,哪个是转换温度.我用过radiobutton,你也可以用Combobox.无论如何,我使用了一个函数,然后选择将所有其他值转换为它,然后转换为所需的值.看一看:

I have written up some code, but what matters is that you try to understand it maximum. I have used radiobuttons to find out which is the main temperature and which is the converting temperature. I have used radiobutton, you can use Combobox too. Anyway, I have used a function and then chose to convert all the other values to it, and then to the desired values. Take a look:

from tkinter import *

root = Tk()

lhs = IntVar(value=-1)
rhs = IntVar(value=-1)
opt = ['Celcius','Farenheit','Kelvin']

def convert(temp,from_='K',to='C'):
    if from_ == to: return None
    
    from_k_formulas = {'C':lambda t: t-273.15,'F': lambda t: (t-273.15)*(9/5)+32,'K': lambda t:t}
    to_k_formulas   = {'C':lambda t: t+273.15,'F': lambda t: (t-32)*(5/9)+273.15,'K': lambda t:t}

    return from_k_formulas[to](to_k_formulas[from_](temp))

def process():
    from_ = opt[lhs.get()].upper()[0]
    to    = opt[rhs.get()].upper()[0]
    temp  = int(e.get())

    out   = convert(temp,from_,to)
    if out is not None:
        print(out)
    else:
        print('Choose distinct units')

Label(root,text='From').grid(row=0,column=0)
Label(root,text='To').grid(row=0,column=1)

for idx,txt in enumerate(opt): # To add first 3 radiobuttons
    Radiobutton(root,text=txt,variable=lhs,value=idx).grid(row=idx+1,column=0,padx=10,pady=10)

for idx,txt in enumerate(opt): # To add second 3 radiobuttons
    Radiobutton(root,text=txt,variable=rhs,value=idx).grid(row=idx+1,column=1,padx=10)

e = Entry(root)
e.grid(row=4,column=0,columnspan=2)

Button(root,text='Convert',command=process).grid(row=5,column=0,pady=10,columnspan=2)

root.mainloop()

这里 lhs 用于查找左侧温度,rhs 用于查找右侧温度.它返回所选选项的数量,然后我稍后用返回的数字索引列表.

Here lhs is used to find the left side temperature and rhs is used to find the right side temperatures. And it returns the number of option selected, so then I index the list later with the returned number.

这篇关于使用 Tkinter 将温度转换为摄氏度、华氏度和开尔文的温度转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆