2017-08-10 25 views
0

ボタン(コマンド送信)を使用したときに変数fplが設定されるようにしようとしています。問題は、ユーザーがボタンを押す前にすべてを実行することです。スクリプトをどのようにして始めるのですか?Tkinterボタンが押されたときにPythonを実行する

#Convert string to list 
fpl = fpl.replace(" " , ".") 

ユーザーがボタンを押した後にのみ実行されますか?以下のコードの残りの部分。 PM 2Ringとして

import json 
from tkinter import * 

def submit(): 
    fpl = entry.get() 

window = Tk() 
window.title ("Infinite FMS") 

Label (window, text="Enter your flight plan 
here:").grid(row=0,column=0,sticky=W) 

entry = Entry(window, width = 75, bg = "light blue") 
entry.grid(row=1,column=0,sticky=W) 

Button (window, text="Submit", 
command=submit).grid(row=2,column=0,sticky=W) 

output = Text(window,width=75,height=6,background="light 
blue") 
output.grid(row=3,column=0,sticky=W) 

#Convert string to list 
fpl = fpl.replace(" " , ".") 

"[" + fpl + "]" 

fpllist = fpl.split(".") 
#Length of fpllist 
fpllen = len(fpllist) 

#Loop through the flight plan missing first and last 
entries 
n = 1 
Invalid = [] 
while n < fpllen - 1: 
    #Check for item in file 
    searchTerm = (fpllist[n]) 
    searchTermF = "\'Name\': \'" + searchTerm + "\'" 
    searchTermV = "\'identifier\': \'" + searchTerm + "\'" 
    file = open('mydirectory', 'r') 
    data1 = json.load(file) 
    file.close() 
    file = open('mydirectory', 'r') 
    data2 = json.load(file) 
    file.close() 
    if searchTermF in str(data1): 
     Validity = "Your route is valid!" 
     n = n + 1 
    elif searchTermV in str(data2): 
     Validity = "Your route is valid!" 
     n = n + 1 
    else: 
     Validity = "Your route is invalid!\nInvalid 
waypoints:" 
    Invalid.append(searchTerm) 
    n = n + 1      
if len(Invalid) == 0: 
    print (Validity , ', '.join(Invalid)) 
else: 
    Validity = "Your route is invalid!\nInvalid 
waypoints:" 
    print (Validity , ', '.join(Invalid)) 

output.delete(0.0, END) 
output.insert(END, (Validity , ', '.join(Invalid))) 
+3

私はあなたのコードを詳細に調べていませんが、 'fpl = fpl.replace(" "、"。 ")'以降は 'submit'関数の中に入れてください。 –

+0

これを見直してそれに応じて修正することができますhttps://stackoverflow.com/help/mcve –

+0

'fpl'をどのような形式で取得しようとしているのか、また' fpl'で何をしたいのかはっきりしませんそのフォーマット。この情報はかなり役立ちます。そしてあなたのコードにあなたのインデントを修正してください。 –

答えて

0

あなたがボタンをクリックしたときに、それが実行されますあなたのsubmit機能でこの

#Convert string to list 
fpl = fpl.replace(" " , ".") 

"[" + fpl + "]" #what is this supposed to do? 

fpllist = fpl.split(".") 
#Length of fpllist 
fpllen = len(fpllist) 

を置く場合と述べました。 また、スペースをポイントに置き換える必要はありませんfpllist = fpl.split()は同じ結果をもたらします。

あなたは本当にあなたがそれが何であるかわからない場合、それはあなたのプログラムをより読みやすくし、異なる機能であなたのプログラムの異なる部分を分割することができますOOPを(Googleそれgoogle)。

関連する問題