Python use image as background (graphics.py)

So my professor is making us use Graphis.py(zelle) to make a GUI I've made all the buttons my issue is that the module doesn't have any functionality that allows a image to be the background only the color. Do you guys have any idea how I can modify it so I can set the background to a image? The setBackground method is the one I believe needs to be edited

class GraphWin(tk.Canvas):

"""A GraphWin is a toplevel window for displaying graphics."""

def __init__(self, title="Graphics Window",
             width=200, height=200, autoflush=True):
    master = tk.Toplevel(_root)
    master.protocol("WM_DELETE_WINDOW", self.close)
    tk.Canvas.__init__(self, master, width=width, height=height)
    self.master.title(title)
    self.pack()
    master.resizable(0,0)
    self.foreground = "black"
    self.items = []
    self.mouseX = None
    self.mouseY = None
    self.bind("<Button-1>", self._onClick)
    self.bind_all("<Key>", self._onKey)
    self.height = height
    self.width = width
    self.autoflush = autoflush
    self._mouseCallback = None
    self.trans = None
    self.closed = False
    master.lift()
    self.lastKey = ""
    if autoflush: _root.update()

def __checkOpen(self):
    if self.closed:
        raise GraphicsError("window is closed")

def _onKey(self, evnt):
    self.lastKey = evnt.keysym


def setBackground(self, color):
    """Set background color of the window"""
    self.__checkOpen()
    self.config(bg=color)
    self.__autoflush()

Upvotes: 0

Views: 2744

Answers (1)

thorargent
thorargent

Reputation: 11

If you store your image as a gif, for example, Python will usually be able to display it. Here are the steps. I will assume that you create a graphic window named "win".

First, save your image in a file such as TestPic.gif. Second, import the image and assign it a name such as b:

b = Image(Point(100,100),"TestPic.gif")

The Point(100,100) is the point that the image will be centered on. Now you want to display it:

Image.draw(b,win)

That's pretty much it. You can manipulate the image of course by using standard Python Graphics commands, such as moving it around, etc.

For reference, look at the Graphics pdf at this link:

http://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf

It spells everything out pretty well.

Upvotes: 1

Related Questions