繁体   English   中英

Python使用图像作为背景(graphics.py)

[英]Python use image as background (graphics.py)

因此,我的教授正在让我们使用Graphis.py(zelle)来创建GUI,我已经完成了所有按钮的任务,我的问题是该模块没有任何允许图像仅作为背景颜色的功能。 你们知道如何修改它,以便可以将背景设置为图像吗? 我认为setBackground方法是需要编辑的方法

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()

例如,如果将图像存储为gif,Python通常将能够显示它。 步骤如下。 我将假定您创建一个名为“ win”的图形窗口。

首先,将图像保存在TestPic.gif等文件中。 其次,导入图像并为其指定名称,例如b:

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

Point(100,100)是图像将居中的点。 现在您要显示它:

Image.draw(b,win)

就是这样。 当然,您可以使用标准的Python图形命令来操纵图像,例如移动图像等。

作为参考,请查看以下链接上的Graphics pdf:

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

它可以很好地说明一切。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM