简体   繁体   中英

More Issues with overlapping labels in Tkinter

I am having a problem with Tkinter. The issue is as follows.

I retrieve information from a Database. I wish to print that out based off a few user selections. Sometimes I want to print out columns A, B, C Sometimes I want to print out columns, A, B, C, D Sometimes I want to print out columns A, B, D, E

Also, when I query, I do not know ahead of time how many results I will have. Some queries give 1. Some give 200. To print them, I do something like this...

            while x < results.num_rows() :
                result = results.fetch_row()
                author = result[0][0]
                title = result[0][1]
                conference = result[0][2]
                year = result[0][3]
                location = result[0][4]
                Label(root, text=title).grid(row=startRow,column=0,columnspan=5,sticky=W)
                Label(root,text=author).grid(row=startRow,column=6,columnspan=1,sticky=W)
                Label(root,text=year).grid(row=startRow,column=7,columnspan=1,sticky=W)
                Label(root,text=conference).grid(row=startRow,column=8,columnspan=1,sticky=W)
                Label(root,text=location).grid(row=startRow,column=9,columnspan=1,sticky=W)
                startRow+=1
                x+=1

This accomplishes what I want. I get a label printed below the previous for each item. It prints the desired columns (in this case, title, author, year, conference, location). Sometimes its different. It's all good.

Until my user does another query. I can't delete the previous labels. I have no reference to them. I can't make a reference to them, because I dont know ahead of time how many I will need. The number of labels is based off of the query the user wants and obviously the matching entries in the database.

So when the user does the 2nd or 3rd query, results start to get printed on top of one another.

What can I do?

EDIT - This was the attempt. I added in

    try :
        for item in labelList :
            item.grid_forget()
    except :
        pass

I put that at the beginning of my button code, so that if the labelList already exists, it will do this at each query. If it doesn't exist, it will just pass.

            while x < results.num_rows() :
                result = results.fetch_row()
                title = result[0][0]
                conference = result[0][1]
                year = result[0][2]
                location = result[0][3]
                message = title + "\t\t\t\t\t" + conference + "\t" + str(year) + "\t" + location
                label = Label(root, text=title)
                label.grid(row=startRow, column=0, columnspan=5,sticky=W)
                labelList.append(label)
                label = Label(root, text=conference)
                label.grid(row=startRow, column=6, columnspan=1,sticky=W)
                labelList.append( label )
                label = Label(root, text=year)
                label.grid(row=startRow, column=7, columnspan=1,sticky=W)
                labelList.append( label )
                label = Label(root, text=location)
                label.grid(row=startRow, column=8, columnspan=1,sticky=W)
                labelList.append( label )
                startRow+=1
                x+=1

However I am still getting the overlapping issue.

If you have a lot of widgets, you can store the ID's in a list and destroy each one, or place them in a new frame and destroy the frame each time, which also removes the reference to the widget's in it. Personally, I would reuse the labels for all of the results from one query, ie attach a textvariable to each one, then set the textvariable to the new value for each record found, or "" if it is empty. See http://effbot.org/tkinterbook/label.htm for info on the textvariable option for labels.

def next_set_of_recs():
    print "next set"
    old_frame.destroy()

top = tk.Tk()
tk.Label(top, text="not in created frame").grid()

old_frame=tk.Frame(top)
old_frame.grid(row=1)
tk.Label(old_frame, text="a_label", width=50).grid()
## give it some time, otherwise it is instantly destroyed
top.after(2000, next_set_of_recs)  

top.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM