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

2024-09-29 07:25:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用不同的坐标系来计算两条线之间的距离,就像用Python写的程序一样。我正在使用tkinter创建窗口。你知道吗

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

Screenshot 你知道吗

左上角的框架(新数据框架)是您选择要为后续计算添加的数据类型(线、平面…)的位置。通过单击“weiter”-buttton(=继续按钮),不同的条目被添加到新数据框内的浅灰色框中。你知道吗

Screenshot 你知道吗

然后可以添加所有必要的数据。你知道吗

我使用了一个额外的框架,因为在点击“hinzufügen”按钮(=添加数据按钮)之后,这个额外框架内的所有小部件都被删除了(我使用了一个for循环,从这个框架中删除所有的子项)。在那之后,浅灰色的框架是空的,所以你可以添加更多的数据。你知道吗

问题是,当单击adddata按钮(~hinzufügen)时,还应该使用命令将浅灰色框中不同条目Widges中的所有数据添加到列表中列表.append(“随机的东西”)。我可以访问列表并添加随机数据,但不能使用get()-方法从浅灰色框架内的入口获取数据。你知道吗

以下是一些代码:

# 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

当单击位于浅灰色框架外的按钮时,如何从浅灰色框架内的不同窗口小部件访问数据?你知道吗


Tags: the数据text框架inputplacelabelbg
1条回答
网友
1楼 · 发布于 2024-09-29 07:25:55

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

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

这些都不是特金特独有的。简而言之,要从一个对象获取值,必须有一个对该对象的引用。有很多方法可以完成这项任务。你知道吗

相关问题 更多 >