Why my image buttons are not appearing?

前端 未结 2 1107
广开言路
广开言路 2021-01-21 22:29

I am trying to place two image buttons on my image background in a certain position, but my buttons are not appearing. I think their images are behind the background.

I

2条回答
  •  萌比男神i
    2021-01-21 23:08

    It is likely that your image is being garbage collected before it is displayed. This is a common Tkinter gotcha. Try changing the lines:

    button1 = PhotoImage(file ="button1.gif")
    button2 = PhotoImage(file ="button2.gif")
    

    to

    self.button1 = PhotoImage(file ="button1.gif")
    self.button2 = PhotoImage(file ="button2.gif")
    

    and use

    settings_button = Button(self, image = self.button1, command = self.mult_command, width = 15)
    

    etc.

    This should keep a reference to your image, stopping it from getting garbage collected.

提交回复
热议问题