在运行另一个查询功能之前从窗口中清除数据

时间:2019-10-30 19:56:36

标签: python tkinter treeview ttk

我有一个GUI连接到数据库,我需要显示一些数据。

def student_department():

    tree["columns"] = ("Name", "DOB", "Department_name")
    tree.column("#0", width=100)
    tree.column("Name", width=100)
    tree.column("DOB", width=100)
    tree.column("Department_name", width=100)

    tree.heading("#0", text="Panther ID")
    tree.heading("Name", text="Name")
    tree.heading("DOB", text="Birth year")
    tree.heading("Department_name", text="Department name")

    sql = "select * from Student"

    cursor.execute(sql)
    result = cursor.fetchall()

    for i in range(0, len(result)):
        tree.insert("", i, text=str(result[i][0]), values=(str(result[i][1]), str(result[i][2]), str(result[i][3])))


def instructor_info():
    tree["columns"] = ("Name", "Department_ID")
    tree.column("#0", width=100)
    tree.column("Name", width=100)
    tree.column("Department_ID", width=100)

    tree.heading("#0", text="Instructor ID")
    tree.heading("Name", text="Name")
    tree.heading("Department_ID", text="Department ID")

    sql = "select * from Instructor"
    cursor.execute(sql)
    result = cursor.fetchall()

    for i in range(0, len(result)):
        tree.insert("", i, text=str(result[i][0]), values=(str(result[i][1]), str(result[i][2])))

我有两个连接到这两个功能的按钮

btn1 = tk.Button(root, text="Student-Department", command=student_department).pack()
btn2 = tk.Button(root, text="Instructor_info", command=instructor_info).pack()

两个都可以找到,但是当我单击1按钮然后单击2按钮时,窗口中仍然有数据。当我单击按钮时,数据将添加到显示中,但先前的数据不会被清除。单击按钮,然后显示新数据时,如何获取要清除的数据。

1 个答案:

答案 0 :(得分:-1)

我确定答案应该是在插入新数据之前修改搜索的方法,并确保字段或显示清晰无误,

尝试查看是否可以找到一个名为.clear的方法。在插入新数据之前使用它。