私は Tkinter GUI を作成して、画像を呼び出す以外は何もしません。もちろん、適切な tkinter ドキュメントを見つけるのにずっと苦労してきました。
要求どおりに実行できないように見えるコード行があります-辞書内のすべての値を呼び出し、次の値が呼び出される前に、同じ名前で画像を個別に印刷およびプルしたいです。私は dict.itervalues() と dict.values() を試しましたが、まったく何もわからないようです...
とにかく、ここにスニペットがあります:
for key in ansDict.iterkeys(): #using the iterkeys function... kind of
x=key
root = tk.Tk() # root window created (is this in the right place?)
root.title('C H E M I S T R Y A B C\'s')
frameAns=tk.Frame(root)
frameAns.grid(row=0, column=0, sticky=tk.NW)
for i in range(len(ansDict[x])):
print '-->' + ansDict[x][i]
for value in ansDict.itervalues(): #This is the most important part
for i in range(len(value)): #pulls value list from dictionary named ansDict
picRef1 = Image.open(value[i] + '.jpg') #calls image file by the same name using PIL
photo1 = ImageTk.PhotoImage(picRef1, master=root)
button1 = tk.Button(frameAns, compound=tk.TOP, image=photo1, text=str(value[i]) + '\nClose me!', bg='white') #pulls up button onto which the image is pasted
button1.grid(sticky=tk.NW, padx=2, pady=2) #places button on grid
button1.image=photo1
root.mainloop()
最後に、1 つまたは 2 つの画像が表示され、次のエラーが表示されます。
TclError: 「image」コマンドを呼び出せません: アプリケーションが破棄されました
何が悪いのかわかりません。画像コマンドを移動できません。何らかの方法で「保存」して、破棄されないようにする必要があります。ここに他のコード エラーがあることはわかっていますが、取得している TclError を把握できれば、他のすべてを正しく設定できると思います。
これをすべて行う簡単な方法があれば教えてください!