2017-08-10 97 views
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))) 
+3

我还没有仔细检查你的代码,但你为什么不把所有的东西从'FPL = fpl.replace(“‘’”)'起了'submit'函数内? –

+0

请你可以查看这个,并相应地修改https://stackoverflow.com/help/mcve –

+0

目前还不清楚你到底想要做什么'fpl'格式化,你想用'fpl'做什么其格式。这个信息会有很大的帮助。并且请在你的代码中修改你的注记。 –

回答

0

由于PM 2Ring说,如果你把这个

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

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

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

submit功能,当您单击按钮,它将会运行。 你也不需要用点fpllist = fpl.split()替换空格会给你相同的结果。

你真该学学OOP(谷歌,如果你不知道它是什么),它会使你的程序更具可读性,您可以在不同势函数分割你的程序的不同势部分。