2016-12-06 20 views
0

PILを使ってイメージを調べ、サイズ、色数、dpiなどを返すプログラムを作成しましたが、今は自分のコードをGUIに入れたいシステムを使用してユーザーを支援します。Python3とtkinter GUIボタンを使ってファイルを開く

私は関数内でaskopenfilename()を使用しましたが、新しいファイルを開くときに問題が発生しています。私は、プログラムが起動した後に関数を実行し、ファイルを選択して正常に動作させることができます。ボタンをクリックして新しいファイルを開くと、新しいファイルを選択できますが、表示された情報は変更されません。

新しいファイルを選択した後、新しい情報で画面を更新するにはどうすればよいですか?

def openPattern(): 
    global fileName 
    path = askopenfilename() 
    fileOpen = open(path, 'r') 
    fileName = os.path.basename(path) 

if __name__ == '__main__': 
    root = Tk() 
    root.title("Art Intake | Developer Build") 
    ms = MainScreen(root) 
    ms.config(bg="grey") 

    openPattern() 
    pattern = Button(ms, text="Choose a file", command=openPattern, 
       highlightbackground='grey') 
    pattern.pack() 
    pName = Label(ms, text="Pattern Name: " + str(fileName), 
       bg='grey') 
    pName.pack() 

    read = Button(ms, text="ReadMe", command=openRM, 
       highlightbackground='grey') 
    read.place(rely=1.0, relx=1.0, x=-25, y=-15, anchor=SE) 

    quit = Button(ms, text="Quit", command=ms.quit, 
      highlightbackground='grey') 
    quit.place(rely=1.0, relx=1.0, x=-25, y=-45, anchor=SE) 

root.mainloop() 
+1

ショーコード。コードなしでは広すぎる質問です。 BTW:多少のエラーメッセージが表示され、うまく動作しない可能性があります。コンソール/ターミナル/ cmd.exe/powershell – furas

+0

show [minimal、but complete] )コード。あなたの答えを編集し、コメントにコードを投稿しないでください。 –

+0

質問を編集してコードを追加してください。コメントはコードのための良い場所ではありません。 – furas

答えて

0

ラベルpNameopenPattern()を実行開始時に作成され、ラベルが表示するfileNameを持っている:ここで私が持っているコードです。しかし、後で手動でラベル内のテキストを変更する必要があります。

pName['text'] = "Pattern Name: " + fileName 

まずそれが既存のラベルのテキストを更新することができ、私は空のラベルを作成し、後で私はopenPattern()を実行します。ボタンをクリックすると、openPattern()は同じことを行います - 既存のラベルのテキストを更新します。 (コードがincompletたため試験されていない)

def openPattern(): 
    path = askopenfilename() 
    fileOpen = open(path, 'r') 
    fileName = os.path.basename(path) 

    pName['text'] = "Pattern Name: " + fileName 

if __name__ == '__main__': 
    root = Tk() 
    root.title("Art Intake | Developer Build") 

    ms = MainScreen(root) 
    ms.config(bg="grey") 

    pattern = Button(ms, text="Choose a file", command=openPattern, 
       highlightbackground='grey') 
    pattern.pack() 
    pName = Label(ms, bg='grey') 
    pName.pack() 

    read = Button(ms, text="ReadMe", command=openRM, 
       highlightbackground='grey') 
    read.place(rely=1.0, relx=1.0, x=-25, y=-15, anchor=SE) 

    quit = Button(ms, text="Quit", command=ms.quit, 
      highlightbackground='grey') 
    quit.place(rely=1.0, relx=1.0, x=-25, y=-45, anchor=SE) 

    openPattern() 

    root.mainloop() 
+0

これは私の問題を修正し、今は美しく動作します。ありがとうございました。 – nmoore146

関連する問題