【发布时间】:2017-08-10 13:02:41
【问题描述】:
我试图让它在使用按钮(命令提交)时设置变量fpl。问题是,它在用户点击按钮之前贯穿了整个事情。我怎样才能让脚本开始?
#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函数中呢? -
请您查看并相应修改stackoverflow.com/help/mcve
-
目前尚不清楚您究竟想将
fpl格式化为什么,以及格式化后您想对fpl做什么。此信息将有很大帮助。请在代码中修正缩进。
标签: python python-3.x user-interface tkinter