我正在为桁架分析创建一个简单的应用程序,我必须收集一些数据并将其保存在文本文件中。根据我的代码,我最多只能解决 6 个节点(6 个输入框)。但是当我不填写所有框时它还将空框保存为空字符串。例如,如果我为 3 个节点(即前 3 个框)放置坐标。我得到以下信息;
nodes = {'1': ['0', '0'], '2': ['1', '1'], '3': ['2', '2'], '': ['', '']}
如何将我的保存按钮编程为仅保存用户填写的数据,仅此而已?
我的代码如下;
from tkinter import*
root = Tk()
root.geometry("480x480")
root.title ("TRUSS 2D")
# creating coordinates input
myLabel = Label(root,text = "Coordinates")
myLabel.grid (row = 0,column = 0)
myLabel9 = Label(root,text = "Node")
myLabel9.grid (row = 1,column = 0, columnspan = 1 )
e_1 = Entry(root,width=5,borderwidth=5)
e_1.grid(row = 2,column = 0)
e_2 = Entry(root,width=5,borderwidth=5)
e_2.grid(row = 3,column = 0)
e_3 = Entry(root,width=5,borderwidth=5)
e_3.grid(row = 4,column = 0)
e_4 = Entry(root,width=5,borderwidth=5)
e_4.grid(row = 5,column = 0)
e_5 = Entry(root,width=5,borderwidth=5)
e_5.grid(row = 6,column = 0)
e_6 = Entry(root,width=5,borderwidth=5)
e_6.grid(row = 7,column = 0)
myLabel_x0 = Label(root,text = "x0")
myLabel_x0.grid (row = 2,column = 1)
e_x0 = Entry(root,width=5,borderwidth=5)
e_x0.grid(row = 2,column = 2)
myLabel_y0 = Label(root,text = "y0")
myLabel_y0.grid (row = 2,column = 3)
e_y0 = Entry(root,width=5,borderwidth=5)
e_y0.grid(row = 2,column = 4)
myLabel_x1 = Label(root,text = "x1")
myLabel_x1.grid (row = 3,column = 1)
e_x1 = Entry(root,width=5,borderwidth=5)
e_x1.grid(row = 3,column = 2)
myLabel_y1 = Label(root,text = "y1")
myLabel_y1.grid (row = 3,column = 3)
e_y1 = Entry(root,width=5,borderwidth=5)
e_y1.grid(row = 3,column = 4)
myLabel_x2 = Label(root,text = "x2")
myLabel_x2.grid (row = 4,column = 1)
e_x2 = Entry(root,width=5,borderwidth=5)
e_x2.grid(row = 4,column = 2)
myLabel_y2 = Label(root,text = "y2")
myLabel_y2.grid (row = 4,column = 3)
e_y2 = Entry(root,width=5,borderwidth=5)
e_y2.grid(row = 4,column = 4)
myLabel_x3 = Label(root,text = "x3")
myLabel_x3.grid (row = 5,column = 1)
e_x3 = Entry(root,width=5,borderwidth=5)
e_x3.grid(row = 5,column = 2)
myLabel_y3 = Label(root,text = "y3")
myLabel_y3.grid (row = 5,column = 3)
e_y3 = Entry(root,width=5,borderwidth=5)
e_y3.grid(row = 5,column = 4)
myLabel_x4 = Label(root,text = "x4")
myLabel_x4.grid (row = 6,column = 1)
e_x4 = Entry(root,width=5,borderwidth=5)
e_x4.grid(row = 6,column = 2)
myLabel_y4 = Label(root,text = "y4")
myLabel_y4.grid (row = 6,column = 3)
e_y4 = Entry(root,width=5,borderwidth=5)
e_y4.grid(row = 6,column = 4)
myLabel_x5 = Label(root,text = "x5")
myLabel_x5.grid (row = 7,column = 1)
e_x5 = Entry(root,width=5,borderwidth=5)
e_x5.grid(row = 7,column = 2)
myLabel_y5 = Label(root,text = "y5")
myLabel_y5.grid (row =7 ,column = 3)
e_y5 = Entry(root,width=5,borderwidth=5)
e_y5.grid(row = 7,column = 4)
# add data to text file
def save():
node_1 = e_1.get()
node_2 =e_2.get()
node_3 =e_3.get()
node_4 =e_4.get()
node_5 =e_5.get()
node_6 =e_6.get()
x0 = e_x0.get()
y0 = e_y0.get()
x1 = e_x1.get()
y1 = e_y1.get()
x2 = e_x2.get()
y2 = e_y2.get()
x3 = e_x3.get()
y3 = e_y3.get()
x4 = e_x4.get()
y4 = e_y4.get()
x5 = e_x5.get()
y5 = e_y5.get()
line = str({node_1:[x0,y0],node_2:[x1,y1],node_3:[x2,y2],node_4:[x3,y3],node_5:[x4,y4],node_6:[x5,y5]})
name_string = line.strip('\"')
print("nodes = " + line,name_string,file = open("input.txt","a"))
file.close()
btn=Button(root, text="save", command = save).grid(row=8,column = 4)
root.mainloop()