here is my code
from tkinter import *
# Main window
App = Tk()
App.title("Converter")
App.geometry('360x180')
# Menu
kind = ['Temperature', 'Length', 'Weight']
all = StringVar()
all_menu = OptionMenu(App, all, *kind)
all_menu.grid(row=0, column=1, pady=5)
# Get index
index = all.get()
if index == 'Temperature' :
scales = ['Fahrenheit','Celsius']
elif index == 'Length' :
scales = ['Mile','Kilometer']
else:
scales = ['Pound','Kilogram']
# The scale of the length to be used for conversion
_from = StringVar()
from_menu = OptionMenu(App, _from, *scales)
from_menu.grid(row=1, column=1, pady=5)
# In between label
lbl = Label(App, text=' convert to ')
lbl.grid(row=1, column=2, pady=5)
# The scale of the length to convert the value to
to_ = StringVar()
to_menu = OptionMenu(App, to_, *scales)
to_menu.grid(row=1, column=3, pady=5)
# Entry pre-label
numL = Label(App, text='Enter: ')
numL.grid(row=2, column=0, columnspan=1, pady=5)
# Entry field
numE = Entry(App)
numE.grid(row=2, column=1, columnspan=1, pady=5)
# In between Entry field and Converter function
equal = Label(App, text=' = ')
equal.grid(row=2, column=2, pady=5)
# Converter temperature function
def Convert_Temperature():
froM = _from.get()
tO = to_.get()
num = int(numE.get())
if froM == 'Fahrenheit' and tO == 'Celsius':
converted_num = (num - 32) * 5 / 9
elif froM == 'Celsius' and tO == 'Fahrenheit':
converted_num = (num * 9 / 5) + 32
else:
converted_num = num
conv_numL = Label(App, text=round(converted_num, 4), width=10)
conv_numL.grid(row=2, column=3, pady=5)
# Converter length function
def Convert_Length():
froM = _from.get()
tO = to_.get()
num = int(numE.get())
if froM == 'Mile' and tO == 'Kilometer':
converted_num = num * 1.609
elif froM == 'Kilometer' and tO == 'Mile':
converted_num = num / 1.609
else:
converted_num = num
conv_numL = Label(App, text=round(converted_num, 4), width=10)
conv_numL.grid(row=2, column=3, pady=5)
# Converter weight function
def Convert_Weight():
froM = _from.get()
tO = to_.get()
num = int(numE.get())
if froM == 'Pound' and tO == 'Kilogram':
converted_num = num / 2.205
elif froM == 'Kilogram' and tO == 'Pound':
converted_num = num * 2.205
else:
converted_num = num
conv_numL = Label(App, text=round(converted_num, 4), width=10)
conv_numL.grid(row=2, column=3, pady=5)
# Convert button
if index == 'Temperature':
conB = Button(App, text='Convert', command=Convert_Temperature)
elif index == 'Length':
conB = Button(App, text='Convert', command=Convert_Length)
else:
conB = Button(App, text='Convert', command=Convert_Weight)
conB.grid(row=3, column=1, pady=5)
# Clear text function
def clear_text():
numE.delete(0, END)
_from.set("")
to_.set("")
conv_numL = Label(App, text="", width=10)
conv_numL.grid(row=2, column=3, pady=5)
# Clear text button
btn = Button(App, text="Restart", command=clear_text)
btn.grid(row=3, column=3, pady=5)
App.mainloop()
# Label -> print text
# Grid -> Locate
# Entry -> table
Aucun commentaire:
Enregistrer un commentaire