4

When I execute the script the background image works as it should, it will match the size of the window, however, I cannot get the buttons to show (they have no functionality yet). I am fairly new to python so I'm not sure if I am using buttons as an event is a good idea. Any Help is appreciated.

import turtle
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk

root = Tk()

taxi = (r"C:\directory\image.png")

class App(Frame):
    def __init__(self, master, Buttons=None):
        Frame.__init__(self, master, Buttons)
        self.columnconfigure(0,weight=1)
        self.rowconfigure(0,weight=1)
        self.original = Image.open(r"C:\directory\Layout.gif")
        self.image = ImageTk.PhotoImage(self.original)
        self.display = Canvas(self, bd=0, highlightthickness=0)
        self.display.create_image(500, 500, image=self.image, anchor=NW, tags="IMG")
        self.display.grid(row=0, column=0, sticky=W+E+N+S)
        self.pack(fill='both', expand=True)
        self.bind("<Configure>", self.resize)

    def resize(self, event):
        size = (event.width, event.height)
        resized = self.original.resize(size,Image.ANTIALIAS)
        self.image = ImageTk.PhotoImage(resized)
        self.display.delete("IMG")
        self.display.create_image(0, 0, image=self.image, anchor=NW, tags="IMG")

    def Buttons(self, event):
        self.Button1 = tk.Button(master = root, text = "Button1") #, command = forward).pack(side = tk.LEFT)
        self.Button1.grid(row=1, column=1)

        self.Button2 = tk.Button(master = root, text = "Button2") #, command = forward).pack(side = tk.LEFT)
        self.Button2.grid(row=2, column=1)

        self.Button3 = tk.Button(master = root, text = "Button3") #, command = forward).pack(side = tk.LEFT)
        self.Button3.grid(row=3, column=1)


app = App(root)
app.mainloop()
EcSync
  • 842
  • 1
  • 6
  • 20

1 Answers1

3

I added the suggestions in the comments to your code and removed some unrelated lines (to the problem). Now the buttons show:

  1. Remove the events argument from the Buttons method.

  2. Add self.Buttons() to self.__init__

Minimal working (solved) example.

from tkinter import *


class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.pack(fill='both', expand=True)
        self.bind("<Configure>", self.resize)
        self.Buttons()  # <--------------------- IMPORTANT!

    def resize(self, event):
        size = (event.width, event.height)

    def Buttons(self):  # <--------------------- Remove the event argument
        self.Button1 = Button(master=self, text="OLD VINS")
        self.Button1.grid(row=1, column=1)

        self.Button2 = Button(master=self, text="QBAY")
        self.Button2.grid(row=2, column=1)

        self.Button3 = Button(master=self, text="HELP")
        self.Button3.grid(row=3, column=1)


root = Tk()
app = App(root)
app.mainloop()
SimonF
  • 1,855
  • 10
  • 23
  • This isn't an answer to my question – EcSync Jan 10 '19 at 11:38
  • @EcSync Your problem was that the buttons doesn't show, correct? ("I cannot get the buttons to show (they have no functionality yet)") – SimonF Jan 10 '19 at 11:58
  • It's isn't a solution if you just remove the parts I need, your answer doesn't include my background image. The issue is I can't display buttons on a canvas w/ background – EcSync Jan 10 '19 at 12:04
  • Apply the solution of the minimal example to your own code. – SimonF Jan 10 '19 at 12:07
  • We can't run your full code as the image only exists on your local machine. Thats why we need a __minimal__ example. See https://stackoverflow.com/help/mcve . I created a minimal example from your code, with the same problem (no buttons) and showed you how to solve it. The problem is not in the image, it's in the buttons... – SimonF Jan 10 '19 at 12:12