在函数中调用参数

时间:2018-08-16 17:55:22

标签: python tkinter

我在def res(rez)处的函数print('hp')上遇到问题 我收到此错误

  

未定义名称“ hp”。

我该如何解决?

enter code here
from tkinter import*
from tkinter import messagebox
root=Tk()
root.title('autosalloni i shpejt')
root.geometry('250x650+0+0')

global parat

def res(rez):
    global hp
    results=Toplevel()
    results.geometry('1000x1000')
    results.title=('Makina e Zgjedhur')
    #this the row that displays the error
    print(hp)

    if (vlera==listaeshteteve[0] and hp=='Volkswagen'and liter=='100' and 
    gasoline==1.0 and pull=='Nafte' and var==1 and listaecvar==1 and 
    para==1600):
        print(para)
        results.mainloop()


def zgjedhjaehtetit(vlera):
    global hp

    def cmimi(parat):
        buxheti=Toplevel()
        buxheti.title('Buxheti')
        buxheti.geometry('100x100-150+0')
        labelaebuxhetit=Label(buxheti,text='Buxheti')
        labelaebuxhetit.place(x=0,y=0)
        entryebuxhetit=Entry(buxheti)
        entryebuxhetit.place(x=0,y=25)
        def rezultati():

            para=entryebuxhetit.get()
            para=int(para)


        btnibuxhetit=Button(buxheti,text='Save',command=rezultati)
        btnibuxhetit.place(x=0,y=50)
        btnirez=Button(buxheti,text='rezultati')
        btnirez.place(x=0,y=76)
        btnirez.config(command=lambda 
        rezultatibe=btnirez.cget('text'):res(rezultatibe))
        buxheti.mainloop()


    def preferencat(preferences):
        preferencatpersonale=Toplevel()
        preferencatpersonale.title('Preferencat personale')
        preferencatpersonale.geometry('1x100-150+0')
        listaecvar=['cvar1','cvar2','cvar3',]
        listaetxttecheckbtn=['ac','km=0','rexhistrim']
        lblepreferncavepersonale=Label(preferencatpersonale, 
        text='preferencatpersonale')
        lblepreferncavepersonale.place(x=0,y=0)
        ypos=25
        for i in range(0,len(listaecvar)):
            listaecvar[i]=IntVar()
        checkbtuttonat=Checkbutton(preferencatpersonale, 
        text=listaetxttecheckbtn[i],variable=         
        listaecvar[i],onvalue=1,offvalue=0)
        checkbtuttonat.place(x=0,y=ypos)
        checkbtuttonat.config(command=lambda 
        shuma=checkbtuttonat.cget('text'):cmimi(checkbtuttonat))
        ypos+=25

        preferencatpersonale.mainloop()

    def terheqja(pull):
        terheqja=Toplevel()
        terheqja.title('Terheqja')
        terheqja.geometry('1x100-150+0')

        lbletwerheqjes=Label(terheqja,text='Terheqja')
        lbletwerheqjes.place(x=0,y=0)
        ypos=25

        var=IntVar()
        radiobutonat=Radiobutton(terheqja,text='2 front',variable=var,value=1)
        radiobutonat.place(x=0,y=25)
        radiobutonat.config(command=lambda personale=radiobutonat.cget('text'):preferencat(personale))
        radiobutonat=Radiobutton(terheqja,text='2 back',variable=var,value=2)
        radiobutonat.place(x=0,y=50)
        radiobutonat.config(command=lambda personale=radiobutonat.cget('text'):preferencat(personale))
        radiobutonat=Radiobutton(terheqja,text='all 4',variable=var,value=3)
        radiobutonat.place(x=0,y=75)
        radiobutonat.config(command=lambda personale=radiobutonat.cget('text'):preferencat(personale))

        terheqja.mainloop()
    def fuel(gasoline):
        karburanti=Toplevel()
        karburanti.title('karburanti')
        karburanti.geometry('1x100-150+0')
        gasoline=float(gasoline)
        listaekarburantit=['Benzine','Nafte','Gaz Natyror']
        lblekarburantit=Label(karburanti,text='Karburanti')
        lblekarburantit.place(x=0,y=0)
        ypos=25
        for i in range(0,len(listaekarburantit)):
            butonatekarburantit=Button(karburanti,text=listaekarburantit[i])
            butonatekarburantit.place(x=0,y=ypos)
            butonatekarburantit.config(command=lambda toruque=butonatekarburantit.cget('text'):terheqja(toruque))
            ypos+=25
        karburanti.mainloop()

    def kubikazha(liter):
        Kubikazha=Toplevel()
        Kubikazha.title('Kubikazha')
        Kubikazha.geometry('1x225-200+0')
        liter=int(liter)
        listaekubikazhave=['1.0','2.0','3.0','4.0','5.0','6.0','7.0','8.0']
        lblcc=Label(Kubikazha,text='CC')
        lblcc.place(x=50,y=0)
        ypos=25
        for i in range(0,len(listaekubikazhave)):
            butonatecc=Button(Kubikazha,text=listaekubikazhave[i])
            butonatecc.place(x=50,y=ypos)
            butonatecc.config(command=lambda kubikazha=butonatecc.cget('text'):fuel(kubikazha))
            ypos+=25
        Kubikazha.mainloop()
    def horsepower(hp):

        print(hp)

        horsepower=Toplevel()
        horsepower.geometry('1x225-250+0')
        horsepower.title('HorsePower')
        listaehp=['100','200','300','400','500','600','700','800']
        lblhp=Label(horsepower,text='HP')
        lblhp.place(x=50,y=0)
        ypos=25
        for i in range(0,len(listaehp)):
            butonatehp=Button(horsepower,text=listaehp[i])
            butonatehp.place(x=50,y=ypos)
            butonatehp.config(command=lambda cc=butonatehp.cget('text'):kubikazha(cc))
            ypos+=25
        horsepower.mainloop()

    if(vlera=='Gjermanez'):
        Veturagjermane=Toplevel()
        Veturagjermane.title('Vetura gjermane')
        Veturagjermane.geometry('1024x270+250+0')
        xpos=125
        listaefirmavegjermane=['Volkswagen','Mercedez-Benz','Audi','BMW']
        listafotovegjermane=['vw.png','mercedes.png','audi.png','BMW.png']
        for i in range(0,len(listaefirmavegjermane)):
            Butonatefirmavegjermane=Button(Veturagjermane ,text=listaefirmavegjermane[i])
            Butonatefirmavegjermane.place(x=xpos,y=0)
            Butonatefirmavegjermane.config(command=lambda ps=Butonatefirmavegjermane.cget('text'):horsepower(ps))
            xpos+=256
        xpos=0
        for i in range(0,len(listafotovegjermane)):
            fototefirmavegjermane=PhotoImage(file=listafotovegjermane[i])

            fototefirmavegjermane1=Label (Veturagjermane,image=fototefirmavegjermane)
            fototefirmavegjermane1.image=fototefirmavegjermane
            fototefirmavegjermane1.place(x=xpos,y=25)
            xpos+=256
            Veturagjermane.mainloop()
    elif(vlera=='Amerikan'):
        Veturaamerikane=Toplevel()
        Veturaamerikane.title('Vetura amerikane')
        Veturaamerikane.geometry('1024x270+250+305')
        xpos=125
        listaefirmaveamerikane=['ford','dodge','chevrolet','cadillac']
        listafotoveamerikane=['ford.png','dodge.png','chevrolet.png','cadillac.png']
        for i in range(0,len(listaefirmaveamerikane)):

            Butonatefirmaveamerikane=Button (Veturaamerikane,text=listaefirmaveamerikane[i])
            Butonatefirmaveamerikane.place(x=xpos,y=0)
            Butonatefirmaveamerikane.config(command=lambda ps=Butonatefirmaveamerikane.cget('text'):horsepower(ps))
            xpos+=256
        xpos=0
        for i in range(0,len(listafotoveamerikane)):
            fototefirmaveamerikane=PhotoImage(file=listafotoveamerikane[i])

            fototefirmaveamerikane1=Label (Veturaamerikane,image=fototefirmaveamerikane)
            fototefirmaveamerikane1.image=fototefirmaveamerikane
            fototefirmaveamerikane1.place(x=xpos,y=25)
            xpos+=256
        Veturaamerikane.mainloop()
    elif(vlera=='Japonez'):
        Veturajaponeze=Toplevel()
        Veturajaponeze.title('Vetura japoneze')
        Veturajaponeze.geometry('1024x270+250+610')
        xpos=125
        listaefirmavejaponeze=['nissan','toyota','mitsubishi','honda']
        listafotovejaponeze=['nissan.png','toyota.png','mitsubishi.png','honda.png']
        for i in range(0,len(listaefirmavejaponeze)):
            Butonatefirmavejaponeze=Button (Veturajaponeze,text=listaefirmavejaponeze[i])
            Butonatefirmavejaponeze.place(x=xpos,y=0)
            Butonatefirmavejaponeze.config(command=lambda ps=Butonatefirmavejaponeze.cget('text'):horsepower(ps))
            xpos+=256
        xpos=0
        for i in range(0,len(listafotovejaponeze)):
            fototefirmavejaponeze=PhotoImage(file=listafotovejaponeze[i])
            fototefirmavejaponeze1=Label (Veturajaponeze,image=fototefirmavejaponeze)
            fototefirmavejaponeze1.image=fototefirmavejaponeze
            fototefirmavejaponeze1.place(x=xpos,y=25)
            xpos+=256
        Veturajaponeze.mainloop()
    elif(vlera=='Britanez'):
        Veturabritaneze=Toplevel()
        Veturabritaneze.title('Vetura britaneze')
        Veturabritaneze.geometry('1024x270+250+0')
        xpos=125
        listaefirmavebritaneze=['aston martin','mclaren','jaguar','land rover']
        listafotovebritaneze=['aston martin.png','mclaren.png','jaguar.png','land rover.png']
        for i in range(0,len(listaefirmavebritaneze)):
             Butonatefirmavebritaneze=Button (Veturabritaneze,text=listaefirmavebritaneze[i])
             Butonatefirmavebritaneze.place(x=xpos,y=0)
             Butonatefirmavebritaneze.config(command=lambda ps=Butonatefirmavebritaneze.cget('text'):horsepower(ps))
             xpos+=256
         xpos=0
         for i in range(0,len(listafotovebritaneze)):
             fototefirmavebritaneze= PhotoImage (file=listafotovebritaneze[i])
             fototefirmavebritaneze1=Label (Veturabritaneze,image=fototefirmavebritaneze)
             fototefirmavebritaneze1.image=fototefirmavebritaneze
             fototefirmavebritaneze1.place(x=xpos,y=25)
             ypos+=256
         Veturabritaneze.mainloop()

listaeshteteve=['Gjermanez','Amerikan','Japonez','Britanez']
ypos=0
for i in range(0,len(listaeshteteve)):
    butonateshteteve=Button(root,text=listaeshteteve[i])
    butonateshteteve.place(x=0,y=ypos)
    ypos=ypos+150
    butonateshteteve.config(command=lambda 
   vleraesshtetit=butonateshteteve.cget('text'):   
   zgjedhjaehtetit(vleraesshtetit))

listaefotovoteshteteve=['german.png','usa.png','japan.png','uk.png']
ypos=25
for i in range(0,len(listaefotovoteshteteve)):
    fototeshteteve=PhotoImage(file=listaefotovoteshteteve[i])
    fototeshteteve1=Label(root,image=fototeshteteve)
    fototeshteteve1.image=fototeshteteve
    fototeshteteve1.place(x=0,y=ypos)
    ypos+=150

root.mainloop()

1 个答案:

答案 0 :(得分:0)

在打印hp之前,必须为其指定一个值。

def new():
    global x
    print(x)

new()

如果运行上面的代码将得到错误: NameError:名称“ x”未定义

但是如果我运行以下代码:

def new():
    global x
    print(x)

x = 4
new()

将得到结果为:4