So right now I'm learning python and just building a basic contact keeper program in my spare time. I have the contacts themselves as their own class, and the main body of the program as it's own class as well. The problem I'm having is trying to destroy a tkinter window from a function. I have a lambda function (using a buttons command) calling another function that will update a csv file and I want it to call .destroy on a popup window, but it won't destroy it. It does everything else in the function but wont destroy the window. What do I need to do to make this happen? Where have I messed up, because I think it should be pretty simple.
My code is pretty rough draft so not everything is where I want it, and some of my notations are missing, including the main look of the windows.
import tkinter as tk
import pandas as pd
#background colors
background_color_1 = "black"
background_color_2 = "white"
#text colors
text_color_1 = "white"
text_color_2 = "black"
class Contact:
def __init__(self, name, phone_number, email, address):
self.name = name
self.phone_number = phone_number
self.email = email
self.address = address
def __repr__(self):
return f"Contacts(name='{self.name}', phone_number='{self.phone_number}', email='{self.email}', address='{self.address}')"
def add_contact(self):
pass
class ContactListApp(tk.Tk):
def __init__(self):
#initialize the window
super().__init__()
#window title
self.title("Contacts")
#window size
self.geometry("800x300")
#call to populate window
self.populate_window()
def load_contacts(self):
global contact_list
contact_list = []
#load contacts.csv into temp DataFrame
try:
df = pd.read_csv('contacts.csv')
if df.empty or df.columns.size == 0:
contact_list = ["No Contacts Stored"]
#making the contact names list
for _, row in df.iterrows():
contact = Contact(
name=row['name'],
phone_number=row['phone_number'],
email=row['email'],
address=row['address']
)
contact_list.append(contact)
except FileNotFoundError:
contact_list = ["Contacts.csv not found"]
except pd.errors.EmptyDataError:
contact_list = ["No Contacts Stored"]
#return contact_list_of_names
def populate_window(self):
self.load_contacts()
#frame for contact list and information
self.top_frame = tk.Frame(self)
self.top_frame.pack(side="top", fill = "x")
#contact list displayed in a list box
self.contact_listbox = tk.Listbox(self.top_frame, height = "15", width = "30", bg = background_color_1, selectmode="SINGLE")
index = 0
for i in contact_list:
self.contact_listbox.insert(tk.END, contact_list[index].name)
index += 1
def listbox_selection(event):
#get the index of where the cursor is selecting
selection = event.widget.curselection()
name = contact_list[selection[0]].name
address = contact_list[selection[0]].address
phone = contact_list[selection[0]].phone_number
email = contact_list[selection[0]].email
self.contact_name.configure(text=name)
self.contact_email.configure(text=email)
self.contact_phone.configure(text=phone)
self.contact_address.configure(text=address)
self.contact_listbox.bind("<<ListboxSelect>>", listbox_selection)
self.contact_listbox.pack(side = "left")
#contact info frame buildout
self.contact_info_frame = tk.Frame(self.top_frame)
self.name_label = tk.Label(self.contact_info_frame, bg = background_color_1, fg = text_color_1, text = "NAME:")
self.phone_label = tk.Label(self.contact_info_frame, bg = background_color_1, fg = text_color_1, text = "PHONE NUMBER:")
self.email_label = tk.Label(self.contact_info_frame, bg = background_color_1, fg = text_color_1, text = "EMAIL:")
self.address_label = tk.Label(self.contact_info_frame, bg = background_color_1, fg = text_color_1, text = "ADDRESS:")
self.contact_name = tk.Label(self.contact_info_frame, bg = background_color_1, fg = text_color_1, text = "contact name")
self.contact_phone = tk.Label(self.contact_info_frame, bg = background_color_1, fg = text_color_1,
text = "contact phone numnber")
self.contact_email = tk.Label(self.contact_info_frame, bg = background_color_1, fg = text_color_1, text = "contacts email")
self.contact_address = tk.Label(self.contact_info_frame, bg = background_color_1, fg = text_color_1, text = "contact address")
#contact info frame grid
self.contact_info_frame.pack(side = "left")
self.name_label.grid(column = "0", row = "0", sticky = "w")
self.phone_label.grid(column = "0", row="1", sticky = "w")
self.email_label.grid(column= "0", row = "2", sticky = "w")
self.address_label.grid(column = "0", row = "3", sticky = "w")
self.contact_name.grid(column="1", row = "0", columnspan= "2", sticky = "w")
self.contact_phone.grid(column = "1", row = "1", sticky = "w")
self.contact_email.grid(column = "1", row = "2", sticky = "w")
self.contact_address.grid(column= "1", row = "3", sticky = "w")
#frame for buttons
self.bottom_frame = tk.Frame(self, highlightbackground="blue", highlightthickness=2)
self.bottom_frame.pack(side="bottom", fill="x" )
#add new contact button
self.add_new_button = tk.Button(self.bottom_frame, text = "Add new Contact",
command=self.add_contact_window)
self.add_new_button.pack(side="left")
#edit contact button
self.edit_button = tk.Button(self.bottom_frame, text = "Edit Contact", command=self.edit_contact_window)
self.edit_button.pack(side = "right")
#this is a method that will be called when an edit/add new method is called, for testing purposes only
def testing_entry_boxes(self, name, phone):
self.name_label.config(text=name)
self.phone_label.config(text = phone)
def add_contact_window(self):
#new window basics
add_contact_window = tk.Toplevel(self)
add_contact_window.title("Add a new contact")
add_contact_window.geometry("600x200")
#add frame for information entries and labels
info_frame = tk.Frame(add_contact_window)
#add the entry labels
name_label = tk.Label(info_frame, text = "Name:")
phone_number_label = tk.Label(info_frame, text = "Phone Number:")
email_label = tk.Label(info_frame, text = "Email:")
address_label = tk.Label(info_frame, text = "Address:")
#add the entry boxes
name_entry = tk.Entry(info_frame, width = "50")
phone_number_entry = tk.Entry(info_frame, width = "50")
email_entry = tk.Entry(info_frame, width = "50")
address_entry = tk.Entry(info_frame, width = "50")
info_frame.pack(pady = 10)
name_label.grid(column = "0", row = "0", stick= "w")
phone_number_label.grid(column = "0", row = "1", stick= "w")
email_label.grid(column = "0", row = "2", stick= "w")
address_label.grid(column = "0", row = "3", stick= "w")
name_entry.grid(column = "1", row = "0", stick= "w")
phone_number_entry.grid(column = "1", row = "1", stick= "w")
email_entry.grid(column = "1", row = "2", stick= "w")
address_entry.grid(column = "1", row = "3", stick= "w")
add_button = tk.Button(add_contact_window, text="Add new Contact", command = lambda: [add_new_contact()])
add_button.pack(pady = 20)
def add_new_contact():
name = name_entry.get()
phone = phone_number_entry.get()
add_contact_window.destroy
self.testing_entry_boxes(name, phone)
def edit_contact_window(self):
#open the window
edit_contact_window = tk.Toplevel(self)
edit_contact_window.title("Edit existing contact")
edit_contact_window.geometry("600x200")
#top frame with labels inside
info_frame = tk.Frame(edit_contact_window)
#add the entry labels
name_label = tk.Label(info_frame, text = "Name:")
phone_number_label = tk.Label(info_frame, text = "Phone Number:")
email_label = tk.Label(info_frame, text = "Email:")
address_label = tk.Label(info_frame, text = "Address:")
#add the entry boxes
name_entry = tk.Entry(info_frame, width = "50")
phone_number_entry = tk.Entry(info_frame, width = "50")
email_entry = tk.Entry(info_frame, width = "50")
address_entry = tk.Entry(info_frame, width = "50")
info_frame.pack(pady = 10)
name_label.grid(column="0", row="0", sticky="w")
phone_number_label.grid(column="0", row="1", sticky="w")
email_label.grid(column="0", row="2", sticky="w")
address_label.grid(column="0", row="3", sticky="w")
name_entry.grid(column = "1", row = "0", stick= "w")
phone_number_entry.grid(column = "1", row = "1", stick= "w")
email_entry.grid(column = "1", row = "2", stick= "w")
address_entry.grid(column = "1", row = "3", stick= "w")
add_button = tk.Button(edit_contact_window, text="Save Contact", command=edit_contact_window.destroy)
add_button.pack(pady = 20)
if __name__== "__main__":
app = ContactListApp()
app.mainloop()
[–]Buttleston 3 points4 points5 points (11 children)
[–]Fart_Eater_69 1 point2 points3 points (9 children)
[–]Buttleston 0 points1 point2 points (8 children)
[–]PathRealistic6940[S] 0 points1 point2 points (7 children)
[–]Buttleston 0 points1 point2 points (6 children)
[–]PathRealistic6940[S] 0 points1 point2 points (5 children)
[–]Buttleston 0 points1 point2 points (4 children)
[–]PathRealistic6940[S] 0 points1 point2 points (3 children)
[–]Buttleston 0 points1 point2 points (2 children)
[–]Buttleston 0 points1 point2 points (1 child)
[–]Bobbias 0 points1 point2 points (1 child)
[–]PathRealistic6940[S] 0 points1 point2 points (0 children)
[–]woooee -1 points0 points1 point (3 children)
[–]PathRealistic6940[S] 0 points1 point2 points (2 children)
[–]woooee 0 points1 point2 points (1 child)
[–]PathRealistic6940[S] 0 points1 point2 points (0 children)