まずは開始したいと思います。私は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
これをクリアするための援助は驚くべきものです。
乾杯、
私はスクリプトを変更しました。しかし、出力は以下のようになります。トレースバック(最新の呼び出しの最後): ファイル: Button1.config(画像= ICON1) はAttributeErrorの "C /ユーザ/ ADRIA/PycharmProjects/LATAM/TESTING CODE.py"、ライン19、 : 'NoneType'オブジェクトに属性 'config'がありません 終了コード1で処理が完了しました –
PNGをICOまたはGIFに再フォーマットする必要がありますか?または新人ミスをしたことがありますか? –
@ A.Sergio私の更新を見てください。 – Vallentin