Python 如何修复这个Tkinter按钮错误

Python 如何修复这个Tkinter按钮错误,python,debugging,tkinter,Python,Debugging,Tkinter,每当我运行程序时,按钮都会显示,但是图像不会显示,而是显示为灰色空间。此外,除“完成”按钮外的所有按钮都是不必要的(请参见所附图片) def NoFood(): 总卡路里=NoBreakfast.Carries NoFoodOption=1 def BoiledEggsFunct(): 总卡路里=煮沸的鸡蛋。卡路里+总卡路里 def FriedEggsFunct(): TotalCarries=FriedEggs.carries+TotalCarries def scrambledegsfunc

每当我运行程序时,按钮都会显示,但是图像不会显示,而是显示为灰色空间。此外,除“完成”按钮外的所有按钮都是不必要的(请参见所附图片)

def NoFood():
总卡路里=NoBreakfast.Carries
NoFoodOption=1
def BoiledEggsFunct():
总卡路里=煮沸的鸡蛋。卡路里+总卡路里
def FriedEggsFunct():
TotalCarries=FriedEggs.carries+TotalCarries
def scrambledegsfunct():
总热量=总热量+总热量
def poachedegsgsfunct():
TotalCarries=水煮玉米。卡路里+总卡路里
def ToastFunct():
TotalCarries=烤面包片。Carries+TotalCarries
def BaconFunct():
总卡路里=培根。卡路里+总卡路里
def CerealFunct():
总卡路里=谷物。卡路里+总卡路里
def PorridgeFunct():
总卡路里=粥。卡路里+总卡路里
def FinishScreen():
FinishWindow=Toplevel()
如果总热量>0且总热量<1000:
HealthyLabel=Label(FinishWindow,text=“恭喜你,你很健康!”,font=(“Comic Sans MS”,25),fg=“浅绿色”,
bg=“黑色”)
HealthyLabel.grid(columnspan=10)
elif总热量>1000:
UnhealthyLabel=Label(FinishWindow,text=“尝试明天吃得更健康。参见个性化建议”,font=(“Comic Sans MS”,25),fg=“yellow”,
bg=“黑色”)
非健康标签网格(columnspan=10)
elif NoFoodOption==1:
NoFoodLabel=Label(FinishWindow,text=“不吃东西会对你的健康有害。请参阅个性化建议”,font=(“Comic Sans MS”,25),fg=“红色”,
bg=“黑色”)
NoFoodLabel.grid(columnspan=10)
其他:
错误=标签(FinishWindow,text=“Error”,font=(“Comic Sans MS”,25),fg=“red”,
bg=“黑色”)
NoFoodLabel.grid(columnspan=10)
BoiledEggsPhoto=PhotoImage(file=“BoiledEgg.gif”)
BoiledEggsButton=按钮(FoodSelectionWindow,text=“boiledEggs”,command=BoiledEggsFunct,image=BoiledEggsPhoto,composite=LEFT,高度=100,宽度=200)
网格(列=1,行=3)
scrambledegsgsphoto=PhotoImage(file=“ScrambledEgg.gif”)
ScrampedEGGSButton=按钮(FoodSelectionWindow,text=“炒鸡蛋”,command=ScrampedEGGSFunct,image=ScrampedEGGSPhoto,复合物=左侧,高度=100,宽度=200)
网格(列=2,行=3)
FriedEggsPhoto=PhotoImage(file=“FriedEgg.gif”)
FriedEggsButton=按钮(FoodSelectionWindow,text=“Fried Eggs”,command=FriedEggsFunct,image=FriedEggsPhoto,Composite=LEFT,高度=100,宽度=200)
grid(列=3,行=3)
poachedegsgsphoto=PhotoImage(file=“PoachedEgg.gif”)
PoachedEggsButton=按钮(FoodSelectionWindow,text=“Poachedegs”,command=PoachedEggsFunct,image=PoachedEggsPhoto,化合物=左侧,高度=100,宽度=200)
PoachedEggsButton.grid(列=1,行=4)
ToastPhoto=PhotoImage(file=“Toast.gif”)
ToastButton=按钮(FoodSelectionWindow,text=“Toast”,command=ToastFunct,image=ToastPhoto,composite=LEFT,height=100,width=200)
ToastButton.grid(列=2,行=4)
BaconPhoto=PhotoImage(file=“Bacon.gif”)
BaconButton=按钮(FoodSelectionWindow,text=“Bacon”,command=BaconFunct,image=BaconPhoto,Composite=LEFT,高度=100,宽度=200)
BaconButton.grid(列=3,行=4)
CerealPhoto=PhotoImage(file=“grane.gif”)
谷物按钮=按钮(FoodSelectionWindow,text=“谷物”,command=CerealFunct,image=CerealPhoto,composite=LEFT,height=100,width=200)
CerealButton.grid(列=1,行=5)
PorridgePhoto=PhotoImage(file=“Porridge.gif”)
PorridgeButton=按钮(FoodSelectionWindow,text=“Porridge”,command=PorridgeFunct,image=PorridgePhoto,化合物=左侧,高度=100,宽度=200)
PorridgeButton.grid(列=2,行=5)
NoBreakfastPhoto=PhotoImage(file=“NoBreakfast.gif”)
NoBreakfastButton=按钮(FoodSelectionWindow,text=“None”,command=NoFood,image=NoBreakfastPhoto,Composite=LEFT,height=100,width=200)
NoBreakfastButton.grid(列=3,行=5)
EndScreen=按钮(FoodSelectionWindow,text=“Finish”,command=FinishScreen,宽度=12)
EndScreen.grid(列=2,行=6)

有什么建议吗?谢谢。

如您发布的摘录所示,创建按钮的代码是否真的在任何函数之外?出现此问题的通常原因是,您仅使用局部变量(在函数中)引用图像,这导致一旦函数退出,图像就会被垃圾收集。解决这个问题的通常方法是将图像存储为使用它的小部件的属性:
BaconButton.photo=BaconPhoto
,例如,它都在一个函数中。所以我需要这样做:
BaconPhoto=PhotoImage(file=“bacon.gif”)BaconButton.photo=BaconPhoto BaconButton=Button(FoodSelectionWindow,text=“bacon”,command=BaconFunct,image=BaconButton.photo,composite=LEFT,height=100,width=200)
准确-您只需确保图像对象的寿命至少与使用它的小部件的寿命相同。
def NoFood():
    TotalCalories = NoBreakfast.calories
    NoFoodOption = 1

def BoiledEggsFunct():
    TotalCalories = BoiledEggs.calories + TotalCalories

def FriedEggsFunct():
    TotalCalories = FriedEggs.calories + TotalCalories

def ScrambledEggsFunct():
    TotalCalories = ScrambledEggs.calories + TotalCalories

def PoachedEggsFunct():
    TotalCalories = PoachedEggs.calories + TotalCalories

def ToastFunct():
    TotalCalories = Toast.calories + TotalCalories

def BaconFunct():
    TotalCalories = Bacon.calories + TotalCalories

def CerealFunct():
    TotalCalories = Cereal.calories + TotalCalories

def PorridgeFunct():
    TotalCalories = Porridge.calories + TotalCalories

def FinishScreen():
    FinishWindow = Toplevel()

    if TotalCalories > 0 and TotalCalories < 1000:
        HealthyLabel = Label(FinishWindow, text = "Congratulations, you are healthy!", font=("Comic Sans MS", 25), fg = "light green",
              bg = "black")
        HealthyLabel.grid(columnspan=10)

    elif TotalCalories > 1000:
        UnhealthyLabel = Label(FinishWindow, text = "Try to eat healthier tomorrow. See personalised advice", font=("Comic Sans MS", 25), fg = "yellow",
              bg = "black")
        UnhealthyLabel.grid(columnspan=10)

    elif NoFoodOption == 1:
        NoFoodLabel = Label(FinishWindow, text = "Not eating can be harmful to your health. See personalised advice", font=("Comic Sans MS", 25), fg = "red",
              bg = "black")
        NoFoodLabel.grid(columnspan=10)

    else:
        Error = Label(FinishWindow, text = "error", font=("Comic Sans MS", 25), fg = "red",
              bg = "black")
        NoFoodLabel.grid(columnspan=10)

BoiledEggsPhoto = PhotoImage(file="BoiledEgg.gif")
BoiledEggsButton = Button(FoodSelectionWindow, text="Boiled Eggs", command=BoiledEggsFunct, image=BoiledEggsPhoto, compound=LEFT, height=100, width = 200)
BoiledEggsButton.grid(column=1,row=3)

ScrambledEggsPhoto = PhotoImage(file="ScrambledEgg.gif")
ScrambledEggsButton = Button(FoodSelectionWindow, text="Scrambled Eggs", command=ScrambledEggsFunct, image=ScrambledEggsPhoto, compound=LEFT, height=100, width = 200)
ScrambledEggsButton.grid(column=2,row=3)

FriedEggsPhoto = PhotoImage(file="FriedEgg.gif")
FriedEggsButton = Button(FoodSelectionWindow, text="Fried Eggs", command = FriedEggsFunct, image=FriedEggsPhoto, compound=LEFT, height=100, width = 200)
FriedEggsButton.grid(column=3, row=3)

PoachedEggsPhoto = PhotoImage(file="PoachedEgg.gif")
PoachedEggsButton = Button(FoodSelectionWindow, text="Poached Eggs", command = PoachedEggsFunct, image=PoachedEggsPhoto,  compound=LEFT, height=100, width = 200)
PoachedEggsButton.grid(column=1, row=4)

ToastPhoto = PhotoImage(file="Toast.gif")
ToastButton = Button(FoodSelectionWindow, text="Toast", command = ToastFunct, image=ToastPhoto, compound=LEFT, height=100, width = 200)
ToastButton.grid(column=2,row=4)

BaconPhoto = PhotoImage(file="Bacon.gif")
BaconButton = Button(FoodSelectionWindow, text="Bacon", command = BaconFunct, image=BaconPhoto, compound=LEFT, height=100, width = 200)
BaconButton.grid(column=3, row=4)

CerealPhoto = PhotoImage(file="Cereal.gif")
CerealButton = Button(FoodSelectionWindow, text="Cereal", command = CerealFunct, image=CerealPhoto, compound=LEFT, height=100, width = 200)
CerealButton.grid(column=1, row=5)

PorridgePhoto = PhotoImage(file="Porridge.gif")
PorridgeButton = Button(FoodSelectionWindow, text="Porridge", command = PorridgeFunct, image=PorridgePhoto, compound=LEFT, height=100, width = 200)
PorridgeButton.grid(column=2, row=5)

NoBreakfastPhoto = PhotoImage(file="NoBreakfast.gif")
NoBreakfastButton = Button(FoodSelectionWindow, text= "None", command = NoFood, image=NoBreakfastPhoto, compound=LEFT, height=100, width = 200)
NoBreakfastButton.grid(column=3, row =5)

EndScreen = Button(FoodSelectionWindow, text="Finish", command = FinishScreen, width=12)
EndScreen.grid(column=2,row=6)