我一直在尝试在Python中创建一个GUI,它将3个数字作为用户输入,并在3D散点图中绘制图形。我已经成功地通过tkinter创建了GUI,但是当涉及到实际的图形时我很困惑。我想为x,y和z变量定义一个空列表,然后用任何用户输入附加它们。然后我会采用这些变量并使用matplotlib图形。我无法像原计划那样做,所以我恢复了定义两个功能,每个功能都绑定到他们自己的按钮。 “存储”按钮将用户输入记录到预先创建的相应的empyt列表中并清除输入框。然后,用户可以点击图形按钮,列表将以3D图形绘制。这是代码:
from tkinter import *
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
#store function
x_List = []
y_List = []
z_List = []
def store():
x_List.append(x_Var)
y_List.append(y_Var)
z_List.append(z_Var)
entry_1.delete(0, END)
entry_2.delete(0, END)
entry_3.delete(0, END)
def graph():
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = x_List
Y = y_List
Z = z_List
ax.scatter(X, Y, Z, c='b', marker='o')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
home = Tk()
home.title("Graph")
homeLabel = Label(home, text="Graph Program").grid(row=0, column=1)
home.minsize(600,600)
home.maxsize(600,600)
#entry boxes and labels
intel = StringVar()
person = StringVar()
attractive = StringVar()
label_1 = Label(home, text="X:").grid(row=2, column=0)
label_2 = Label(home, text="Y:").grid(row=3, column=0)
label_3 = Label(home, text="Z:").grid(row=4, column=0)
entry_1 = Entry(home, textvariable=x_Var)
entry_1.grid(row=2, column=1)
entry_2 = Entry(home, textvariable=y_Var)
entry_2.grid(row=3, column=1)
entry_3 = Entry(home, textvariable=z_Var)
entry_3.grid(row=4, column=1)
#buttons
storeButton = Button(home, text="Store", command=store).grid(row=5, column=0)
graphButton = Button(home, text="Graph", command=graph).grid(row=5, column=1)
home.mainloop()