Tkinter错误类型variab

2024-05-01 22:18:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一名电气工程师,对Python编程还不熟悉。你知道吗

我想为三相变压器计算器写一个Python程序。这个程序非常简单,它只做了一些代数运算;我编写了简单的python代码,没有GUI,它工作得非常好。因此,我决定使用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()

我遇到的第一种错误是ValueError: could not convert string to float。我读到这种情况是因为在开始时,entry小部件是空的,因此python可以将其转换为float。所以我加了try/except block。现在,当我启动程序时,它会在except块中打印一条错误消息(我不明白这一点:calcola函数与calculate按钮相关联,但它似乎在启动时不按按钮就运行该函数),然后就不会发生任何其他事情,尽管在输入框中写入数字并按calculate按钮。我怀疑的是我使用函数的方式是错误的(语法或其他)。如果有人能帮我,我将非常感激。你知道吗

对不起,英语不好。非常感谢你。 尼古拉


Tags: textcolumnlabelgridfirowvnsticky
1条回答
网友
1楼 · 发布于 2024-05-01 22:18:15

当通过函数传递参数时,如果函数是由tkinter按钮调用的,则应始终使用如下lambda语句: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()

相关问题 更多 >