Reputation: 1
using tkinter with python, i am making a menu that shows what you put in. then can be removed by pressing a button, my question is how do i delete the Labels in the right order. the order should be from top to bottom. the "orders" come in by date, the oldest being at the top and the newest being put at the bottom of the list. By using destroy(), i can only manage to delete the very last label(the newest). i need help to figure out how to delete a specific label.
#imports-----------------------
from tkinter import *
#from PIL import ImageTk,image
from tkinter import messagebox
import time
#imports----------------------
#start-----------
x=1
height=0
l="0"
root = Tk()
root.geometry("300x200")
#start-----------
def myClick():
global x #number of the label
global height
global l #name of the label
global vartoprint
global y #to retain the old value of x
if x == 1:
global statehold
statehold=1
l=str(statehold)
vartoprint="order one"
y=1
if x > y: #when x is different than the old value of y, we assume the state has gone up
#and therefor a new entry has come, time to print this new value
height=height+1
statehold= statehold + 1
l=str(statehold)
vartoprint="order 'xxx'" #in this case, the new value is always "order xxx" for testing
l = Label(root, text=vartoprint)
l.grid(row=height, column=L)
x=x+1
mybutton = Button(root, text="next", padx=10, pady=8, command=myClick, fg="black", bg="white")
mybutton.grid(row=1, column=5)
def mydelete():
global statehold
global x
global l
statehold= statehold - 1 #tells the system that 1 label has been removed
l.destroy() #destroys the label
x=x-1 #tells the system that 1 label has been removed
DeleteButton = Button(root, text="next and delete", command=mydelete)
DeleteButton.grid(row=1, column=6)
#end-------------
time.sleep(3)
root.mainloop()
#end-------------
Upvotes: 0
Views: 1233
Reputation: 1
thanks using a list and deleting the first list item, i am able to do my task.
I use
my_list.append(l)
to insert the variable at the end of the list. and like you said, i use
(my_list.pop(0)).destroy()
do remove the first item in the list.
thanks, you were great!
Upvotes: 0