tkinter 显示图像 [英] tkinter to display the images

查看:39
本文介绍了tkinter 显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 python 的新手.我有一个代码,其中图像没有打印在 Tkinter 上.所以请帮助我如何显示图像以及按钮和文本框.

I am a newbie to python.I have a code where the image is not printed on the Tkinter.So please help me on how to display the image along with the Button and Textbox.

代码:

import Tkinter
from Tkinter import *
class myproject(Tkinter.Tk):
            def __init__(self,parent):
                Tkinter.Tk.__init__(self)
                self.button2()
                self.text()
                self.image()
            def button2(self):
                button2 = Tkinter.Button(self, text = "hello")
                button2.grid(column=5,row=7)
            def text(self):
                text = Tkinter.Text(self, height=3, width=31) # self.text
                text.grid(column=1,row=3)
                text.insert(END, "Wiilliam Skakespeare")

            def image(self):

                logo = PhotoImage(file="linux.gif")
                w1 = Tkinter.Label(self, image=logo).pack(side="right")


app = myproject(None)
app.mainloop()

请帮忙!答案将不胜感激!

Please help!Answers will be appreciated!

推荐答案

您必须保存对照片图像的引用.

查看此页面了解更多信息,或这个

但是,您发布的代码还有许多其他问题;例如,在函数和类声明之后需要冒号.发布代码时,类中也不需要额外的方法,它们只会增加理解难度

There are numerous other problems with the code you posted, however; you need colons after function and class declarations, for example. When posting code, there's also no need for extraneous methods in the class, they only make it more difficult to understand

您也不能混用经理,否则您的整个程序可能会停止.这意味着您不应该在同一个程序中使用 packgrid.通读 effbot 教程,真的很有帮助!

You also cannot mix managers or you're whole program might stall. This means you shouldn't be using pack and grid in the same program. Read through the effbot tutorial, it's really helpful!

class myproject(Tkinter.Tk):
        def __init__(self,parent):
            Tkinter.Tk.__init__(self)

            self.image()


        def image(self):

            logo = Tkinter.PhotoImage(file='linux.gif')
            self.logo = logo # You always need a reference to the image or it gets garbage collected
            w1 = Tkinter.Label(self, image=logo).grid()


app = myproject(None)
app.mainloop()

这篇关于tkinter 显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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