简体   繁体   English

使用标签小部件使用 Tkinter 显示图像

[英]Displaying Image with Tkinter Using Label Widget

I'm trying to display an image using PIL in a Tkinter class:我正在尝试在 Tkinter 类中使用 PIL 显示图像:

class PasswordCheck(Frame):
    def __init__(self,master=None):
            Frame.__init__(self,master)
            self.pack()

    def create_widgets(self):
            self.title=Label(self,text='Curretly using password')
            self.pwfield=Entry(self,text=self.password)
            self.web=Label(self,image=self.image)
            self.ok=Button(self)
            self.ok['text']='OK'
            self.ok['command']=root.destroy
            self.ok.pack(side='top')
            self.quit=Button(self,text="Quit",command=root.destroy)
            self.quit.pack(side='bottom')

    def setParms(self,password,image):
            self.password=password
            self.image=image

I need to mention that I am a Tkinter beginner.我需要提一下,我是 Tkinter 初学者。 I create the image from a web site (using HTMLParser) and set up the window thus:我从网站(使用 HTMLParser)创建图像并设置窗口:

with open(authFile,'r') as f:
    lines=f.read().splitlines()
password=lines[1]
f=urllib.urlopen(URL)
parser=PWParser()
parser.feed(f.read())
response=requests.get(URL+imageURL.replace(" ","%20"))
img=PIL.Image.open(BytesIO(response.content))
root=Tk()
window=PasswordCheck(master=root)
window.setParms(password,img.convert('1').tobitmap())
window.create_widgets()
window.mainloop()

The image is good (img.show()) so I converted it to bitmap and passed it to the Tkinter class.图像很好 (img.show()) 所以我将它转换为位图并将它传递给 Tkinter 类。 When I run the script I get an error saying static char image_bits[] = { ... does not exist:当我运行脚本时,我收到一条错误消息,指出 static char image_bits[] = { ... 不存在:

(cannot post traceback, form incorrectly thinks it is improperly formatted code, need help here) (无法发布回溯,表单错误地认为它是格式不正确的代码,这里需要帮助)

I read in several places about garbage collection getting rid of the image before it is displayed but it was not clear how to stop that.我在几个地方阅读了关于垃圾收集在显示之前摆脱图像的内容,但不清楚如何阻止它。 If that is the reason, how do I keep 'img' from being removed or is there something else wrong?如果这是原因,我如何防止 'img' 被删除,或者还有其他问题吗? TIA. TIA。

I got it.我知道了。 It turns out the image needs to be a photoimage.事实证明,图像需要是照片图像。 This is what worked:这是有效的:

window.setParms(password,PIL.ImageTk.PhotoImage(img))

Try this:尝试这个:

import tkinter as tk

root = tk.Tk()
logo = tk.PhotoImage(file="program_logo.gif")

explanation = """At present, only GIF and PPM/PGM
formats are supported, but an interface 
exists to allow additional image file
formats to be added easily."""

w = tk.Label(root, 
             compound = tk.CENTER,
             text=explanation, 
             image=logo).pack(side="right")

root.mainloop()

program_logo.gif is your logo but as a .gif file. program_logo.gif 是您的徽标,但作为 .gif 文件。

I got it from this site: https://www.python-course.eu/tkinter_labels.php我从这个网站得到它: https : //www.python-course.eu/tkinter_labels.php

hope it helps, bye :)希望它有帮助,再见:)

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

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