2017-11-24 22 views
-1

私の人生にとっては、これがうまくいかない理由がわかりません。私はPythonで画像を表示するためにTKinterを使用しています。私が今までに書いたコードは、ここに表示されています:tkinterでフォームに画像が表示されない

from tkinter import * 
from tkinter import messagebox 

def unlock(): 
    root.withdraw() 


    def logout(): 
     inside.destroy() 
     root.deiconify() 

    ####################### 
    ###     ### 
    ### unlock Form ### 
    ###     ### 
    ####################### 


    inside = Tk() 
    inside.geometry("576x576") 
    inside.title("SAFE CRACKER") 
    # LABELS, Textboxes and Buttons 
    imageInside = PhotoImage(file = "images/inside.gif") 
    imageLabel = Label(inside,image = imageInside).grid(row =1, columnspan = 2) 
    label = Label(inside, text="Safe Cracker", font = ("Arial",16)).grid(row = 0, columnspan = 2) 
    exitButton = Button(inside, text = "Exit", width = 15, command = logout) 
    exitButton.grid(row = 3, column = 0,padx = 10, pady = 10) 



####################### 
###     ### 
### Main Form ### 
###     ### 
####################### 


root = Tk() 
root.geometry("430x450") 
root.title("SAFE CRACKER") 
# LABELS, Textboxes and Buttons 
label = Label(root, text="Safe Cracker", font = ("Arial",16)).grid(row = 0, columnspan = 2) 
imageSafe = PhotoImage(file = "images/safe.gif") 
imageLabel = Label(root,image = imageSafe).grid(row =1, columnspan = 2) 
label = Label(root, text = "Enter the code", font = ("Arial",12)).grid(row = 2, column = 0) 
unlockCode = Entry(root, width = 30) 
unlockCode.grid(row = 2, column = 1,padx = 10, pady = 10) 
exitButton = Button(root, text = "Exit", width = 15, command = exit).grid(row = 3, column = 0,padx = 10, pady = 10) 
enterButton = Button(root, text = "Enter the VAULT", width = 15, command = unlock).grid(row = 3, column = 1,padx = 10, pady = 10) 

コードはあまりうまくいきませんが、私が取り組んでいるものです。私がプログラムを実行すると、セーフ(グレート)の画像が表示され、ボタンをクリックすると次のフォームに移動します。画像コードは、それを削除されたときに、画像、ラベルとボタンが表示されない新しいフォームで

image of safe working

は、しかし、すべてがswimmingly動作します。

最初は、関数内にルートフォームを置くことを考えましたが、関数内にこのコードを配置するとイメージ(ahhhh)が読み込まれません。これらの画像を機能内に配置することはできませんか?

+0

エラーが発生し、フォームを表示できない場合があります。エラーメッセージが表示されるかどうかをコンソールで確認しましたか? – furas

+0

ところで、 'grid =' Widget(...)。grid() 'を実行すると' grid() '(そして' pack() ')は' None'を返すので、 'var'に' None'を代入します。ウィジェット。ウィジェットにアクセスするために 'var'が必要な場合は、' var = Widget(...) 'と' var.grid() 'という2行のコードで行う必要があります。後で 'var'を使う必要がなければ、' var'を使わずにウィジェットを作ることができます。それは 'ウィジェット(...)を意味します。grid()' – furas

+0

'tkinter'はメインウィンドウを作るのに' Tk() 'だけを使うべきです。 'Toplevel()'で作成しなければならない他のウィンドウ – furas

答えて

1

写真を参照する必要があります。

imageInside = PhotoImage(file = "images/inside.gif") 
imageLabel = Label(inside,image = imageInside) 
imageLabel.grid(row =1, columnspan = 2) 
imageLabel.photo_ref = imageInside # keep a reference! 
+0

助けを借りていますが動作しません。 imageLabel =ラベル(inside、image = imageInside) ファイル「C:¥Program Files¥Python36¥lib¥tkinter¥__ init__.py」2760行目__init__ ウィジェット.__ init __(自己、マスター、 'label'、cnf、kw) ファイル "C:¥Program Files¥Python36¥lib¥tkinter¥__ init__.py"、2293行目、__init__ (widgetName、self._w)+余分な+ self._options(cnf )) _tkinter.TclError:image "pyimage2"は存在しません –

+0

@NeosNokiaあなたはあなたの質問にそれを編集する必要があります。 – Nae

0

ありがとうございました。

このコードが動作することを確認できます。

from tkinter import * 
from tkinter import messagebox 

def unlock(): 
    root.withdraw() 


    def logout(): 
     inside.destroy() 
     root.deiconify() 

    ####################### 
    ###     ### 
    ### unlock Form ### 
    ###     ### 
    ####################### 


    inside = Toplevel() 
    inside.geometry("576x576") 
    inside.title("SAFE CRACKER") 
    # LABELS, Textboxes and Buttons 
    imageInside = PhotoImage(file = "images/inside.gif") 
    imageLabel = Label(inside,image = imageInside) 
    imageLabel.grid(row =1, columnspan = 2) 
    imageLabel.photo_ref = imageInside 
    label = Label(inside, text="Safe Cracker", font = ("Arial",16)).grid(row = 0, columnspan = 2) 
    exitButton = Button(inside, text = "Exit", width = 15, command = logout) 
    exitButton.grid(row = 3, column = 0,padx = 10, pady = 10) 



####################### 
###     ### 
### Main Form ### 
###     ### 
####################### 


root = Tk() 
root.geometry("430x450") 
root.title("SAFE CRACKER") 
# LABELS, Textboxes and Buttons 
label = Label(root, text="Safe Cracker", font = ("Arial",16)).grid(row = 0, columnspan = 2) 
imageSafe = PhotoImage(file = "images/safe.gif") 
imageLabel = Label(root,image = imageSafe).grid(row =1, columnspan = 2) 
label = Label(root, text = "Enter the code", font = ("Arial",12)).grid(row = 2, column = 0) 
unlockCode = Entry(root, width = 30) 
unlockCode.grid(row = 2, column = 1,padx = 10, pady = 10) 
exitButton = Button(root, text = "Exit", width = 15, command = exit).grid(row = 3, column = 0,padx = 10, pady = 10) 
enterButton = Button(root, text = "Enter the VAULT", width = 15, command = unlock).grid(row = 3, column = 1,padx = 10, pady = 10) 
関連する問題