簡體   English   中英

Python tkinter - 創建標簽然后刪除它們

[英]Python tkinter - Creating Labels and then deleting them

我想在單擊按鈕(創建)時在另一個下方創建多個標簽,然后在單擊另一個按鈕(刪除)時刪除一個實例中的所有標簽。

現在,我正在考慮為標簽創建多個名稱 - 每個名稱都是通過單擊創建的。 因此,如果我單擊按鈕 n 次,我將獲得 n 個名稱:myLabel1、myLabel2、...、myLabeln

然后,當我單擊“刪除”按鈕時,我會看到類似的東西 - myLabel1.destroy(), myLabel2.destroy(), 。 . . myLabeln.destroy()

我正在努力以 -myLabel1 = tk.Label 等的形式將這些名稱分配給標簽。我正在考慮 Z8F6823ABD383A341BBF0D78EF38 中的 eval function。 Python 有類似的東西嗎?

有沒有更好的方法來做我想做的事?

如果我使用 .pack() 方法,我會得到一個低於另一個的標簽。 但它們都只分配給 myLabel(因為我使用 myLabel = tk.Label)。 而當我使用 myLabel.destroy() 時,只刪除了 label 的最后一個實例。

代碼 with.pack() 和 myLabel 的單一用法,其中標簽根據需要形成,但只刪除最后一個:

import tkinter as tk
from tkinter import ttk
import tkinter.font as tkFont

root = tk.Tk()

def click():
    global myLabel
    myLabel = tk.Label(root, text = e.get())
    myLabel.pack(pady=10)
    e.delete(0,'end')

def delete():
    myLabel.destroy()

e = tk.Entry(root, width = 50)
e.pack(padx = 10, pady = 10)

CreateButton = tk.Button(root, text = 'Enter name', command = click)
CreateButton.pack(pady=10)

DeleteButton = tk.Button(root, text = 'Delete', command = delete)
DeleteButton.pack(pady=40)

root.mainloop()

但是由於這只會刪除最后一個創建的 label,所以我嘗試了我在開始時提到的方法 -

import tkinter as tk
from tkinter import ttk
import tkinter.font as tkFont

ii = 0

root = tk.Tk()

def click():
    global labelname, ii
    ii += 1
    labelname = 'myLabel' + str(ii)
    print(labelname)
    labelname = tk.Label(root, text = e.get()) # I wanted 'labelname' here to be 'myLabel1'
    e.delete(0,'end')

def delete():
#     for ii in range(1,max(ii)):
    labelname.config(text = '') # I wanted 'labelname' here to be 'myLabeln' in a for loop going from 1 to n

e = tk.Entry(root, width = 50)
e.pack(padx = 10, pady = 10)

CreateButton = tk.Button(root, text = 'Enter name', command = click)
CreateButton.pack(pady=10)

DeleteButton = tk.Button(root, text = 'Delete', command = delete)
DeleteButton.pack(pady=40)

root.mainloop()

也許我以一種非常復雜的方式來處理它,並且有一種更簡單的方法而不使用 For 循環等。

請指教。

謝謝

R

您可以使用此腳本:每個 label 的文本都存儲在 var 標簽中。

您可以在新行中重新創建一個 label,而不是為每個 label 分配一個變量,但是通過將其添加到一個數組或類似的東西可以包含所有標簽,因此您可以隨時刪除它們中的每一個。

from tkinter import *

win = Tk()                         # create the window

labels = []                        # no labels

def add_label():
    global labels, text
    labels.append(Label(win, text=text.get())) # append a label object to the list
    labels[-1].pack()              # draw the last label

def delete_all_labels():
    global labels
    for label in labels:           # delete all the labels
        label.pack_forget()
    labels = []

text = Entry(win)                  # add the entry
text.pack()

Button(win, text="Create new label", command=add_label).pack()
Button(win, text="Delete all the labels", command=delete_all_labels).pack()

win.mainloop()

填寫條目並單擊按鈕。

您可以使用網格來組織 window。 在這種情況下,使用 grid_forget 而不是 pack_forget 來刪除標簽。

from tkinter import *

win = Tk()                         # create the window

labels = []                        # no labels

                                   # index for method grid()
rowIndex = 2                       # Set to 2 because of the
                                   # buttons and the entry.
                                   # this variable is used to increment
                                   # the y position of the labels so that
                                   # they are drawn one after the other,
                                   # and not on top of each other.

def add_label():
    global labels, text, rowIndex
    labels.append(Label(win, text=text.get())) # append a label object to the list
    rowIndex += 1                  # increment the y position

                                   # draw the added label
    labels[-1]\
            .grid(columnspan=2,    # for align in the middle.
                  row=rowIndex,    # y position specific to this label
                  pady=5)

def delete_all_labels():
    global labels
    for label in labels:           # delete all the labels
        label.grid_forget()        # one by one

    rowIndex = 2                   # reset the vars
    labels = []                    #

label=Label(win, text="Type the text :")
label.grid(column=0, row=0, padx=5)

text = Entry(win)                  # add the entry
text.grid(column=1, row=0, padx=5)
text.focus()                       # focus in entry

Button(win, text="Create new label", command=add_label)\
            .grid(column=0, row=1, padx=5)
Button(win, text="Delete all the labels", command=delete_all_labels)\
            .grid(column=1, row=1, padx=5)

win.mainloop()

暫無
暫無

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

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