[我想在单击按钮(创建)时一个在另一个标签的下面创建一个标签,然后在另一个实例中单击(删除)的一个实例中删除所有标签。
现在,我正在考虑为标签创建多个名称-只需单击一次即可创建每个名称。因此,如果我单击n次按钮,我将获得n个名称:myLabel1,myLabel2,...,myLabeln
然后,当我单击Delete(删除)按钮时,我会看到类似-myLabel1.destroy(),myLabel2.destroy(),。。。myLabeln.destroy()
我正在努力以-myLabel1 = tk.Label等形式将这些名称分配给标签。我在考虑MATLAB中的eval函数。 Python是否有类似的东西?
是否有更好的方式来做我想做的事?
如果使用.pack()方法,我的标签将一个接一个。但是它们都只分配给myLabel(因为我使用myLabel = tk.Label)。当我使用myLabel.destroy()时,仅删除了label的最后一个实例。
带有.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()
但是由于这只会删除最后创建的标签,因此我尝试了开头提到的方法-
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
您可以使用此脚本:每个标签的文本都存储在var标签中。
您可以在新行中重新创建一个标签,而不是为每个标签分配一个变量,但是可以通过将其添加到数组或包含所有标签的类似标签中,因此可以随时删除每个标签。
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()
填写条目并单击按钮。
您可以使用网格来组织窗口。在这种情况下,请使用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()