2016-07-06 54 views
0

之後的輸出我是蟒蛇初學者,我試圖讓一個BMI計算器 但我有一些問題,我想從self.Heighttype獲取輸入的輸入和輸出如何獲得輸入,並給予計算

self.Weighttype並給出輸出self.BMI

還有一些提示,只是我的編碼?

from tkinter import * 
import tkinter.messagebox 

root = Tk() 
root.resizable(0,0) 

class win1: 
    def __init__(self, master): 
     self.master = master 
     master.title("BMI Calculator") 
     # 
     self.he = IntVar() 
     self.we = IntVar() 
     self.height = Label(master, text="ENTER Your Height(cm) Here:") 
     self.Heighttype = Entry(master, textvariable=self.he) #here 
     self.weight = Label(master,text="ENTER Your Weight(kg) Here:") 
     self.Weighttype = Entry(master, textvariable=self.we) #and here 
     # 
     self.ans = IntVar() 
     self.BMI = Label(master, textvariable=self.ans) #output here 
     self.credit = Button(master, text="Credit", command=self.credit_show) 
     self.result = Button(master, text="Result", command=self.calculation) 
     root.protocol('WM_DELETE_WINDOW', self.ask_quit) 
     self.close = Button(master, text="Close", command=self.ask_quit) 

     self.height.grid(sticky=E, padx=2, pady=4) 
     self.Heighttype.grid(row=0, column=1, columnspan=2, padx=2) 
     self.weight.grid(sticky=E, padx=2, pady=4) 
     self.Weighttype.grid(row=1, column=1, columnspan=2, padx=2) 
     self.BMI.grid(row=2, column=1, columnspan=2, padx=2) 
     self.credit.grid(row=3, sticky=W, padx=4 , pady=4) 
     self.result.grid(row=3, column=1, pady=4, sticky=W+E, padx=4) 
     self.close.grid(row=3, column=2, pady=4, sticky=W+E, padx=1) 

    def calculation(self): 
     # i want to get the user input from top and make calculation 
     # and make a output self.BMI = Label 


    def credit_show(self): 
     tkinter.messagebox.showinfo("Credit","Created by BlackLotus") 
    def ask_quit(self): 
     if tkinter.messagebox.askokcancel("Quit", "Do you want to Quit?"): 
      root.destroy() 

apps = win1(root) 
root.mainloop() 

有人請幫助我。非常感謝。

+0

在'self.he'和'self.we'上使用'.get()'來獲取值。爲了將輸出設置爲'self.BMI',使用'.set(val)'。看到這篇文章(http://effbot.org/tkinterbook/entry.htm) – 2016-07-06 09:43:19

回答

0

使用上IntVar s到獲得的參數和設置的結果.get().set()

def calculation(self): 
    m = self.we.get() 
    l = self.he.get() 
    bmi = # calculate the bmi here 
    self.ans.set(bmi) 

此外,雖然似乎有IntVar工作爲好,似乎更合理,使ans一個DoubleVar ,即:

self.ans = DoubleVar()