【发布时间】:2020-12-29 09:04:55
【问题描述】:
在 tkinter 循环中,我有一个变量列表,用于存储用户输入的文本字符串。我在 tkinter 中使用 .get 方法将 tkinter 文本变量分配给常规 python 变量。然后我将这些变量传递给一个函数并返回它们。但它变得相当混乱。
它在技术上有效,但我想让它在眼睛上更好看。我包含了check_inputs()(这是checkInputsMaster.py 中的一个文件)来显示变量是如何传递和返回给其他函数的。所有其他功能都是这样设计的。
主要:
# obtains user inputted variables from GUI and feeds them into all other modules
folderPath,ship,flightNumber,flightDate,testNumber,missionNumber,pilot,tc,ops,missionType,etd,eta,sw,tm,gps,rdr,rtas,rswb,rmwb,lswb,lmwb,slam = check_inputs(folder_path,ship1,flight_number,flight_date,test_number,mission_number,pilot1,tc1,ops1,mission_type,etd1,eta1,sw1,var1,var2,var3,var4,var5,var6,var7,var8,var9)
flightParameters = [folderPath,ship,flightNumber,flightDate,testNumber,missionNumber,pilot,tc,ops,missionType,etd,eta,sw,tm,gps,rdr,rtas,rswb,rmwb,lswb,lmwb,slam]
# test command print(flightParameters)
# main chunk of code that creates paperwork
check_errors(*flightParameters)
logger(*flightParameters)
check_inputs() 中的checkInputsMaster.py 函数:
def check_inputs(folder_path,ship1,flight_number,flight_date,test_number,mission_number,pilot1,tc1,ops1,mission_type,etd1,eta1,sw1,var1,var2,var3,var4,var5,var6,var7,var8,var9):
print('--------------------------\nProgram starting.\nGrabbing User Input Variables')
global folderPath
global ship
global flightNumber
global flightDate
global testNumber
global missionNumber
global pilot
global tc
global ops
global missionType
global eta
global etd
global sw
global tm
global gps
global rdr
global rtas
global rswb
global rmwb
global lswb
global lmwb
global slam
folderPath=folder_path.get()
ship=ship1.get()
flightNumber=flight_number.get()
flightDate=flight_date.get()
testNumber=test_number.get()
missionNumber=mission_number.get()
pilot=pilot1.get()
tc=tc1.get()
ops=ops1.get()
missionType=mission_type.get()
etd=etd1.get()
eta=eta1.get()
sw=sw1.get()
tm=var1.get()
gps=var2.get()
rdr=var3.get()
rtas=var4.get()
rswb=var5.get()
rmwb=var6.get()
lswb=var7.get()
lmwb=var8.get()
slam=var9.get()
print('Variables have been grabbed')
return(folderPath,ship,flightNumber,flightDate,testNumber,missionNumber,pilot,tc,ops,missionType,etd,eta,sw,tm,gps,rdr,rtas,rswb,rmwb,lswb,lmwb,slam)
【问题讨论】:
-
你能试试把所有的变量都放在字典里吗?
-
避免全局变量(或至少减少它们的数量)的常用方法是使用一些短容器(如列表和字典),或者通常更好的是,定义一个自定义类来保存(并更新)他们。
-
你为什么将它们声明为全局并且还作为参数传递它们?这会导致代码混乱。