2017-03-15 14 views
0

まずは開始したいと思います。私はPythonの初心者です。質問を投稿する前に、以前のさまざまな投稿を通じて広範な調査と試行錯誤を行いました。ボタンにアイコンを埋め込むことはできません

理想的には、フレームの詳細と3つのボタンを定義するラベル付きの小さなtkinterフレームを作成しようとしています。ボタンとラベルが機能し、配置がうまくいった。そのとき私はボタンにアイコンを追加しようとすると、それは私とエラーをスローします。

これは、これまでのスクリプトです:

import tkinter 
from tkinter import * 

def doNothing(): 
    print("doNothing") 

icon1=PhotoImage(file="icon1.png") 
icon2=PhotoImage(file="icon2.png") 
icon3=PhotoImage(file="icon3.png") 

W=tkinter.Tk() 
W.geometry("325x300+0+0") 
W.configure(bg="gray10") 

FRAME1=Frame(W, width=100, height =100) 
FRAME1.place(x=0,y=0) 
LABEL1=Label(FRAME1,relief=FLAT, text="Profile",font="Verdana 10 bold",width=25, height =1).grid(row=0, column=0) 
Button1= Button(FRAME1,relief=FLAT, width=3, height =1,command=doNothing).grid(row=0, column=1) 
Button1.config(image=icon1) 
Button1.pack() 
Button2= Button(FRAME1,relief=FLAT, width=3, height =1,command=doNothing).grid(row=0, column=2) 
Button2.config(image=icon2) 
Button2.pack() 
Button3= Button(FRAME1,relief=FLAT, width=3, height =1,command=doNothing).grid(row=0, column=3) 
Button3.config(image=icon3) 
Button3.pack() 

W.mainloop() 

私はこのスクリプトを実行すると、私は次の出力を取得します。

Traceback (most recent call last): 
    File "C:/Users/ADRIA/PycharmProjects/LATAM/TESTING CODE.py", line 7, in <module> 
icon1=PhotoImage(file="icon1.png") 
    File "C:\Users\ADRIA\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 3394, in __init__ 
Image.__init__(self, 'photo', name, cnf, master, **kw) 
    File "C:\Users\ADRIA\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 3335, in __init__ 
raise RuntimeError('Too early to create image') 
RuntimeError: Too early to create image 

これをクリアするための援助は驚くべきものです。

乾杯、

答えて

1

問題は、あなたがTkインスタンスを作成した前PhotoImageを作成しようとしているということです。このエラーは、Too early to create imageであなたに伝えようとしているものです。

は単純に、この周りに切り替える:

icon1=PhotoImage(file="icon1.png") 
icon2=PhotoImage(file="icon2.png") 
icon3=PhotoImage(file="icon3.png") 

W=tkinter.Tk() 

だからtkinter.Tk()は次のように最初に来る:

あなたはこのような Button1割り当てるため
W=tkinter.Tk() 

icon1=PhotoImage(file="icon1.png") 
icon2=PhotoImage(file="icon2.png") 
icon3=PhotoImage(file="icon3.png") 

あなたが取得している他の問題がある:

Button1= Button(FRAME1,relief=FLAT, width=3, height =1,command=doNothing).grid(row=0, column=1) 

Buttonは割り当てられていません実際に戻り値.grid(row=0, column=1)を割り当てている値はButton1です。あなたも、この操作を行う必要があります

Button1= Button(FRAME1,relief=FLAT, width=3, height =1,command=doNothing) 
Button1.grid(row=0, column=1) 
Button1.config(image=icon1) 

が、Button2Button3

Button1= Button(FRAME1,relief=FLAT, width=3, height =1,command=doNothing).grid(row=0, column=1) 
Button1.config(image=icon1) 

の中へ:あなたがする必要がどのような

は変化です。

さらにnever mix grid and pack

Warning: Never mix grid and pack in the same master window. Tkinter will happily spend the rest of your lifetime trying to negotiate a solution that both managers are happy with. Instead of waiting, kill the application, and take another look at your code. A common mistake is to use the wrong parent for some of the widgets.

だから、あなたがButton*.grid()を使用する場合、あなたはButton.pack()呼び出しを削除する必要があります。

+0

私はスクリプトを変更しました。しかし、出力は以下のようになります。トレースバック(最新の呼び出しの最後): ファイル: Button1.config(画像= ICON1) はAttributeErrorの "C /ユーザ/ ADRIA/PycharmProjects/LATAM/TESTING CODE.py"、ライン19、 : 'NoneType'オブジェクトに属性 'config'がありません 終了コード1で処理が完了しました –

+0

PNGをICOまたはGIFに再フォーマットする必要がありますか?または新人ミスをしたことがありますか? –

+0

@ A.Sergio私の更新を見てください。 – Vallentin

関連する問題