单击按钮后,无法使用get() - 方法从另一帧的条目中获取数据

时间:2013-12-12 16:30:28

标签: python tkinter get frame tkinter-entry

我正在尝试用Python编写一个可以解决不同几何问题的程序,比如计算3D坐标系中两条线之间的距离。我正在使用tkinter来创建窗口。

以下是显示程序正在创建的窗口的屏幕截图:

Screenshot http://oi39.tinypic.com/2iv1wux.jpg

左上角(新数据框)中的框架是您选择要为后续计算添加的数据类型(线,平面......)的位置。通过单击“weiter”-buttton(= continue-button),可以将不同的条目添加到新数据框内的浅灰色框中。

Screenshot http://oi44.tinypic.com/vemyx1.jpg

然后,您可以添加所有必要的数据。

我正在使用一个额外的帧,因为在点击“hinzufügen” - 按钮(= add-data-button)之后,这个额外帧内的所有小部件都被删除了(我正在使用删除的for循环)来自这个框架的所有孩子)。之后,浅灰色框再次为空,因此您可以添加更多数据。

问题是当点击添加数据按钮(~hinzufügen)时,它还应该使用命令list.append(“randomstuff”将浅灰色框架中不同的Entry-Widges的所有数据添加到列表中。 “)。我可以访问列表并添加随机数据,但我不能使用get() - 方法从浅灰色框内的Entrys获取数据。

以下是一些代码:

# this is the function that creates the entries in the light grey frame
# the continuebutton (~weiter) is using this method after you click it
# li_datatype is the list where you can choose what kind of data to add.
# as you can see, it contains four elements.
# depending on the chosen elements, a certain amount of entries is created
# in the light grey frame

def data_input():
if li_datatype.get("active") == "Punkt":
        add.append(["DATATYPE","NAME","X","Y","Z"])
        #name
        lbl_text_1 = Label(f_input_2, text="Name des Punktes:", bg="#f8f8f8")
        lbl_text_1.place(relx=0.02, rely=0.1, anchor="w")
        entry_Name = Entry(f_input_2)
        entry_Name.place(relx=0.98, rely=0.1, anchor="e", width="110")

        #Info
        lbl_text_2 = Label(f_input_2, text="P( X / Y / Z )", bg="#f8f8f8")
        lbl_text_2.place(relx=0.02, rely=0.3, anchor="w")

        #Eingabe Punkt
        lbl_text_2 = Label(f_input_2, text="Punkt:", bg="#f8f8f8")
        lbl_text_2.place(relx=0.02, rely=0.45, anchor="w")

        lbl_X = Label(f_input_2, text="X:",font="8", bg="#f8f8f8")
        lbl_Y = Label(f_input_2, text="Y:",font="8", bg="#f8f8f8")
        lbl_Z = Label(f_input_2, text="Z:",font="8", bg="#f8f8f8")
        lbl_X.place(relx=0.02,rely=0.6,anchor="w")
        lbl_Y.place(relx=0.35,rely=0.6,anchor="w")
        lbl_Z.place(relx=0.67,rely=0.6,anchor="w")

        entry_X = Entry(f_input_2)
        entry_Y = Entry(f_input_2)
        entry_Z = Entry(f_input_2)
        entry_X.place(relx=0.13,rely=0.6,anchor="w", width="35")
        entry_Y.place(relx=0.46,rely=0.6,anchor="w", width="35")
        entry_Z.place(relx=0.78,rely=0.6,anchor="w", width="35")
elif (...)
# the other code basically does the same thing

# this is what happens after you click the "add-data-button" (~hinzufügen)

def data_add():
    a = entry_Name.get()
    print(a)

    for child in f_input_2.winfo_children():
        child.destroy()

# i first tried to save the info from the  name-entry inside a variable, but it doesn't work

当点击那个框架的那个按钮时,如何从浅灰色框内的不同小部件中访问数据?

1 个答案:

答案 0 :(得分:0)

从您的代码中可以看出,您使用本地变量来保存对小部件的引用。您需要执行以下操作之一:

  • 切换到面向对象的方法,以便您可以将对小部件的引用保存为实例属性
  • 将引用存储为全局变量
  • 让您的按钮将对窗口小部件的引用传递给其回调(假设该按钮是在同一范围内创建的)。

这些都不是tkinter独有的。简而言之,要从对象获取值,您必须具有对象的引用。有很多方法可以完成这项任务。