![](/img/trans.png)
[英]Removing Tkinter Objects (created in a function) in a separate function
[英]Removing labels created by a function in tkinter
我有一個生成標簽並使用.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
我認為這可以幫助您:
from tkinter import *
gui=Tk()
def label():
global l1,l2,l3,l4
l1 = Label(gui,text='hi')
l1.place(x=10,y=10)
l2 = Label(gui,text='hi2')
l2.place(x=30,y=10)
l3 = Label(gui,text='hi3')
l3.place(x=50,y=10)
l4 = Label(gui,text='hi4')
l4.place(x=70,y=10)
def remove_label():
l1.place_forget()
l2.place_forget()
l3.place_forget()
l4.place_forget()
print('All cleared!')
b = Button(gui,text='place label',command=label)
b.place(x=10,y=50)
h = Button(gui,text='remove label',command=remove_label)
h.place(x=10,y=100)
gui.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.