Help me address the Python list.
-
I have a code in which the Tkinter's window of the cycle turns the name of all the files in the folder in the frime. But I also wanted to add buttons to remove and open these files. But I don't know how to turn to a specific element, because every single set of frimes is a separate iteration of the For cycle. Cycle code:
self.arrcount =0 self.blocksCounter = 0 self.Height = 0.05 for i in range(self.file_count): self.videoBlock = tk.Frame(self.mainFrame1, bg=self.darkColour) self.videoBlock.place(relx=0.01, rely=self.Height, relwidth=0.95, relheight=0.2) self.vTitle = tk.Label(self.videoBlock, bg = self.darkColour, fg = self.whitecolour, text = self.count[2][self.arrcount], anchor = tk.W, font = ('Helvetica', 14) ) self.vTitle.place(relx = 0.3, rely = 0.1, relwidth = 0.7, relheight = 0.2) self.openBtn = tk.Button(self.videoBlock, fg = self.darkColour, bg = self.cactive,bd = 0, text = "Open" ,command=self.openfile) self.openBtn.place(relx = 0.3, rely = 0.4, relwidth = 0.1, relheight = 0.25) self.deleteBtn = tk.Button(self.videoBlock, bg= 'red', fg = self.whitecolour, text = "X", command = self.removeFile) self.deleteBtn.place(relx = 0.94, rely = 0.65, relwidth = 0.05, relheight = 0.3) self.Height += 0.22 self.blocksCounter += 1 self.arrcount +=1
self.file_count - Removable, which stores the number of files in the folder self.count[][ is a list called files. The list itself is:
('C:/Users/HP/Desktop/Empty Folder', [], ['Duke Dumont - Ocean Drive (Official Music Video).mp4', 'Joji - SLOW DANCING IN THE DARK.mp4', 'Lana Del Rey - Mytime Sadness.
With his frais and his name, he's pulling out good as all the buttons, but how is it possible to tie a function? If you try such a function:
def removeFile(self): self.mes = tk.messagebox.askquestion(title="Warning",icon='warning',message='Do you want to delete this fie?') if self.mes == 'yes': os.remove(self.count[2][self.arrcount]) print('File deleted') else: print('Deleting Canceled')
The code makes a mistake:
IndexError: list index out of range
-
I don't see the problem of transmitting the name of the file as a parameter for opening and disposal functions.
def removeFile(self, filename): self.mes = tk.messagebox.askquestion(title="Warning", icon='warning', message=f'Do you want to delete file "{filename}" ?') if self.mes == 'yes': os.remove(filename) print('File deleted') else: print('Deleting Canceled')
self.arrcount =0 self.blocksCounter = 0 self.Height = 0.05 for i in range(self.file_count): self.videoBlock = tk.Frame(self.mainFrame1, bg=self.darkColour) self.videoBlock.place(relx=0.01, rely=self.Height, relwidth=0.95, relheight=0.2) self.vTitle = tk.Label(self.videoBlock, bg = self.darkColour, fg = self.whitecolour, text = self.count[2][self.arrcount], anchor = tk.W, font = ('Helvetica', 14) ) self.vTitle.place(relx = 0.3, rely = 0.1, relwidth = 0.7, relheight = 0.2) self.openBtn = tk.Button(self.videoBlock, fg = self.darkColour, bg = self.cactive,bd = 0, text = "Open", command=lambda: self.openfile(self.count[2][self.arrcount])) self.openBtn.place(relx = 0.3, rely = 0.4, relwidth = 0.1, relheight = 0.25) self.deleteBtn = tk.Button(self.videoBlock, bg= 'red', fg = self.whitecolour, text = "X", command = lambda: self.removeFile(self.count[2][self.arrcount])) self.deleteBtn.place(relx = 0.94, rely = 0.65, relwidth = 0.05, relheight = 0.3) self.Height += 0.22 self.blocksCounter += 1 self.arrcount +=1
Or, if you want, you can only pass the index on the list.
self.arrcount
If the list itself is still kept.
(I don't really understand why to make class attributes local countersself.blocksCounter
andself.arrcount
and the difference between the variablei
)