删除tkinter中的函数创建的标签

时间:2016-11-07 21:32:06

标签: python tkinter label labels

我有一个生成标签的函数,并使用.place()将它们放到窗口上。虽然我的程序后期我希望删除这些标签。我如何使用.place_forget()来隐藏所有标签?正如我试图调用.place_forget()但是只删除了其中一个标签。并且有几种此类标签。

以下是生成标签的函数:

def playerTab(team, name, pos, pts, reb, ast, stl, blk, to, y):
    global playerTeam, playerName, playerPosition, playerPoints, playerRebounds, playerAssists, playerSteals, playerBlocks, playerTurnovers
    #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)
    playerAssists = Label(statWindow, bg = "white", text = ast)
    playerAssists.config(height = 1, width = 4)
    playerAssists.place(x=410,y=y)
    playerSteals = Label(statWindow, bg = "white", text = stl)
    playerSteals.config(height = 1, width = 4)
    playerSteals.place(x=446,y=y)
    playerBlocks = Label(statWindow, bg = "white", text = blk)
    playerBlocks.config(height = 1, width = 4)
    playerBlocks.place(x=482,y=y)
    playerTurnovers = Label(statWindow, bg = "white", text = to)
    playerTurnovers.config(height = 1, width = 4)
    playerTurnovers.place(x=518,y=y)

代码非常重复,但在这个阶段对我来说不是问题。虽然我也欢迎提高效率的方法。 然后,此函数使用前一个生成许多标签:

def sortByPoints():
    foundPlayers = []
    with open ('PlayerList.csv') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            foundPlayers.append((int(row['Average PTS']), int(row['PlayerCode'])))
    sortedPlayers = sorted(foundPlayers, reverse=True)
    print(sortedPlayers)
    for i in range(len(sortedPlayers)):
        with open ('PlayerList.csv') as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                if row['PlayerCode'] == str(sortedPlayers[i][1]):
                    print(sortedPlayers[i][1])
                    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 + (i * 25)))

然后我如何隐藏所有生成的标签。或者我不可能采用我所采用的方法? 我正在使用python 3.4

1 个答案:

答案 0 :(得分:0)

我认为这可以为您提供帮助:

import matplotlib.pyplot as plt
x = [1,2,3]
y = [1,4,9]
plt.scatter(x,y)
ax = plt.gca()
ax.xaxis.set_minor_locator([1.1, 1.9, 2.5])