PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 7, 2022

[FIXED] How can I remove an object from a list using its method?

 July 07, 2022     class, python, tkinter     No comments   

Issue

Here's my unfinished program:

adding class objects with a button

I made a class for a Frame() with input fields and a button.

I then have a + button which creates new instances of this class by adding them to an empty list. Objects .pack() themselves upon initialization, so they appear in the window.

Edit: added working code you can try:

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

class JobItem:
    def __init__(self, parent):
        self.frame = tk.Frame(parent, bd=2, relief=tk.GROOVE)
        self.frame.pack(side=tk.TOP, fill=tk.X, padx=2, pady=2)

        self.nameLabel = ttk.Label(self.frame, text="Description:")
        self.nameLabel.grid(column=0, row=0, sticky=tk.W, padx=5, pady=5)

        self.nameEntry = ttk.Entry(self.frame)
        self.nameEntry.grid(column=1, row=0, sticky=tk.W, padx=5, pady=5)

        self.jobTypeLabel = ttk.Label(self.frame, text="Job type:")
        self.jobTypeLabel.grid(column=3, row=0, sticky=tk.W, padx=5, pady=5)

        self.selected_job = tk.StringVar()
        self.job_type_cb = ttk.Combobox(self.frame, textvariable=self.selected_job, state='readonly')
        self.job_type_cb['values'] = ['Still', 'Animation', 'Model production']
        self.job_type_cb.current(0)
        self.job_type_cb.grid(column=4, row=0, sticky=tk.W, padx=5, pady=5)

        self.x = ttk.Button(self.frame, text="X", command=self.delete_itself)
        self.x.grid(column=5, row=0, sticky=tk.E, padx=5, pady=5)

        # v v v This method is what I don't know how to do properly v v v
        def delete_itself():
            pass

job_items = list()

def add_jobItem():
    job_items.insert(len(job_items), JobItem(itemListContainer))


root=tk.Tk()
root.title('Estimate generator')
root.geometry('800x500')

headerFrame = tk.Frame(root, height=100)
headerFrame.pack(side=tk.TOP, fill=tk.X)

jobNameLabel = ttk.Label(headerFrame, text="Project name: ")
jobNameLabel.pack(side=tk.LEFT)
jobNameEntry = ttk.Entry(headerFrame)
jobNameEntry.pack(side=tk.LEFT, expand=True, fill=tk.X)

buttonFont = tkFont.Font(weight="bold")
plusButton = tk.Button(headerFrame, text='+', command=add_jobItem, font=buttonFont, fg='#656565', height=0, width=10)
plusButton.pack(side=tk.RIGHT, padx=(100,2), pady=2)

# Item List Frame
itemListContainer = tk.Frame(root)
itemListContainer.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

root.mainloop()

How can I make X button on a given instance remove not only the packed elements, but also the object from job_items list? How would we know what index does it occupy in the list? Or maybe I should take a different approach?


Solution

I would suggest to pass a function to JobItem which will be executed inside delete_itself:

class JobItem:
    def __init__(self, parent, on_delete=None):
        self.on_delete = on_delete
        ...

    def delete_itself(self):
        # destroy the frame
        self.frame.destroy()
        # call self.on_delete if it is not None
        if self.on_delete:
            # pass JobItem itself to self.on_delete
            self.on_delete(self)

job_items = list()

def del_jobItem(item):
    # remove the job item from job_items list
    job_items.remove(item)

def add_jobItem():
    # can use job_items.append(...) instead
    #job_items.insert(len(job_items), JobItem(itemListContainer, del_jobItem))
    job_items.append(JobItem(itemListContainer, del_jobItem))

...


Answered By - acw1668
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing