-2

I wanted to make a configuration menu where the user can select their language options and the resolution the game will run at. However, the labels for the language select and the image that is supposed to appear at the bottom do not display at all.

Language Label:

#Langauge Radio Buttons
Langlabel = Label(ConfigWindow, textvariable="Languages")
EngButton = Radiobutton(ConfigWindow, text = "English",variable = EngSelect, value = 1)
JapButton = Radiobutton(ConfigWindow, text = "Japanese", variable = JapSelect, value = 1)
#Buton Placements
Langlabel.place(x = 100,y = 20)
EngButton.place(x = 100,y = 50)
JapButton.place(x = 180,y = 50)
Langlabel.pack()

Image:

#Game Image
TestImage = ImageTk.PhotoImage(Image.open("TestMap.png"))
canvas.create_image(x = 100, y = 100, anchor=NW, image=TestImage)

ConfigWindow.mainloop()

Thanks in advance.

  • 1
    Your example code does not show the reason, read [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/a/16424553/7414759). Provide a [mcve]. – stovfl Feb 23 '20 at 11:53
  • `Langlabel.pack()` will override `Langlabel.place(...)`. You should use either one and not both. – acw1668 Feb 24 '20 at 02:07

1 Answers1

0

This happens because Python garbage collects the images because they have no reference to them. This can be fixed by assigning the created images/labels to an array or variable:

class ConfigWindow:
    images = []
    labels = []

    def setup(self):
        #Langauge Radio Buttons
        Langlabel = Label(ConfigWindow, textvariable="Languages")
        EngButton = Radiobutton(ConfigWindow, text = "English",variable = EngSelect, value = 1)
        JapButton = Radiobutton(ConfigWindow, text = "Japanese", variable = JapSelect, value = 1)
        self.labels.push(Langlabel)
        self.labels.push(EngButton)
        self.labels.push(JapButton)

        #Buton Placements
        Langlabel.place(x = 100,y = 20)
        EngButton.place(x = 100,y = 50)
        JapButton.place(x = 180,y = 50)
        Langlabel.pack()


    def load_images(self):
        TestImage = ImageTk.PhotoImage(Image.open("TestMap.png"))
        self.images.add(TestImage);
        canvas.create_image(x = 100, y = 100, anchor=NW, image=TestImage)
MaartenDev
  • 5,631
  • 5
  • 21
  • 33