簡體   English   中英

如何根據條件刪除python tkinter標簽?

[英]How do I delete a python tkinter label based on condition?

我將使用偽代碼,因為我的真實代碼更加混亂。 基本上我有一個看起來像這樣的代碼:

if i>0 and B!=1:
  #display label #1 using .grid
elif: i<0 and B!=1 :
  #display label #2 but on the same place as label#1

因此,我最終將兩個標簽都放在另一個標簽上。 我如何獲得其中一個標簽

在顯示其他標簽之前將其刪除?

編輯:請注意,我的程序也將在無限循環中運行,因此兩個標簽將在整個程序運行期間顯示。

使用widget.destroy()方法銷毀label1

if i>0 and B!=1:
    label1 = Tk.Label(root,text='Label1')
    label1.grid(row = 2,column=5)

elif i<0 and B!=1:
    if 'label1' in globals():
        label1.destroy()
    label2 = Tk.Label(root,text='Label2')
    label2.grid(row = 2,column=5)

編輯:

注意您可以使用:

if 'label1' in globals():
    label1.destroy()

為避免local variable "label1" refrence before assignment錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM