将变量/标签添加到列表作为元组

时间:2016-11-08 16:04:29

标签: python list tkinter label

我希望在列表中记录由函数生成的标签。这完全是为了以后使用label.place_forget()来删除它们。因为在调用函数时会产生很多标签,所以我多次调用函数。我在函数外部创建了我的列表,然后在我的函数内部调用了list.extend()来将变量/标签添加到列表中。但是,当我打印列表时,在调用函数之后,会出现变量的内存位置。这是我的代码......

import sys, tkinter, csv
from tkinter import *

playerRows = []

def playerTab(team, name, pos, pts, reb, ast, stl, blk, to, y):
        global playerRows
        #Print the team
        playerTeam = Label(statWindow, bg = "white", text = team)
        playerTeam.config(height = 1, width = 13)
        playerTeam.place(x=20,y=y)
        #Print the name
        playerName = Label(statWindow, bg = "white", text = name)
        playerName.config(height = 1, width = 25)
        playerName.place(x=119,y=y)
        #Print the players position
        playerPosition = Label(statWindow, bg = "white", text = pos)
        playerPosition.config(height = 1, width = 4)
        playerPosition.place(x=302,y=y)
        #Print the players average points
        playerPoints = Label(statWindow, bg = "white", text = pts)
        playerPoints.config(height = 1, width = 4)
        playerPoints.place(x=338,y=y)
        #Print the players average rebounds
        playerRebounds = Label(statWindow, bg = "white", text = reb)
        playerRebounds.config(height = 1, width = 4)
        playerRebounds.place(x=374,y=y)
        #Print the players average assists
        playerAssists = Label(statWindow, bg = "white", text = ast)
        playerAssists.config(height = 1, width = 4)
        playerAssists.place(x=410,y=y)
        #Print the players average steals
        playerSteals = Label(statWindow, bg = "white", text = stl)
        playerSteals.config(height = 1, width = 4)
        playerSteals.place(x=446,y=y)
        #Print the players average blocks
        playerBlocks = Label(statWindow, bg = "white", text = blk)
        playerBlocks.config(height = 1, width = 4)
        playerBlocks.place(x=482,y=y)
        #Print the players average turnovers
        playerTurnovers = Label(statWindow, bg = "white", text = to)
        playerTurnovers.config(height = 1, width = 4)
        playerTurnovers.place(x=518,y=y)


        playerRows.extend((playerTeam, playerName, playerPosition, playerPoints, playerRebounds, playerAssists, playerSteals, playerBlocks, playerTurnovers))
        return playerTeam, playerName, playerPosition, playerPoints, playerRebounds, playerAssists, playerSteals, playerBlocks, playerTurnovers





playerTab(row['Team'], (row['First Name'] + ' ' + row['Last Name']), row['Position'], row['Average PTS'], row['Average REB'], row['Average AST'], row['Average STL'], row['Average BLK'], row['Average TO'], 120)

当我打印打印列表时,我可以看到添加到列表中的值是......

[(<tkinter.Label object at 0x00000000035E8F28>, <tkinter.Label object at 0x00000000035E8B70>, <tkinter.Label object at 0x00000000035E8BE0>)]

因此,如何在列表中保存标签,以便我可以在以后引用它们以便将其删除。

1 个答案:

答案 0 :(得分:0)

由于您的列表包含窗口小部件实例,并且您没有显式转换为字符串,因此python必须隐式地将对象转换为字符串。当它这样做时,它会打印一个像<tkinter.Label object at 0x00000000035E8F28>这样的标签。 Python无法知道你真正想要的是“text”配置选项的价值。

如果要打印标签文本,则必须为每行中的每个元素调用cget方法:

for row in playerRows:
    data = [label.cget("text") for label in row]
    print(data)