Program Python: Convertor de temperatura Celsius – Fahrenheit

Salutare!

Astăzi vă propun un program simplu care transforma gradele Celsius in Fahrenheit. Este făcut cu ajutorul modulului Tkinter. Mai multe informații despre Tkinter găsiți aici:

https://docs.python.org/3/library/tkinter.html Este foarte folositor!😁

Programul creează întâi fereastra, îi pune titlu, creează un Label() ‘Celsius -> Fahrenheit’, îl afișează cu ajutorul funcției .pack(), apoi creează un Frame() sub Label()-ul ‘Celsius -> Fahrenheit’.

Apoi creează un mesaj (un Label()) pentru Entry(), și după creează un Entry() pentru introducerea valorii gradelor Celsius și, în final, creează funcția de convertire Celsius -> Fahrenheit.

Dar stai! Asta nu e tot! După setăm mesajul pentru eroare (când introducem ceva invalid precum str()-ul ‘1[0‘ in loc de ‘10‘), setăm Label()-ul pentru rezultat, creează Button()-ul pentru transformarea gradelor Celsius în Fahrenheit, setăm dimensiunea ferestrei.

Iată codul scris de mine (*Free to copy and use for educational purposes):

# Copyright 2021 by Sebastian Chetroni - ZCoder.ro
# Free to copy and use for educational purposes
from tkinter import *
import tkinter.font as font

# Afisam numele ferestrei
my_window = Tk()
my_window.title("Convertor Celsius in Fahrenheit")

# Afisam textul la inceputul ferestrei
description = Label(text = 'Celsius -> Fahrenheit', font = font.Font(font='Courier', size = 20), fg = "grey")
description.pack()
frame = Frame(my_window)
frame.pack(pady = 20)

# Text si casuta in care introducem valoarea temperaturii in grade Celsius
message_one = Label(frame, text = 'Introdu temperatura in grade Celsius: ', font = font.Font(font='', size = 10))
message_one.grid(row = 0, column = 0)
celsius_value = Entry(frame)
celsius_value.grid(row = 0, column = 1)

# Functia de convertire a temperaturii din Celsius in Fahrenheit
def convert():
    temp_celsius = celsius_value.get()
    if(temp_celsius.replace('.','',1).isdigit()):
        error_msg.grid_forget()
        temp_fahrenheit = (float(temp_celsius) * 9/5) + 32
        output_fahrenheit.config(text = 'Temperatura in grade Fahrenheit este: ' + str(temp_fahrenheit), fg = 'red')
    else:
        error_msg.grid(row=1, column=1)

# Afisam mesaj de eroare
error_msg = Label(frame, text = 'Te rog sa introduci temperatura in grade Celsius!', font = font.Font(font='', size = 8), fg = 'red')

# Afisam rezultatul
output_fahrenheit = Label(frame, font = font.Font(font='Courier', size = 12))
output_fahrenheit.grid(row = 2, column = 0, columnspan = 2, pady = 10)

#Desenam butonul de conversie
submit_btn = Button(frame, text = 'Transforma', width = 30, fg = "black", bg = "light green", bd = 0, padx = 20, pady = 10, command = convert)
submit_btn.grid(row = 3, column = 0, columnspan = 2, pady = 10)
my_window.geometry('500x250')
my_window.mainloop()

Nu uita: Învață, exersează, repetă!

Ma numesc Sebastian, am 10 ani si sunt pasionat de: programare in Python, C++, Raspberry Pi, citit, astronomie, astrofizica, chimie, sport, fotografie si Xbox. Ma gasesti si pe: Instagram, Youtube, Facebook
Comments

Dă-i un răspuns lui Sebastian Chetroni Anulează răspunsul

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *