如何修复这个Tkinter按钮错误

2024-03-28 09:49:17 发布

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

每当我运行程序时,按钮都会显示,但是图像不会显示,而是显示为灰色空间。此外,除了“finish”按钮之外的所有按钮都是不可使用的(请参见随附的图片)。Button bug

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)

有什么建议吗?谢谢。你知道吗


Tags: textimagedefcolumnbuttonwidthgifcommand