找到你要的答案

Q:Python Tkinter: Delete label not working

Q:Python Tkinter:删除标签不工作

I have a GUI that contains all the database records. Upon clicking the "search" button, I want all the records to be cleared from the screen, and the records relevant to the user's search to appear. I am trying to reference my labels and call the "destroy" method like this: a.destroy() but the program is not recognizing these labels as widgets.

class DBViewer:
    def __init__(self, rows):
        # Display all records
            row_for_records = 2
            for record in rows:
                a = tkinter.Label(self.main_window, text=record[0])
                a.grid(row=row_for_records, column=0)
                b = tkinter.Label(self.main_window, text=record[1])
                b.grid(row=row_for_records, column=1)
                # More labels here

            self.display_rows(rows)
            tkinter.mainloop()

    def display_rows(self, rows):
        # Where I'm having trouble

    def main():
        global db
        try:
            dbname = 'books.db'
            if os.path.exists(dbname):
                db = sqlite3.connect(dbname)
                cursor = db.cursor()
                sql = 'SELECT * FROM BOOKS'
                cursor.execute(sql)
                rows = cursor.fetchall()
                DBViewer(rows)
                db.close()

Edit: Separated widget assignment and grid but still facing have the same problem. Anyone want to help out a programming newbie?

我有一个包含所有数据库记录的图形用户界面。点击“搜索”按钮后,我想从屏幕上清除所有的记录,并记录与用户搜索相关的记录。我想引用我的标签和调用的方法这样的“破坏”:A. destroy()但程序不识别这些标签控件。

class DBViewer:
    def __init__(self, rows):
        # Display all records
            row_for_records = 2
            for record in rows:
                a = tkinter.Label(self.main_window, text=record[0])
                a.grid(row=row_for_records, column=0)
                b = tkinter.Label(self.main_window, text=record[1])
                b.grid(row=row_for_records, column=1)
                # More labels here

            self.display_rows(rows)
            tkinter.mainloop()

    def display_rows(self, rows):
        # Where I'm having trouble

    def main():
        global db
        try:
            dbname = 'books.db'
            if os.path.exists(dbname):
                db = sqlite3.connect(dbname)
                cursor = db.cursor()
                sql = 'SELECT * FROM BOOKS'
                cursor.execute(sql)
                rows = cursor.fetchall()
                DBViewer(rows)
                db.close()

编辑:分离小部件分配和网格,但仍然面临着同样的问题。有人想帮助一个编程的新手吗?

answer1: 回答1:

In order to delete something, you must have a reference to it. You aren't saving references to the widgets you are creating. Try saving them to a list or dictionary.

When creating them:

...
self.labels = []
for record in rows:
    a = Tkinter.Label(...)
    self.labels.append(a)
    ...

When you want to delete them:

for label in self.labels:
    label.destroy()

为了删除一些东西,你必须有一个参考。您没有保存对正在创建的部件的引用。尝试保存到列表或字典。

创建它们时:

...
self.labels = []
for record in rows:
    a = Tkinter.Label(...)
    self.labels.append(a)
    ...

当你想删除它们:

for label in self.labels:
    label.destroy()
python  tkinter