如何在函数外使用此变量?

时间:2016-10-23 11:56:10

标签: function python-3.x variables scope

Three_Series = ["3S BLACK STOCK RIMS.png"]

def SelectedCarFunction(event=None):
    Model = Var2.get()
    Colour = Var3.get()
    Wheels = Var4.get()

变量ModelColourWheels完全检索,因为它们应该是

if Model == "3 Series" and Colour == "Black" and Wheels == "Stock":
    VarSC = Three_Series[0]
    print(VarSC)

我将print(VarSC)放在此处进行测试,看看它是否会打印"3S BLACK STOCK RIMS.png"并且确实

   return VarSC

VarSC = StringVar()

DisplayFrame.grid_propagate(False)

print(VarSC)

然而,现在当我print(VarSC)查看它是否打印出与之前相同的值时,相反它打印出PY_VAR9

CarImage = PhotoImage(file=VarSC)

这里我想将文件分配给VarSC,即我希望VarSC等于"3S BLACK STOCK RIMS.png",但它不是

CarImageLabel = Label(DisplayFrame, image=CarImage)
CarImageLabel.grid(row=0, column=0)

root.mainloop()

以下是整个代码:

Three_Series = ["3S BLACK STOCK RIMS.png"]

def SelectedCarFunction(event=None):
    Model = Var2.get()
    Colour = Var3.get()
    Wheels = Var4.get()

    if Model == "3 Series" and Colour == "Black" and Wheels == "Stock":
    VarSC = Three_Series[0]
    print(VarSC)
    return VarSC

VarSC = StringVar()

DisplayFrame.grid_propagate(False)

print(VarSC)
CarImage = PhotoImage(file=VarSC)
CarImageLabel = Label(DisplayFrame, image=CarImage)
CarImageLabel.grid(row=0, column=0)

root.mainloop()

0 个答案:

没有答案