我是 python 的新手,我正在尝试创建一个用于桁架分析的简单 GUI 应用程序。问题是在我在文本小部件中输入数据并将其保存在 txt 文件中后,数据带有我无法删除的单引号.我尝试使用 .strip 但仍然无法正常工作。我得到以下输出;
nodes = {'1': ['0', '0'], '2': ['1', '1'], '3': ['2', '2'], '4': ['3', '3'], '5': ['4', '4'], '6': ['5', '5']}
虽然我期待这个输出;
nodes = {1: [0, 0], 2: [1, 1], 3: [2, 2], 4: [3, 3], 5: [4, 4'], 6: [5, 5]}
我的代码在下面,对代码的庞大感到抱歉...有人可以帮我解决这个问题吗?提前致谢。
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()