jeudi 30 novembre 2017

Adding a "if" "then" checkbutton

I am new to coding and I wrote some code for a calculator I use to solve some stuff at work.

I am now stuck trying to add check buttons for "feet" & "meters". I want them to integrate into my equation, that way I wont have to convert them manually beforehand. I will add the second equation for feet once I can figure out how to make them toggle between the two.

Here is my code so far.

import Tkinter
import math
from Tkinter import *


Lreq = 105
Dref = 1

class compintapp_tk(Tkinter.Tk):
zdef __init__(self,parent):
    Tkinter.Tk.__init__(self,parent)
    self.parent = parent
    self.initialize()

def initialize(self):
    self.grid()
    self["bg"] = "grey"


    ## Title & subtitle labels ---------------------------------------
    titlelbl = Tkinter.Label(self, text="Wattage Calculator",
                          anchor="center",fg="black",bg="darkorange2")
    titlelbl.grid(column=0,row=0,columnspan=4,sticky='EW')




    ## Input Labels --------------------------------------------------

    sensitivitylbl = Tkinter.Label(self, text="Loudspeaker sensitivity?",
                          anchor="w",fg="white",bg="SlateGray4")
    sensitivitylbl.grid(column=0,row=2,columnspan=3,sticky='EW')

    distancelbl = Tkinter.Label(self, text="Distance from speaker to listening position?",
                          anchor="w",fg="white",bg="SlateGray4")
    distancelbl.grid(column=0,row=3,columnspan=3,sticky='EW')

    headroomlbl = Tkinter.Label(self, text="How much headroom for amplifier?",
                          anchor="w",fg="white",bg="SlateGray4")
    headroomlbl.grid(column=0,row=4,columnspan=3,sticky='EW')


    wattagelbl = Tkinter.Label(self, text="Total Wattage :",
                          anchor="w",fg="white",bg="SlateGray4")
    wattagelbl.grid(column=0,row=7,columnspan=3,sticky='EW')



    ## end of input labels ------------------------------------------------

    ## Input Boxes --------------------------------------------------------
    #self.sensitivity = Tkinter.DoubleVar()
    #speakersensitivity = Tkinter.Entry(self,textvariable=self.sensitivity)
    #speakersensitivity.grid(column=1,row=2,sticky='EW')

    self.sensitivity = Tkinter.DoubleVar()
    speakersensitivity = Tkinter.Entry(self,textvariable=self.sensitivity)
    speakersensitivity.grid(column=3,row=2,sticky='EW')

    self.distance = Tkinter.DoubleVar()
    spkdistance = Tkinter.Entry(self,textvariable=self.distance)
    spkdistance.grid(column=3,row=3,sticky='EW')

    self.headroom = Tkinter.IntVar()
    spkheadroom = Tkinter.Entry(self,textvariable=self.headroom)
    spkheadroom.grid(column=3,row=4, sticky='EW')


    ## end of input boxes -------------------------------------------------

    ## Button
    button = Tkinter.Button(self,text="C A L C U L A T E",
                            anchor="w",fg="black",bg="green",
                            command=self.OnButtonClick)
    button.grid(column=0,row=6,columnspan=1)




    var = BooleanVar()

    c=Checkbutton(self, text="Meters", variable=BooleanVar(),
                  anchor="center",fg="white",bg="grey25")
    c.grid(column=2,row=6,columnspan=1)

    c=Checkbutton(self, text="Feet", variable=BooleanVar(),
                  anchor="center",fg="white",bg="grey25")
    c.grid(column=3,row=6,columnspan=1)




    # initialize global variables
    self.ckbuttonstatus = BooleanVar()



    ## end of button

    ## Output labels
    self.amt = Tkinter.StringVar()
    amtout = Tkinter.Label(self,textvariable=self.amt,
                          anchor="e",fg="red",bg="gold")
    amtout.grid(column=3,row=7,columnspan=1,sticky='EW')

    ## end of output labels -----------------------------------------------

    self.grid_columnconfigure(0,weight=2)
    self.resizable(0,0)


def OnButtonClick(self):
    Lsens = self.sensitivity.get()
    D2 = self.distance.get()
    HR = self.headroom.get()


    exponent = (Lreq-Lsens+20 * math.log10(D2/1)+HR)/10
    amount = 10 ** exponent
    self.amt.set(amount)

if __name__ == "__main__":
app = compintapp_tk(None)
app.title('Wattage Calculator')
app.mainloop()

I hope you all can help because I am having a really hard time finding anything on this.

Thank you,

DiFino




Aucun commentaire:

Enregistrer un commentaire