简体   繁体   English

Tkinter中标签重叠的更多问题

[英]More Issues with overlapping labels in Tkinter

I am having a problem with Tkinter. 我对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 有时我想打印A,B,C列有时我想打印A,B,C,D列有时我想打印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... 有些查询给出1。有些给出200。要打印它们,我做这样的事情...

            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. 我将其放在按钮代码的开头,以便如果labelList已经存在,它将在每个查询中执行此操作。 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. 如果您有许多小部件,则可以将ID存储在一个列表中并销毁每个ID,或者将它们放置在新框架中并每次销毁该框架,这也将删除其中的对小部件的引用。 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. 就个人而言,我将为一个查询的所有结果重用标签,即为每个查询附加一个textvariable,然后为找到的每个记录将textvariable设置为新值;如果为空,则将其设置为“”。 See http://effbot.org/tkinterbook/label.htm for info on the textvariable option for labels. 有关标签的textvariable选项的信息,请参见http://effbot.org/tkinterbook/label.htm

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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