2017-06-10 22 views
0

イメージボタンを作っています。私のコードは以下の通りです。python tkinter画像ボタンの使い方は?

button1=Button(root,width=80,height=200) 
image1=PhotoImage(file="/home/imagefolder/1.png") 
button1.config(image=self.image1) 
button1.image=image1 
button.pack(side=left) 

私はファイル "1.png"を持っていないとき、私は空としてボタンを設定したいと思います。しかし、今私はエラーがあります:

tkinter.TclError: couldn't open "/...". no such file or directory.

どうすれば解決できますか?

答えて

0

あなただけのエラーチェックを追加することができます。

button1=Button(root,width=80,height=200) 
try: 
    image1=PhotoImage(file="/home/imagefolder/1.png") 
    button1.config(image=self.image1) 
    button1.image = image 
except TclError: 
    pass 
button1.pack(side=left) 
+0

は、それがうまく働きました。ありがとうございました :) – Kmin

関連する問題