簡體   English   中英

使用 Tkinter() 的 BMI 計算器程序

[英]BMI Calculator program with Tkinter()

我想用 Tkinter 創建一個 BMI 計算器程序,但我被困在計算過程中我使用 StringVar() 來保持用戶數據進行計算,但我不知道如何計算

這是我的代碼:

from tkinter import * 
def mainwindow():
    main = Tk()
    main.geometry("300x400")
    main.title("BMI")
    main.rowconfigure((0,1,2,3,4), weight=1)
    main.columnconfigure(0, weight=1)
    main.config(bg="green")
    main.option_add("*Font", "times 15 bold")
    return main

def createframe(main):
    Label(main, text="BMI APP", bg="lightgreen").grid(row=0, column=0, sticky=N)
    frame_1 = Frame(main, bg="white")
    frame_1.grid(row=1, column=0, sticky="NEWS", padx=10, pady=10, ipady=5)

    frame_2 = Frame(main, bg="white")
    frame_2.grid(row=2, column=0, sticky="NEWS", padx=10, pady=10, ipady=5)

    frame_3 = Frame(main, bg="white")
    frame_3.grid(row=3, column=0, sticky="NEWS", padx=10, pady=10, ipady=20)

    frame_bottom = Frame(main, bg="white")
    frame_bottom.grid(row=4, column=0, sticky="NEWS", padx=10, pady=10, ipady=20)
    frame_bottom.columnconfigure(0, weight=0)
    frame_bottom.columnconfigure(1, weight=2)
    return frame_1, frame_2, frame_3, frame_bottom

def widget(frame_1, frame_2, frame_3, frame_bottom):
    Label(frame_1, text="HEIGHT:(cm.)").grid(row=0, column=0, padx=5, pady=5, sticky=W)
    ent_height = Entry(frame_1, bg="pink", textvariable=height_var)
    ent_height.grid(row=1, column=0, ipadx=40, padx=10, sticky=N+W)

    Label(frame_2, text="WEIGHT:(kg.)").grid(row=0, column=0, padx=5, pady=5, sticky=W)
    ent_weight = Entry(frame_2, bg="lightblue", textvariable=weight_var)
    ent_weight.grid(row=1, column=0, ipadx=40, padx=10, sticky=N+W)

    Button(frame_bottom, text="Calculate", highlightbackground="lightgreen", fg="white", command=find_bmi).grid(row=2, column=1)

    show_data = Label(frame_bottom, bg="white")
    return ent_height, ent_weight


def find_bmi():
    global bmi
    bmi = 0
    height = height_var.get()
    weight = weight_var.get()
    height = float(height) / 100.0
    bmi = float(weight) / height ** 2
    print("BMI = %0.2f" % bmi)

bmi = 0   
main = mainwindow()
height_var = StringVar()
height_var.set("1")
weight_var = StringVar()
weight_var.set("1")
frame_1, frame_2, frame_3, frame_bottom = createframe(main)
ent_height, ent_weight = widget(frame_1, frame_2, frame_3, frame_bottom)
find_bmi()
main.mainloop()

我嘗試設置一個新值並計算它,因為 StringVar() 無法自行計算,但是當我以這種方式使用它時,我必須將默認值設置為 1,如果我不設置它會出錯 ZeroDivisionError: float 除以零 I不想先設置數字,如果我設置第一個用戶會看到該數字

frame_3 用於在計算完成時向用戶顯示 BMI

在進行轉換/計算之前,您可以檢查是否已填寫身高和體重輸入。

from tkinter import * 
def mainwindow():
    main = Tk()
    main.geometry("300x400")
    main.title("BMI")
    main.rowconfigure((0,1,2,3,4), weight=1)
    main.columnconfigure(0, weight=1)
    main.config(bg="green")
    main.option_add("*Font", "times 15 bold")
    return main

def createframe(main):
    Label(main, text="BMI APP", bg="lightgreen").grid(row=0, column=0, sticky=N)
    frame_1 = Frame(main, bg="white")
    frame_1.grid(row=1, column=0, sticky="NEWS", padx=10, pady=10, ipady=5)

    frame_2 = Frame(main, bg="white")
    frame_2.grid(row=2, column=0, sticky="NEWS", padx=10, pady=10, ipady=5)

    frame_3 = Frame(main, bg="white")
    frame_3.grid(row=3, column=0, sticky="NEWS", padx=10, pady=10, ipady=20)

    frame_bottom = Frame(main, bg="white")
    frame_bottom.grid(row=4, column=0, sticky="NEWS", padx=10, pady=10, ipady=20)
    frame_bottom.columnconfigure(0, weight=0)
    frame_bottom.columnconfigure(1, weight=2)
    return frame_1, frame_2, frame_3, frame_bottom

def widget(frame_1, frame_2, frame_3, frame_bottom):
    Label(frame_1, text="HEIGHT:(cm.)").grid(row=0, column=0, padx=5, pady=5, sticky=W)
    ent_height = Entry(frame_1, bg="pink", textvariable=height_var)
    ent_height.grid(row=1, column=0, ipadx=40, padx=10, sticky=N+W)

    Label(frame_2, text="WEIGHT:(kg.)").grid(row=0, column=0, padx=5, pady=5, sticky=W)
    ent_weight = Entry(frame_2, bg="lightblue", textvariable=weight_var)
    ent_weight.grid(row=1, column=0, ipadx=40, padx=10, sticky=N+W)

    Label(frame_3, text="BMI").grid(row=0, column=0, padx=5, pady=5,sticky=W)
    show_data = Label(frame_3)
    show_data.grid(row=1, column=0, ipadx=40, padx=10, sticky=N+W)

    Button(frame_bottom, text="Calculate", highlightbackground="lightgreen", fg="white", command=find_bmi).grid(row=2, column=1)
    return ent_height, ent_weight, show_data


def find_bmi():
    height = height_var.get()
    weight = weight_var.get()
    # check height and weight filled in
    if height and weight:
        height = float(height) / 100.0
        bmi = round(float(weight) / height ** 2, 2)
        show_data.config(text = bmi)
    else:
        show_data.config(text='')


main = mainwindow()
height_var = StringVar()
weight_var = StringVar()
frame_1, frame_2, frame_3, frame_bottom = createframe(main)
ent_height, ent_weight, show_data = widget(frame_1, frame_2, frame_3, frame_bottom)
main.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM