Python Tkinker - 将 jpg 显示为类方法不起作用 [英] Python Tkinker - showing a jpg as a class method not working

查看:13
本文介绍了Python Tkinker - 将 jpg 显示为类方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 jpg 图像显示为我正在构建的 GUI 事物的背景.

I'm trying to show a jpg image as background for a GUI thing I'm building.

我可以用一种方法让它工作:

I can get it to work in a single method:

from Tkinter import *
from PIL import Image, ImageTk

class MakeGUI(object):
    master = None
    w = None

    def __init__(self):
        self.canvasSizeY = 400 #height
        self.canvasSizeX = 640 #width

    def setupCanvas(self):
        """
        preps the canvas for drawing.
        """
        self.master = Tk()
        self.w = Canvas(self.master, width=self.canvasSizeX, height=self.canvasSizeY)
        self.w.config(bg='white')
        image = Image.open("background.jpg")
        photo = ImageTk.PhotoImage(image)
        self.w.create_image(0,0, image=photo, anchor=NW)
        self.w.pack()
        mainloop()

def main():
    makeGUI = MakeGUI()
    makeGUI.setupCanvas()

if __name__ == '__main__':
    main()

但是当我尝试用一​​种方法制作画布,并用另一种方法显示画布时,它不显示 jpg(当我一直在测试时,我已经创建并显示 & 文本和矩形使用这个方法):

But when I try and make the canvas in one method, and show the canvas in another, it doesn't show the jpg (when I've been testing, I've created and shown & text and rectangles using this approach):

from Tkinter import *
from PIL import Image, ImageTk

class MakeGUI(object):
    master = None
    w = None

    def __init__(self):
        self.canvasSizeY = 400 #height
        self.canvasSizeX = 640 #width

    def setupCanvas(self):
        """
        preps the canvas for drawing.
        """
        self.master = Tk()
        self.w = Canvas(self.master, width=self.canvasSizeX, height=self.canvasSizeY)
        self.w.config(bg='white')
        image = Image.open("background.jpg")
        photo = ImageTk.PhotoImage(image)
        self.w.create_image(0,0, image=photo, anchor=NW)

    def showImage(self):
        """
       pushes the image to the screen
       """
        self.w.pack()
        self.w.mainloop()

def main():
    makeGUI = MakeGUI()
    makeGUI.setupCanvas()

if __name__ == '__main__':
    main()

我想在进行一些编辑时动态使用 GUI 来显示一些文本,所以我有兴趣在我深入构建之前了解我出了什么问题,以防万一它是一个showstopper...

I want to use the GUI dynamically to show some text as I work through some editing, so I'm interested to understand what I've got wrong before I get too far into the build in case its a showstopper...

推荐答案

最明显的问题是,在第二种情况下,您永远不会调用 showImage.即使您确实调用了该函数,您的图像也可能不会出现.如果没有对图像的引用,图像将被垃圾收集.看起来好像有一个参考,因为您将它添加到画布上,但这还不够.

The most obvious problem is that in the second case you are never calling showImage. Even after you do call that function, your image probably won't show up. Images will be garbage-collected if there isn't a reference to them. It may seem like there's a reference because you're adding it to a canvas, but that isn't enough.

您需要执行以下操作:

self.photo = ImageTk.PhotoImage(image)

最后,我建议您从 showImage 中调用 mainloop.mainloop 必须始终被调用一次,因此最典型的是它是程序中的最后一行代码,或主函数中的最后一行代码.

Finally, I recommend that you take the call to mainloop out of showImage. mainloop must always be called exactly once, so most typically it is the last line of code in your program, or the last line of code in your main function.

制作 Tkinter 应用程序的更常见方法是将 Tk 对象或 Frame 对象子类化,而不是让您的主应用程序成为通用对象.例如:

A more common way to make a Tkinter application is to subclass either the Tk object or a Frame object, rather than having your main application be a generic object. For example:

class MyApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        ...
        self.setupCanvas(...)
        ...
if __name__ == "__main__":
    app = MyApp()
    app.mainloop()

这篇关于Python Tkinker - 将 jpg 显示为类方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆