📜  用于 GUI 的在线 python 编译器 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:21.969000             🧑  作者: Mango

代码示例6
from tkinter import *

app = Tk()
app.title("Unit Converter")
scales = ["Meters","Inches","Foot"]

_from = StringVar()
from_menu = OptionMenu(app, _from, *scales)
from_menu.grid(row=0,colomn=0,pady=5)

lbl = Label(app,text = 'Convert to')
lbl.grid(row=0,colomn=1,pady=5)

to_ =StringVar()
to_menu = OptionMenu(app,to_,*scales)
to_menu.grid(row=0,colomn=2,pady=5)

enterLabel = Label(app,text='Enter your length')
enterLabel.grid(row=1,colomn=0,colomnspan=2,pady=5)

num1 = Entry(app)
num1.grid(row=1,colomn=2,colomnspan=2,pady=5)

## 1 Meter = 39.37 Inches
## 1 Meter = 3.28 Inches
## 1 Foot = 12 Inches

def converter():
    user_input_type = _from.get()
    convert_type = to_.get()
    user_input_number = num1.get()

    if user_input_type == 'Meters' and convert_type == 'Inches':
        converted_value = user_input_number * 39.37
    elif user_input_type == 'Meters' and convert_type == 'Foot':
        converted_value = user_input_number * 3.28
    elif user_input_type == 'Inches' and convert_type == 'Meters':
        converted_value = user_input_number / 39.37
    elif user_input_type == 'Foot' and convert_type == 'Inches':
        converted_value = user_input_number * 12
    elif user_input_type == 'Inches' and convert_type == 'Foot':
        converted_value = user_input_number / 12
    elif user_input_type == 'Foot' and convert_type == 'Meters':
        converted_value = user_input_number / 3.28
    else:
        converted_value = user_input_number

    output_number_label = Label(app,text = round(converted_value,2),width=10)
    output_number_label.grid(row=1,colomn=4,pady=5)


conversion_button = Button(app,text='convert',command='onverter')
conversion_button.grid(row='2',colomn='0',pady='5')

app.mainloop()