2017-12-29 50 views
1

에 이미지를 추가, 나는이 문제에 대해 와서 :Tkinter를 학습하는 과정에서 버튼

<ipython-input-30-6ad3ebb78b5b> in <module>() 
     7 button.grid() 
     8 photo=PhotoImage(file="giphy.gif") 
----> 9 button.config(image=photo, compound=RIGHT) 
    10 
    11 root.mainloop() 

/usr/lib/python3.5/tkinter/__init__.py in configure(self, cnf, **kw) 
    1331   the allowed keyword arguments call the method keys. 
    1332   """ 
-> 1333   return self._configure('configure', cnf, kw) 
    1334  config = configure 
    1335  def cget(self, key): 

/usr/lib/python3.5/tkinter/__init__.py in _configure(self, cmd, cnf, kw) 
    1322   if isinstance(cnf, str): 
    1323    return self._getconfigure1(_flatten((self._w, cmd, '-'+cnf))) 
-> 1324   self.tk.call(_flatten((self._w, cmd)) + self._options(cnf)) 
    1325  # These used to be defined in Widget: 
    1326  def configure(self, cnf=None, **kw): 

TclError: image "pyimage.." doesn't exist 
: 그 코드에 오류가 있습니다

from tkinter import* 
from tkinter import ttk 

root=Tk() 

button=ttk.Button(root) 
button.grid() 
photo=PhotoImage(file="giphy.gif") 
button.config(image=photo, compound=RIGHT) 

root.mainloop() 

: 나는 버튼에 이미지를 추가 할 수 없습니다

왜 그렇습니까? 어떻게 해결할 수 있습니까?

+2

오류 코드와 일치하지 않습니다. 고쳐주세요. –

+2

나는 그것을 테스트했고 그 코드는 나에게 잘 작동한다. 오류를 재현하는 코드를 보여주십시오. – Novel

+1

레이블과 같은 다른 tkinter의 구성 요소에 동일한 오류가 있습니다. – Korgan

답변

1

furas가 의견에서 말했듯이 코드는 python script.py으로 완벽하게 실행됩니다.

오류는 Jupyter QtConsole에서 실행한다는 사실에서 비롯된 것입니다. Jupyter QtConsole에서 실행하려면 tkinter에게 PhotoImage의 부모 창이 무엇인지 명시 적으로 알려줘야합니다. 나는 콘솔에서 기본 부모가 당신이 만든 Tk 인스턴스가 아니라 숨겨진 윈도우이기 때문에 이것이라고 생각합니다. 따라서 이미지의 부모는 버튼의 부모가 아니므로 tkinter는 이미지를 찾지 못합니다.

다음 코드는 콘솔에서 실행해야합니다

import tkinter as tk 
from tkinter import ttk 

root = tk.Tk() 

button = ttk.Button(root) 
button.grid() 
photo = tk.PhotoImage(file="giphy.gif", master=root) 
button.config(image=photo, compound=tk.RIGHT)