2

I am an electrical engineer and I am new to Python programming.

I want to write a Python program for three phase transformer calculator. The program is very simple, it does just a few algebraic operation; I wrote simple python code without GUI and it worked very fine. So I decided to make the same program using Tkinter module to give a GUI to my application. I had some error and I read a lot of questions and asked many questions on this community, but I am not able to solve it. I am not able to trace the cause of the error. The code is:

from tkinter import *
from math import *


finestra=Tk()
finestra.geometry('800x800+300+300')
finestra.title('Dimensionamento Trasformatore')


def calcola(Vn,Vn2,Sn,Vcc,V_spira,f,Bmax,mu,J,w,Snf,N1,N2,If1,If2,kv,ki,fi,fi_c,S_colonna,S_conduttore1,S_conduttore2,Sezione_netta_cu1,Sezione_netta_cu2,Vf1,Vf2):

    try:


       #lettura caselle di testo/ read entry
                Vn=float(Vn_s.get())
                Vn2=float(Vn2_s.get())
                Vcc=float(Vcc_s.get())
                V_spira=float(Vspira_s.get())
                Sn=float(Sn_s.get())
                J=float(J_s.get())
                mu=float(mu_s.get())
                Bmax=float(Bmax_s.get())
                f=float(f_s.get())

    except ValueError:
                print('inserito valore sbagliato')
    else:



        #calcoli / calculate
                if (var_1.get()==1):
                      collegamento1='triangolo'
                else:
                      collegamento1='stella'
                if (var_2.get()==1):
                      collegamento2='triangolo'
                else:
                      collegamento2='stella'


                Snf=(Sn/3.0)
                w=(2*pi*f)
                if (collegamento1=='triangolo'):
                            Vf1=Vn
                else:
                            Vf1=(Vn/sqrt(3))
                if (collegamento2=='triangolo'):
                            Vf2=(Vn2)
                else:
                            Vf2=(Vn2/sqrt(3))
                N1=Vf1/V_spira
                N2=Vf2/V_spira
                If1=Snf/Vf1
                If2=(Snf/Vf2)
                kv=Vf1/Vf2
                ki=If2/If1
                fi=Vf1/(w*N1)
                fi_c=(N1*fi)
                S_colonna=(fi_c/(Bmax*sqrt(2)))
                S_conduttore1=(If1/J)
                S_conduttore2=(If2/J)
        #        Sezione_netta_cu1.set(S_conduttore1*N1/k_stip_cu)
        #        Sezione_netta_cu2.set(S_conduttore2*N2/k_stip_cu)

                testo_23=Label(finestra,text=str(N1)).grid(sticky=W,row=4,column=5)
                testo_24=Label(finestra,text=str(N2)).grid(sticky=W,row=6,column=5)
                testo_25=Label(finestra,text=str(kv)).grid(sticky=W,row=11,column=5)
                testo_26=Label(finestra,text=str(ki)).grid(sticky=W,row=13,column=5)
                testo_27=Label(finestra,text=str(fi_c)).grid(sticky=W,row=21,column=5)
                testo_28=Label(finestra,text=str(S_colonna)).grid(sticky=W,row=25,column=5)
                testo_29=Label(finestra,text=str(S_conduttore1)).grid(sticky=W,row=19,column=5)
                testo_30=Label(finestra,text=str(S_conduttore2)).grid(sticky=W,row=17,column=5)
##                testo_31=Label(finestra,text=str(Sezione_netta_cu1)).grid(sticky=W,row=16,column=5)
##                testo_32=Label(finestra,text=str(Sezione_netta_cu2)).grid(sticky=W,row=8,column=5)
##                testo_33=Label(finestra,text=str(N1)).grid(sticky=W,row=14,column=5)
##                testo_34=Label(finestra,text=str(N1)).grid(sticky=W,row=22,column=5)





                return; 







#Testi / label
testo_0=Label(finestra,text="Parametri di ingresso:").grid(sticky=W,row=0,column=0)
testo_1=Label(finestra,text="Collegamento primario:").grid(sticky=W,row=3,column=0)
testo_2=Label(finestra,text="Collegamento secondario:").grid(sticky=W,row=5,column=0)
testo_3=Label(finestra,text="Tensione nominale concatenata primaria:").grid(sticky=W,row=10,column=0)
testo_4=Label(finestra,text="Tensione nominale concatenata secondaria:").grid(sticky=W,row=12,column=0)
testo_5=Label(finestra,text="Induzione massima:").grid(sticky=W,row=20,column=0)
testo_6=Label(finestra,text="Densita di corrente:").grid(sticky=W,row=24,column=0)
testo_7=Label(finestra,text="Frequenza:").grid(sticky=W,row=18,column=0)
testo_8=Label(finestra,text="Tensione di corto circuito:").grid(sticky=W,row=16,column=0)
testo_9=Label(finestra,text="Potenza apparente nominale:").grid(sticky=W,row=8,column=0)
testo_10=Label(finestra,text="Volt-spira:").grid(sticky=W,row=14,column=0)
testo_11=Label(finestra,text="Permeabilita del ferro:").grid(sticky=W,row=22,column=0)
testo_12=Label(finestra,text="Valori calcolati:").grid(sticky=W,row=0,column=5)
testo_13=Label(finestra,text="Numero spire primario:").grid(sticky=W,row=3,column=5)
testo_14=Label(finestra,text="Numero spire secondario:").grid(sticky=W,row=5,column=5)
testo_15=Label(finestra,text="Rapporto trasformazione tensione:").grid(sticky=W,row=10,column=5)
testo_16=Label(finestra,text="Rapporto trasformazione corrente:").grid(sticky=W,row=12,column=5)
testo_17=Label(finestra,text="Flusso concatenato efficace:").grid(sticky=W,row=20,column=5)
testo_18=Label(finestra,text="Sezione colonna:").grid(sticky=W,row=24,column=5)
testo_19=Label(finestra,text="Sezione conduttore primario:").grid(sticky=W,row=18,column=5)
testo_20=Label(finestra,text="Sezione conduttore secondario:").grid(sticky=W,row=16,column=5)
testo_21=Label(finestra,text="Sezione avvolgimento primario netta:").grid(sticky=W,row=8,column=5)
testo_22=Label(finestra,text="Sezione avvolgimento secondario netta:").grid(sticky=W,row=14,column=5)





#variabili
If1=DoubleVar()
If2=DoubleVar()
N1=DoubleVar()
N2=DoubleVar()
var_1=IntVar()
var_2=IntVar()
Vn=DoubleVar()
Vf1=DoubleVar()
Vf2=DoubleVar()
Vn2=DoubleVar()
Vcc=DoubleVar()
V_spira=DoubleVar()
Sn=DoubleVar()
Snf=DoubleVar()
J=DoubleVar()
mu=DoubleVar()
Bmax=DoubleVar()
f=DoubleVar()
Vn_s=StringVar()
Vn2_s=StringVar()
Vcc_s=StringVar()
Vspira_s=StringVar()
Sn_s=StringVar()
J_s=StringVar()
mu_s=StringVar()
Bmax_s=StringVar()
f_s=StringVar()
collegamento1=StringVar()
collegamento2=StringVar()
w=DoubleVar()
kv=DoubleVar()
ki=DoubleVar()
fi=DoubleVar()
fi_c=DoubleVar()
S_colonna=DoubleVar()
S_conduttore1=DoubleVar()
S_conduttore2=DoubleVar()
Sezione_netta_cu1=DoubleVar()
Sezione_netta_cu2=DoubleVar()




#Radiobutton
#collegamento primario/ first winding
collegamentoI_1=Radiobutton(finestra,text='triangolo',value=1,variable=var_1)
collegamentoI_1.grid(row=4,column=0)
collegamentoI_2=Radiobutton(finestra,text='stella',value=2,variable=var_1)
collegamentoI_2.grid(row=4,column=1)
#collegamento secondario/ second winding
collegamentoII_1=Radiobutton(finestra,text='triangolo',value=1,variable=var_2)
collegamentoII_1.grid(row=6,column=0)
collegamentoII_2=Radiobutton(finestra,text='stella',value=2,variable=var_2)
collegamentoII_2.grid(row=6,column=1)





#caselle di testo / entry
Vn_=Entry(finestra,textvariable=Vn_s)
Vn_.grid(row=11,column=0)
Vspira_=Entry(finestra,textvariable=Vspira_s)
Vspira_.grid(row=15,column=0)
Vn2_=Entry(finestra,textvariable=Vn2_s)
Vn2_.grid(row=13,column=0)
Sn_=Entry(finestra,textvariable=Sn_s)
Sn_.grid(row=9,column=0)
Bmax_=Entry(finestra,textvariable=Bmax_s)
Bmax_.grid(row=21,column=0)
mu_=Entry(finestra,textvariable=mu_s)
mu_.grid(row=23,column=0)
Vcc_=Entry(finestra,textvariable=Vcc_s)
Vcc_.grid(row=17,column=0)
f_=Entry(finestra,textvariable=f_s)
f_.grid(row=19,column=0)
J_=Entry(finestra,textvariable=J_s)
J_.grid(row=25,column=0)


#Calculatebutton
gobutton=Button(finestra,text='Calcola',command=calcola(Vn,Vn2,Sn,Vcc,V_spira,f,Bmax,mu,J,w,Snf,N1,N2,If1,If2,kv,ki,fi,fi_c,S_colonna,S_conduttore1,S_conduttore2,Sezione_netta_cu1,Sezione_netta_cu2,Vf1,Vf2))
gobutton.grid(row=28, column=3)




finestra.mainloop()

The first kind of error I had was a ValueError: could not convert string to float. I read this could happen because at the start time, the entry widgets are empty so python can convert it to float. So I added try/except block. Now when I start the program, it prints an error message in except block (I can't understand this: the calcola function is associated to the calculate button, but it seems to run the function without button press, at start time) then don't happen anything else, though writing number in entry box and pressing calculate button. What I suspect is that the way I am using function is wrong (syntax or something else). If anyone can help me, I'd really appreciate.

Sorry for bad English. Thank you very much. Nicola

Jared Goguen
  • 8,772
  • 2
  • 18
  • 36
  • se non capisci bene la mia risposta scrivi qui sotto in italiano. – Liam May 16 '16 at 11:30
  • Ciao Liam, ti ringrazio infinitamente per la tua risposta. Se posso approfittare della tua gentilezza ti vorrei chiedere delle spiegazioni. Dunque prima di tutto ho corretto il codice e funziona perfettamente, però non capisco lamba esattamente a cosa serva. Ho notato che ora non mi da l'errore appena faccio girare il programma (come se andasse ad eseguire la funzione come parte dello script e non come appunto funzione che deve essere richiamata per l'esecuzione). L'errore era solo quello? Grazie ancora stavo impazzendo. – Nicola Barlabà May 16 '16 at 16:35
  • Mi spiego meglio: (premettendo che so programmare, ma non a livello pro, in C e matlab e pochissimo fortran) dalla mia esperienza una function non dovrebbe essere eseguita ad inizio codice. Sto cercando in rete delle spiegazioni ma francamente non mi è molto chiaro il funzionamento. – Nicola Barlabà May 16 '16 at 16:46
  • Ti ringrazio infinitamente Liam, non lo avrei mai capito da solo – Nicola Barlabà May 16 '16 at 16:47

1 Answers1

0

when passing parameters through your function if it is called by a tkinter button you should always use the lambda statement like this: Button(finestra,text='Calcola',command=lambda: calcola(paramaters..) or the function will be called just once when the program starts.

your code:

from tkinter import *
from math import *


finestra=Tk()
finestra.geometry('800x800+300+300')
finestra.title('Dimensionamento Trasformatore')


def calcola(Vn,Vn2,Sn,Vcc,V_spira,f,Bmax,mu,J,w,Snf,N1,N2,If1,If2,kv,ki,fi,fi_c,S_colonna,S_conduttore1,S_conduttore2,Sezione_netta_cu1,Sezione_netta_cu2,Vf1,Vf2):

    try:


       #lettura caselle di testo/ read entry
                Vn=float(Vn_s.get())
                Vn2=float(Vn2_s.get())
                Vcc=float(Vcc_s.get())
                V_spira=float(Vspira_s.get())
                Sn=float(Sn_s.get())
                J=float(J_s.get())
                mu=float(mu_s.get())
                Bmax=float(Bmax_s.get())
                f=float(f_s.get())

    except ValueError:
                print('inserito valore sbagliato')
    else:



        #calcoli / calculate
                if (var_1.get()==1):
                      collegamento1='triangolo'
                else:
                      collegamento1='stella'
                if (var_2.get()==1):
                      collegamento2='triangolo'
                else:
                      collegamento2='stella'


                Snf=(Sn/3.0)
                w=(2*pi*f)
                if (collegamento1=='triangolo'):
                            Vf1=Vn
                else:
                            Vf1=(Vn/sqrt(3))
                if (collegamento2=='triangolo'):
                            Vf2=(Vn2)
                else:
                            Vf2=(Vn2/sqrt(3))
                N1=Vf1/V_spira
                N2=Vf2/V_spira
                If1=Snf/Vf1
                If2=(Snf/Vf2)
                kv=Vf1/Vf2
                ki=If2/If1
                fi=Vf1/(w*N1)
                fi_c=(N1*fi)
                S_colonna=(fi_c/(Bmax*sqrt(2)))
                S_conduttore1=(If1/J)
                S_conduttore2=(If2/J)
        #        Sezione_netta_cu1.set(S_conduttore1*N1/k_stip_cu)
        #        Sezione_netta_cu2.set(S_conduttore2*N2/k_stip_cu)

                testo_23=Label(finestra,text=str(N1)).grid(sticky=W,row=4,column=5)
                testo_24=Label(finestra,text=str(N2)).grid(sticky=W,row=6,column=5)
                testo_25=Label(finestra,text=str(kv)).grid(sticky=W,row=11,column=5)
                testo_26=Label(finestra,text=str(ki)).grid(sticky=W,row=13,column=5)
                testo_27=Label(finestra,text=str(fi_c)).grid(sticky=W,row=21,column=5)
                testo_28=Label(finestra,text=str(S_colonna)).grid(sticky=W,row=25,column=5)
                testo_29=Label(finestra,text=str(S_conduttore1)).grid(sticky=W,row=19,column=5)
                testo_30=Label(finestra,text=str(S_conduttore2)).grid(sticky=W,row=17,column=5)
##                testo_31=Label(finestra,text=str(Sezione_netta_cu1)).grid(sticky=W,row=16,column=5)
##                testo_32=Label(finestra,text=str(Sezione_netta_cu2)).grid(sticky=W,row=8,column=5)
##                testo_33=Label(finestra,text=str(N1)).grid(sticky=W,row=14,column=5)
##                testo_34=Label(finestra,text=str(N1)).grid(sticky=W,row=22,column=5)





                return; 







#Testi / label
testo_0=Label(finestra,text="Parametri di ingresso:").grid(sticky=W,row=0,column=0)
testo_1=Label(finestra,text="Collegamento primario:").grid(sticky=W,row=3,column=0)
testo_2=Label(finestra,text="Collegamento secondario:").grid(sticky=W,row=5,column=0)
testo_3=Label(finestra,text="Tensione nominale concatenata primaria:").grid(sticky=W,row=10,column=0)
testo_4=Label(finestra,text="Tensione nominale concatenata secondaria:").grid(sticky=W,row=12,column=0)
testo_5=Label(finestra,text="Induzione massima:").grid(sticky=W,row=20,column=0)
testo_6=Label(finestra,text="Densita di corrente:").grid(sticky=W,row=24,column=0)
testo_7=Label(finestra,text="Frequenza:").grid(sticky=W,row=18,column=0)
testo_8=Label(finestra,text="Tensione di corto circuito:").grid(sticky=W,row=16,column=0)
testo_9=Label(finestra,text="Potenza apparente nominale:").grid(sticky=W,row=8,column=0)
testo_10=Label(finestra,text="Volt-spira:").grid(sticky=W,row=14,column=0)
testo_11=Label(finestra,text="Permeabilita del ferro:").grid(sticky=W,row=22,column=0)
testo_12=Label(finestra,text="Valori calcolati:").grid(sticky=W,row=0,column=5)
testo_13=Label(finestra,text="Numero spire primario:").grid(sticky=W,row=3,column=5)
testo_14=Label(finestra,text="Numero spire secondario:").grid(sticky=W,row=5,column=5)
testo_15=Label(finestra,text="Rapporto trasformazione tensione:").grid(sticky=W,row=10,column=5)
testo_16=Label(finestra,text="Rapporto trasformazione corrente:").grid(sticky=W,row=12,column=5)
testo_17=Label(finestra,text="Flusso concatenato efficace:").grid(sticky=W,row=20,column=5)
testo_18=Label(finestra,text="Sezione colonna:").grid(sticky=W,row=24,column=5)
testo_19=Label(finestra,text="Sezione conduttore primario:").grid(sticky=W,row=18,column=5)
testo_20=Label(finestra,text="Sezione conduttore secondario:").grid(sticky=W,row=16,column=5)
testo_21=Label(finestra,text="Sezione avvolgimento primario netta:").grid(sticky=W,row=8,column=5)
testo_22=Label(finestra,text="Sezione avvolgimento secondario netta:").grid(sticky=W,row=14,column=5)





#variabili
If1=DoubleVar()
If2=DoubleVar()
N1=DoubleVar()
N2=DoubleVar()
var_1=IntVar()
var_2=IntVar()
Vn=DoubleVar()
Vf1=DoubleVar()
Vf2=DoubleVar()
Vn2=DoubleVar()
Vcc=DoubleVar()
V_spira=DoubleVar()
Sn=DoubleVar()
Snf=DoubleVar()
J=DoubleVar()
mu=DoubleVar()
Bmax=DoubleVar()
f=DoubleVar()
Vn_s=StringVar()
Vn2_s=StringVar()
Vcc_s=StringVar()
Vspira_s=StringVar()
Sn_s=StringVar()
J_s=StringVar()
mu_s=StringVar()
Bmax_s=StringVar()
f_s=StringVar()
collegamento1=StringVar()
collegamento2=StringVar()
w=DoubleVar()
kv=DoubleVar()
ki=DoubleVar()
fi=DoubleVar()
fi_c=DoubleVar()
S_colonna=DoubleVar()
S_conduttore1=DoubleVar()
S_conduttore2=DoubleVar()
Sezione_netta_cu1=DoubleVar()
Sezione_netta_cu2=DoubleVar()




#Radiobutton
#collegamento primario/ first winding
collegamentoI_1=Radiobutton(finestra,text='triangolo',value=1,variable=var_1)
collegamentoI_1.grid(row=4,column=0)
collegamentoI_2=Radiobutton(finestra,text='stella',value=2,variable=var_1)
collegamentoI_2.grid(row=4,column=1)
#collegamento secondario/ second winding
collegamentoII_1=Radiobutton(finestra,text='triangolo',value=1,variable=var_2)
collegamentoII_1.grid(row=6,column=0)
collegamentoII_2=Radiobutton(finestra,text='stella',value=2,variable=var_2)
collegamentoII_2.grid(row=6,column=1)





#caselle di testo / entry
Vn_=Entry(finestra,textvariable=Vn_s)
Vn_.grid(row=11,column=0)
Vspira_=Entry(finestra,textvariable=Vspira_s)
Vspira_.grid(row=15,column=0)
Vn2_=Entry(finestra,textvariable=Vn2_s)
Vn2_.grid(row=13,column=0)
Sn_=Entry(finestra,textvariable=Sn_s)
Sn_.grid(row=9,column=0)
Bmax_=Entry(finestra,textvariable=Bmax_s)
Bmax_.grid(row=21,column=0)
mu_=Entry(finestra,textvariable=mu_s)
mu_.grid(row=23,column=0)
Vcc_=Entry(finestra,textvariable=Vcc_s)
Vcc_.grid(row=17,column=0)
f_=Entry(finestra,textvariable=f_s)
f_.grid(row=19,column=0)
J_=Entry(finestra,textvariable=J_s)
J_.grid(row=25,column=0)


#Calculatebutton
gobutton=Button(finestra,text='Calcola',command=lambda: calcola(Vn,Vn2,Sn,Vcc,V_spira,f,Bmax,mu,J,w,Snf,N1,N2,If1,If2,kv,ki,fi,fi_c,S_colonna,S_conduttore1,S_conduttore2,Sezione_netta_cu1,Sezione_netta_cu2,Vf1,Vf2))
gobutton.grid(row=28, column=3)




finestra.mainloop()
Liam
  • 6,009
  • 4
  • 39
  • 53