2016-07-06 4 views
0

Ich bin Anfänger für Python geben, und ich versuchte, einen BMI-Rechner aber ich mit dem Eingang und Ausgang Eingang von self.Heighttype bekommenWie Eingabe erhalten und einen Ausgang nach der Berechnung

Ich mag einige Probleme zu machen und self.Weighttype und geben Sie eine Ausgabe bei self.BMI

Und ein paar Tipps, um einfach meine Codierung?

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() 

Jemand mir bitte helfen. Danke vielmals.

+0

Verwenden Sie '.get()' auf 'self.he' und' self.we', um den Wert zu erhalten. Um die Ausgabe auf 'self.BMI' zu setzen, verwenden Sie' .set (val) '. Siehe diesen [Artikel] (http://effbot.org/tkinterbook/entry.htm) –

Antwort

0

Verwenden .get() und .set() auf dem IntVar s um die Parameter zu erhalten und setzen Sie das Ergebnis:

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

Auch, während es mit einem IntVar als gut zu funktionieren scheint, scheint es vernünftiges ans ein DoubleVar zu machen , dh:

self.ans = DoubleVar() 
Verwandte Themen