0
我試圖讓這個變量fpl
是當按鈕(命令提交)設定的Python腳本運行。問題是,它在用戶點擊按鈕之前貫穿整個事情。我如何讓劇本開始?Tkinter的時候按下按鈕
#Convert string to list
fpl = fpl.replace(" " , ".")
只有在用戶按下按鈕後才運行?下面的代碼的其餘部分。
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)))
我還沒有仔細檢查你的代碼,但你爲什麼不把所有的東西從'FPL = fpl.replace(「‘’」)'起了'submit'函數內? –
請你可以查看這個,並相應地修改https://stackoverflow.com/help/mcve –
目前還不清楚你到底想要做什麼'fpl'格式化,你想用'fpl'做什麼其格式。這個信息會有很大的幫助。並且請在你的代碼中修改你的註記。 –