2016-05-16 11 views
2

私は電気技術者です。私はPythonプログラミングには初めてです。Tkinterエラータイプ変数

3相変圧器の計算機用のPythonプログラムを作成したいと思います。このプログラムは非常に簡単で、数回の代数演算しか行いません。私はGUIなしでシンプルなPythonコードを書いて、それは非常にうまくいった。そこで私はTkinterモジュールを使って同じプログラムを作ってアプリケーションにGUIを与えることにしました。私はいくつかのエラーがあり、多くの質問を読んでこのコミュニティについて多くの質問をしましたが、解決できません。私はエラーの原因を追跡することができません。コードは

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() 

でした。これは、開始時にエントリウィジェットが空であるため、pythonがfloatに変換できるため、これが起こる可能性があります。そこでtry/exceptブロックを追加しました。今私はプログラムを起動すると、ブロックを除いてエラーメッセージを表示します(これは理解できません:calcola関数は計算ボタンに関連付けられていますが、開始時にボタンを押さずに関数を実行しているようです)入力欄に数字を書いて計算ボタンを押しても何も起こらない。私が疑うことは、私が関数を使用している方法が間違っているということです(構文や他の何か)。誰かが私を助けることができれば、本当に感謝します。

申し訳ありません。どうもありがとうございました。 ニコラ

+0

SE非capisciベーネ・ラ・ミアrisposta scrivi QUIソットでイタリア語。 – Veltro

+0

Ciao Liam、ti ringrazio infinitamente per la tua risposta。それはあなたの恋人の恋人を魅了します。それ以外の場合は、それ以外の場合は無効となります。あなたはプログラムの一部として、あなたが他の人のスクリプトを使っているかどうかを調べる必要があります。 L'errore era solo quello? Grazie ancora stavo impazzendo。 –

+0

私の知る限りでは:(前の日のように、私はそれ以外のプロフェッショナルでもあります)モンテカルロ・キアロ・キング・オブ・ファンデーションでは、 –

答えて

0

あなたが常にこのようlambdaステートメントを使用する必要がありTkinterのボタンで呼び出された場合、あなたの関数を使用してパラメータを渡す:Button(finestra,text='Calcola',command=lambda: calcola(paramaters..)またはプログラムの起動時に関数が一度だけ呼び出されます。

あなたのコード:

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()