我需要使用我制作的应用程序从BMI计算中收集和保存数据,但我不知道该怎么做。我找到了一些代码,它可以满足我的需求,但它只是
from Tkinter import *
raiz = Tk()
frame = Frame(raiz)
out = []
def cadastro():
form = Toplevel(raiz)
Label(form, text='Name: ').grid(column=0, row=0, sticky=E)
Label(form, text='Phone number: ').grid(column=0, row=1, sticky=E)
nome = StringVar()
celular = StringVar()
a=Entry(form, textvariable=nome, width=15)
a.grid(column=1, row=0, sticky=W)
Entry(form, textvariable=celular, width=15).grid(column=1, row=1, sticky=W)
def onCancel():
form.destroy()
def onOk():
with open('outt.txt','w') as txt:
txt.write('Name : ' + str(nome.get()) + ' ' + 'Telephone No. : ' + str(celular.get()))
onCancel()
Button(form, text='OK', command=onOk).grid(column=0, row=2, sticky=E)
Button(form, text='Cancel', command=onCancel).grid(column=1, row=2, sticky=W)
def listar():
with open('outt.txt','r') as txt_read:
print txt_read.read()
w = Button(raiz, text='Cadastrar',command=cadastro).grid()
x = Button(raiz, text='Listar' , command=listar).grid()
raiz.mainloop()
那是为了收集无法正常工作的数据
那就是计算器代码
from tkinter import*
class mFrame(Frame):
def __init__(self, master=None, labeltext=''):
Frame.__init__(self,master)
self.grid()
self.label=Label(master, text=labeltext, width=30, bg="#fb5250", fg="#fee56c", font='Arial 12')
self.label.grid()
self.text=StringVar()
self.entry=Entry(master, bd=5, textvariable=self.text)
self.entry.grid()
class Application(Frame):
def __init__(self,master=None):
Frame.__init__(self,master)
self.grid()
self.B2=Button (text='Metric', width=15,bg="#32a2ad", activebackground="#00525a")
self.B2['command']=self.poop
self.B2.grid()
self.L1=mFrame(master,"Name:")
self.L2=mFrame(master, "Height(Metres/Inches):")
self.L3=mFrame(master, "Weight(Kilos/Pounds):")
self.B1=Button( text='Calculate', width=20, bg="#32a2ad", activebackground="#00525a")
self.B1['command']=self.action
self.B1.grid()
def poop(self):
if self.B2['text'] == "Metric":
self.B2['text'] = "Imperial"
else:
self.B2['text'] = "Metric"
def action(self):
c= Toplevel(root)
c.title("BMI-Result")
c.geometry('270x160+230+130')
name=self.L1.text.get()
if not self.L1.text.get():
Label(c, text='Unacceptable name!',font='Arial 10').pack(side=TOP)
else:
pass
height=self.L2.text.get()
if not self.L2.text.get():
Label(c, text='Write the information in this format "x.xx"',font='Arial 10').pack(side=TOP)
elif not height.isdigit():
try:
height=float(self.L2.text.get())
except ValueError:
Label(c, text='Enter height as a number!',font='Arial 10').pack(side=TOP)
else:
height=float(self.L2.text.get())
pass
weight=self.L3.text.get()
if not self.L3.text.get():
Label(c, text='Write the information in this format "x.xx"',font='Arial 10').pack(side=TOP)
elif not weight.isdigit():
Label(c, text='Enter weight as a number!',font='Arial 10').pack(side=TOP)
else:
weight=float(self.L3.text.get())
pass
if self.B2['text'] == "Metric":
bmi=round(float(weight)/(float(height)**2),2)
Label(c, text=['Hello', name,],font='Arial 10').pack(side=TOP)
Label(c, text='Your Body-Mass-Index:').pack(side=TOP)
Label(c, text=(bmi),font='Arial 10').pack(side=TOP)
else:
bmi=round(float(weight/2.20462)/(float(height/39.3701)**2),2)
Label(c, text=['Hello', name,],font='Arial 10').pack(side=TOP)
Label(c, text='Your Body-Mass-Index:').pack(side=TOP)
Label(c, text=(bmi),font='Arial 10').pack(side=TOP)
if bmi>=25:
words=('You are over weight')
color="#fb5250"
if bmi>=30:
words=('You are obese')
color="#dc3c3a"
if bmi<=18.5:
words=('You are underweight')
color="#b9a797"
if bmi >=18.5 and bmi <= 25:
words=('Your BMI is good')
color="#2ca947"
Label(c, text=(words), bg=color, font='Arial 10').pack(side=TOP)
root=Tk()
root.title("BMI")
root.configure(background="#fb5250")
app=Application(master=root)
app.mainloop()
哪种方法有效,我只需要收集数据。
感谢您的建议
答案 0 :(得分:1)
你可以使用泡菜。 https://docs.python.org/2/library/pickle.html。 您需要做的就是
import pickle
pickle.dump(obj, file)
并且
pickle.load(file)
一个是作为变量存储的数据,文件很好,你的文件。