Why my image buttons are not appearing?

前端 未结 2 1084
广开言路
广开言路 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条回答
  • 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.

    0 讨论(0)
  • 2021-01-21 23:08

    In addition to keeping a reference to the image, you have a problem with this line:

    self.grid()
    

    in the __init__ method of Application. It's gridding the Frame into the window, but since nothing is ever packed or gridded into the frame, it doesn't ever expand past a little, tiny frame, so you just don't see the Buttons inside it. A simple fix here would be the pack method, with arguments to fill the window and expand when needed:

    self.pack(fill=BOTH, expand=1)
    
    0 讨论(0)
提交回复
热议问题