How to put image Buttons on an image using Tkinter?

Asked

Viewed 26 times

0

Hi, I’m developing a game and I need buttons with images to appear on another image. These buttons also need to switch between different game screens. However, I just can’t get the buttons to appear on the screen, whether they have images or not. I’m desperate T.T

    import tkinter as tk
        
    class SampleApp(tk.Tk):
        def __init__(self):
            tk.Tk.__init__(self)
            self._frame = None
            self.switch_frame(StartPage)

        def switch_frame(self, frame_class):
            new_frame = frame_class(self)
            if self._frame is not None:
                self._frame.destroy()
            self._frame = new_frame
            self._frame.place(x= 0, y=0)

    class StartPage(tk.Frame):
        def __init__(self, master):
            tk.Frame.__init__(self, master)
            self.image = tk.PhotoImage(file= r"D:\imagem.png")
            self.image = self.image.subsample(1,1)
            self.labelimage = tk.Label(image = self.image)
            self.labelimage.place(x= 0, y=0)
            #tk.Label(self, text="Start page", font=('Helvetica', 18, "bold"), image = self.imagem).pack(side="right", fill="both", pady=5)
            self.image_button1 = tk.PhotoImage(file = r"D:\botao.png")
            tk.Button(self, image = self.image_button1, command=lambda: master.switch_frame(PageOne)).place(x= 10, y=10)
            tk.Button(self, text="Go to page two", command=lambda: master.switch_frame(PageTwo)).place(x= 40, y=40)

    class PageOne(tk.Frame):
        def __init__(self, master):
            tk.Frame.__init__(self, master)
            tk.Frame.configure(self,bg='blue')
            tk.Label(self, text="Page one", font=('Helvetica', 18, "bold")).place(x= 0, y=0)
            tk.Button(self, text="Go back to start page",
                    command=lambda: master.switch_frame(StartPage)).place(x= 50, y=50)

    class PageTwo(tk.Frame):
        def __init__(self, master):
            tk.Frame.__init__(self, master)
            tk.Frame.configure(self,bg='red')
            tk.Label(self, text="Page two", font=('Helvetica', 18, "bold")).place(x= 0, y=0)
            tk.Button(self, text="Go back to start page",
                    command=lambda: master.switch_frame(StartPage)).place(x= 70, y=70)

    if __name__ == "__main__":
        app = SampleApp()
        app.title("Exemplo")
        app.geometry("700x700")
        app.configure(background = "white")
        app.mainloop()
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.