tkinter checkbutton改变函数值

时间:2015-07-18 10:43:36

标签: python tkinter

作为个人项目,我正在尝试创建一个程序来计算跑步机跑步过程中燃烧的卡路里。基本程序有效,但我希望用户能够选择他们喜欢的测量单位,例如磅或千克。为此,我认为检查按钮是最合适的,但我真的无法弄清楚如何使所选的检查按钮改变功能。因此,如果用户选择'lbs',我将需要该函数将值除以大约2.2,将其转换为KG。我已经阅读了一些其他问题,我似乎无法连接点。任何帮助将非常感激 :)。我在下面打印了我的代码。

from tkinter import *

#function def calculate():  mass = float(mass1.get())   gradient_q = float(gradient1.get())     gradient = gradient_q/100   minutes = float(minutes1.get())     seconds = float(seconds1.get())     speed = float(speed1.get())     vo2 = 3.5 + (0.2 * (speed * 26.82))+(gradient * (speed * 26.82) * 0.9)  mets = vo2 / 3.5    cal_m = (vo2 * mass)/1000 *
5.05    total_cal = ((seconds/60) + minutes) * cal_m    tcal = Label(window, text='total calories burned:%.2f' % total_cal).grid(row=8,column=1)    return


window = Tk() window.title('Calorie Calculator') window.geometry('400x600')

title=Label(window,text='Treadmill Calorie Counter', font='bold').grid(columnspan=4) title=Label(window,text='').grid(columnspan=2)

mass1 = StringVar() gradient1 = StringVar() minutes1 = StringVar() seconds1 = StringVar() speed1 = StringVar()

var=StringVar()

mass=Label(window,text='Mass').grid(row=2,column=0) mass_e=Entry(window, textvariable=mass1).grid(row=2,column=1) c = Checkbutton (window, text='kg').grid(row=2,column=2) c = Checkbutton (window, text='lbs').grid(row=2, column=3)

gradient=Label(window,text='Gradient').grid(row=3,column=0) gradient_e=Entry(window, textvariable=gradient1).grid(row=3,column=1)

minutes=Label(window,text='Minutes').grid(row=4,column=0) minutes_e=Entry(window, textvariable=minutes1).grid(row=4,column=1)

seconds=Label(window,text='Seconds').grid(row=5,column=0) seconds_e=Entry(window, textvariable=seconds1).grid(row=5,column=1)

speed=Label(window,text='Speed').grid(row=6,column=0) speed_e=Entry(window, textvariable=speed1).grid(row=6,column=1)

c = Checkbutton (window, text='kph',variable=var).grid(row=6,column=2) c = Checkbutton (window, text='mph').grid(row=6, column=3)

calculate = Button(window, text='Calculate',command=calculate).grid(row=7,column=1)

window.mainloop()

2 个答案:

答案 0 :(得分:1)

我编辑了你的代码,现在它将检查检查按钮是否处于活动状态。

我还添加了一个小函数,确保一次只选择一个checkbutton。我总是遇到选择这两个选项的用户的问题。

以下是更新的代码:

from tkinter import *

def calculate():

   #this will check which checkbutton is active
   if cbSpeed[0].get() == 1:
        print "kph"
   else:
        print "mph"

   if cbUnit[0].get() == 1:
        print "kg"
   else:
        print "lbs"

   mass = float(mass1.get()) 
   gradient_q = float(gradient1.get()) 
   gradient = gradient_q/100 
   minutes = float(minutes1.get()) 
   seconds = float(seconds1.get()) 
   speed = float(speed1.get()) 
   vo2 = 3.5 + (0.2 * (speed * 26.82))+(gradient * (speed * 26.82) * 0.9) 
   mets = vo2 / 3.5 
   cal_m = (vo2 * mass)/1000 * 5.05 
   total_cal = ((seconds/60) + minutes) * cal_m 
   tcal = Label(window, text='total calories burned:%.2f' % total_cal).grid(row=8,column=1) 

def changeCB(buttons,no):
#This function will make sure only one checkbutton is selected at a time

    for i in range(0,len(buttons)):

        if i <> no:

            buttons[i].set(0)


window = Tk() 
window.title('Calorie Calculator') 
window.geometry('400x600')

title=Label(window,text='Treadmill Calorie Counter', font='bold').grid(columnspan=4) 
title=Label(window,text='').grid(columnspan=2)

mass1 = StringVar() 
gradient1 = StringVar() 
minutes1 = StringVar() 
seconds1 = StringVar() 
speed1 = StringVar()

var=StringVar()

mass=Label(window,text='Mass').grid(row=2,column=0) 
mass_e=Entry(window, textvariable=mass1).grid(row=2,column=1) 

#the following will create an array with 2 entrys. One entry per checkbutton
cbUnit = []
for i in range(0,2):
    cbUnit.append(0)
    cbUnit[i] = IntVar()

#this will set the first checkbutton as default
cbUnit[0].set(1)

c = Checkbutton (window, text='kg',variable=cbUnit[0],command = lambda: changeCB(cbUnit,0)).grid(row=2,column=2) 
c = Checkbutton (window, text='lbs',variable=cbUnit[1],command = lambda: changeCB(cbUnit,1)).grid(row=2, column=3)

gradient=Label(window,text='Gradient').grid(row=3,column=0) 
gradient_e=Entry(window, textvariable=gradient1).grid(row=3,column=1)

minutes=Label(window,text='Minutes').grid(row=4,column=0) 
minutes_e=Entry(window, textvariable=minutes1).grid(row=4,column=1)

seconds=Label(window,text='Seconds').grid(row=5,column=0) 
seconds_e=Entry(window, textvariable=seconds1).grid(row=5,column=1)

speed=Label(window,text='Speed').grid(row=6,column=0) 
speed_e=Entry(window, textvariable=speed1).grid(row=6,column=1)

#sames as with units
cbSpeed = []
for i in range(0,2):
    cbSpeed.append(0)
    cbSpeed[i] = IntVar()

cbSpeed[0].set(1)
c = Checkbutton (window, text='kph',variable=cbSpeed[0],command = lambda: changeCB(cbSpeed,0)).grid(row=6,column=2) 
c = Checkbutton (window, text='mph',variable=cbSpeed[1],command = lambda: changeCB(cbSpeed,1)).grid(row=6, column=3)


calculate = Button(window, text='Calculate',command=calculate).grid(row=7,column=1)

window.mainloop()

希望这就是你要找的东西。

答案 1 :(得分:0)

简短的回答是,您的止回按钮或单选按钮与变量相关联,无论何时进行计算,都使用该变量的值。此外,按钮可以在单击时调用函数,这样您就可以在选择按钮时重新计算结果。

如果您向用户提供独家选择(lbs或kg),则使用的正确小部件是Radiobutton而不是Checkbutton。您可以通过让radiobuttons共享变量来将它们绑在一起。变量将只具有所选按钮的值。

您问题中的代码几乎无法阅读,因此这是一个通用示例:

def calculate():
    units = weightUnits.get()
    if units == "lbs":
        ...
    else:
        ...

weightUnits = StringVar(value="lbs")
lbsChoice = Radiobutton(..., text="lbs", value="lbs", 
                        variable=weightUnits, command=calculate)
lbsChoice = Radiobutton(..., text="kg", value="kg", 
                        variable=weightUnits, command=calculate)