鱼C论坛

 找回密码
 立即注册
查看: 3353|回复: 2

[已解决]有关tkinter的t图像显示问题

[复制链接]
发表于 2015-10-8 23:34:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ~风介~ 于 2015-10-9 19:19 编辑

为什么图片显示不出来?

  1. import tkinter as tk
  2. class APP:
  3.     def __init__(self,root):
  4.         frame=tk.Frame(root)
  5.         frame.pack(side=tk.LEFT)

  6.         photo=tk.PhotoImage(file='D:\\python3.3.2\\18.gif')
  7.         imglable=tk.Label(root,image=photo)
  8.         imglable.pack()

  9.         self.hi_there=tk.Label(root,text='欢迎来到yz工作室!')
  10.         self.hi_there.pack()

  11.         theButton=tk.Button(root,text='ok',command=self.callback)
  12.         theButton.pack(padx=100,pady=100)

  13.     def callback(self):
  14.         print('great!')


  15. root=tk.Tk()
  16. app=APP(root)

  17. root.mainloop()
复制代码
最佳答案
2015-10-9 15:20:40
问题在于,你在乱调用!
要么你调用方法,代码如下!!!!可以实现!!
  1. import tkinter as tk
  2. class APP:
  3.     def __init__(self,root):
  4.         self.frame=tk.Frame(root)
  5.         self.frame.pack(side=tk.LEFT)

  6.         self.photo=tk.PhotoImage(file='D:\\python3.3.2\\18.gif')
  7.         self.imglable=tk.Label(root,image=self.photo)
  8.         self.imglable.pack()

  9.         self.hi_there=tk.Label(root,text='欢迎来到yz工作室!')
  10.         self.hi_there.pack()

  11.         theButton=tk.Button(root,text='ok',command=self.callback)
  12.         theButton.pack(padx=100,pady=100)

  13.     def callback(self):
  14.         print('great!')


  15. root=tk.Tk()
  16. app=APP(root)

  17. root.mainloop()
复制代码


要么你用函数式的调用!!代码如下:
  1. import tkinter as tk
  2. class APP:
  3.     def __init__():
  4.         root=tk.Tk()
  5.         frame=tk.Frame(root)
  6.         frame.pack(side=tk.LEFT)

  7.         photo=tk.PhotoImage(file='D:\\python3.3.2\\18.gif')
  8.         imglable=tk.Label(root,image=photo)
  9.         imglable.pack()

  10.         hi_there=tk.Label(root,text='欢迎来到yz工作室!')
  11.         hi_there.pack()

  12.         theButton=tk.Button(root,text='ok',command=APP.callback)
  13.         theButton.pack(padx=100,pady=100)
  14.         root.mainloop()

  15.     def callback():
  16.         print('great!')

  17. APP.__init__()
复制代码


不要在类里面混着弄!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-10-9 15:20:40 | 显示全部楼层    本楼为最佳答案   
问题在于,你在乱调用!
要么你调用方法,代码如下!!!!可以实现!!
  1. import tkinter as tk
  2. class APP:
  3.     def __init__(self,root):
  4.         self.frame=tk.Frame(root)
  5.         self.frame.pack(side=tk.LEFT)

  6.         self.photo=tk.PhotoImage(file='D:\\python3.3.2\\18.gif')
  7.         self.imglable=tk.Label(root,image=self.photo)
  8.         self.imglable.pack()

  9.         self.hi_there=tk.Label(root,text='欢迎来到yz工作室!')
  10.         self.hi_there.pack()

  11.         theButton=tk.Button(root,text='ok',command=self.callback)
  12.         theButton.pack(padx=100,pady=100)

  13.     def callback(self):
  14.         print('great!')


  15. root=tk.Tk()
  16. app=APP(root)

  17. root.mainloop()
复制代码


要么你用函数式的调用!!代码如下:
  1. import tkinter as tk
  2. class APP:
  3.     def __init__():
  4.         root=tk.Tk()
  5.         frame=tk.Frame(root)
  6.         frame.pack(side=tk.LEFT)

  7.         photo=tk.PhotoImage(file='D:\\python3.3.2\\18.gif')
  8.         imglable=tk.Label(root,image=photo)
  9.         imglable.pack()

  10.         hi_there=tk.Label(root,text='欢迎来到yz工作室!')
  11.         hi_there.pack()

  12.         theButton=tk.Button(root,text='ok',command=APP.callback)
  13.         theButton.pack(padx=100,pady=100)
  14.         root.mainloop()

  15.     def callback():
  16.         print('great!')

  17. APP.__init__()
复制代码


不要在类里面混着弄!!!

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
~风介~ + 3 + 3 热爱鱼C^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2015-10-10 15:39:25 | 显示全部楼层
仔细想了下,可能你想知道的是,为什么theButton可以显示,而同样方式的Photo却不能显示!!!
在用到图像时最好把图像格式的gif或者bmp格式的放到实例或者全局中,防止Python将图像自动回收了,当垃圾处理掉了!
所以我对你说的在乱调用,是因为你app=APP(root),实例调用,然而,图像却没有self绑定,也即不是实例的属性,事实上你这样app就两个属性一个hi_there,即app.hi_there,一个是callback,图像却不属于实例,也不是全局的!!!建议弄成实例的属性,前面加self
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-17 20:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表