Experimentos com Python Para Técnicos em Eletrônica – Parte X

Para não nos alongarmos (de novo), aqui vai o programa completo onde, em cada linha, existe um comentário (logo depois do ‘#’) explicando sua função:

from tkinter import *                                                 # importa toda a biblioteca tkinter

root = Tk()                                                                 # cria um container  grafico ‘root’

root.title(‘Calculadora LM-555’)                                       # titulo da interface grafica

root.geometry(‘300×230’)                                # tamanho em pixels do container

root.resizable(False, False)                                 # desabilita redimensionamento da janela

font=(‘Helvetica’, 15)                                         # fonte dos textos

def visor(f):                                                                        # funcao que cria visor default

    print(f)

    visor = Label(

                root, text=round(f,2),                                    # Visor digital da calculadora

                justify=LEFT, anchor=’w’,                                           # posiciona visor na esquerda

                font=(“Helvetica”, 20), relief=’ridge’,           # tipo de fonte, tamanho e borda

                background=’yellow’, borderwidth=’1′         # fundo e espessura do visor

    )                             

    visor.grid(sticky = W, column=0, row=1, padx=50)  # posicionamento do visor

visor(.0000)                                                                        # chama funcao visor default

def frequencia():                                                                # funcao que atualiza visor

                r1 = int(eR1.get())                                             # captura valor R1

                r2 = int(eR2.get())                                             # captura valor R2

                c =  float(eC.get())                                             # captura valor C

                f = (1.44 / ((r1+2*r2) *c))                             # formula da frequencia

                #print(f)

                visor(f*1000)                                                     # mostra valor em hertz no visor

hertz = Label(                                                                     # label ‘Hertz’

                root, text=’Hertz’,

                font=font, justify=LEFT, anchor=’w’

)   

hertz.grid(sticky = W, column=1, row=1)

Label(root, text=”).grid(column=0, row=2)                      # linha 2 vazia

R1 = Label(

                root, text=’R1 (Kohms)’, font=font,               # label ‘R1’

                justify=LEFT, anchor=’w’

)        

R1.grid(sticky = W, column=0, row=3, padx=50)

R2 = Label(

                root, text=’R2 (Kohms)’, font=font,               # label ‘R2’

                justify=LEFT, anchor=’w’

)        

R2.grid(sticky = W, column=0, row=4, padx=50)

C = Label(

                root, text=’C   (uF)’, font=font,                       # label ‘C’

                justify=LEFT, anchor=’w’

)           

C.grid(sticky = W, column=0, row=5, padx=50)

eR1 = Entry(root, width=6)                                                # caixa de entrada para R1

eR1.grid(sticky = W, column=1, row=3)      

eR1.focus()                                                                         # foco inicial na caixa eR1

eR2 = Entry(root, width=6)                                                # caixa de entrada para R2

eR2.grid(sticky = W, column=1, row=4)      

eC =  Entry(root, width=6)                                # caixa de entrada para C

eC.grid(sticky = W, column=1, row=5)      

Label(root, text=”).grid(column=0, row=6)                    # linha 6 vazia

Label(root, text=”).grid(column=0, row=0)                      # linha 0 vazia

ok = Button(root, text=’OK’,  width=10, command=frequencia) # cria botao ‘OK’

ok.grid(sticky = W, column=0, row=7, padx=50)

sair = Button(root, text=’Sair’, command=root.quit)  # cria botao ‘Sair’

sair.grid(sticky = W, column=1, row=7)

root.mainloop()                                                                  # executa continuamente o script

Deixe um comentário