Skip to content

Instantly share code, notes, and snippets.

@SoulFireMage
Created May 7, 2023 11:37
Show Gist options
  • Save SoulFireMage/62db93a39838eb7b2ce2889e520d0172 to your computer and use it in GitHub Desktop.
Save SoulFireMage/62db93a39838eb7b2ce2889e520d0172 to your computer and use it in GitHub Desktop.
Showing how to use simple OOP with TK to build a list of objects populated by a second form.
import tkinter as tk
# Define the UserExamStore class
class UserExamStore:
def __init__(self, id, name, date):
self.id = id
self.name = name
self.date = date
self.exams = []
self.languages = []
self.formats = []
# String representation of the UserExamStore instance
def __str__(self):
return f"ID: {self.id}\nName: {self.name}\nDate: {self.date}\nExams: {self.exams}\nLanguages: {self.languages}\nFormats: {self.formats}"
# Define the MainWindow class, inheriting from tk.Tk
class MainWindow(tk.Tk):
def __init__(self):
super().__init__()
self.title("Main Window")
self.geometry("450x400")
# Initialize an empty list to store UserExamStore instances
self.user_exam_stores = []
# Create and pack the button to open the second window
open_button = tk.Button(self, text="Open Second Window", command=self.open_second_window)
open_button.pack()
# Create and pack the listbox to display the list of UserExamStore instances
self.user_exam_stores_listbox = tk.Listbox(self)
self.user_exam_stores_listbox.pack()
# Bind the listbox's selection event to the on_listbox_select method
self.user_exam_stores_listbox.bind("<<ListboxSelect>>", self.on_listbox_select)
# Create and pack the text widget to display information about a selected UserExamStore instance
self.result_text = tk.Text(self, wrap=tk.WORD)
self.result_text.pack(expand=True, fill=tk.BOTH, padx=10, pady=10)
# Method to open the second window and wait for it to close
def open_second_window(self):
user_exam_store = UserExamStore(self.get_next_id(), "John Doe", "2023-05-04")
second_window = SecondWindow(self, user_exam_store)
self.wait_window(second_window)
# Method to add a new UserExamStore instance to the list and update the listbox
def update_listbox(self, user_exam_store):
self.user_exam_stores.append(user_exam_store)
self.user_exam_stores_listbox.insert(tk.END, f"{user_exam_store.id}: {user_exam_store.name}")
# Method to handle the listbox's selection event
def on_listbox_select(self, event):
selection = self.user_exam_stores_listbox.curselection()
if selection:
index = selection[0]
user_exam_store = self.user_exam_stores[index]
self.update_text_widget(user_exam_store)
# Method to update the text widget with information about a UserExamStore instance
def update_text_widget(self, user_exam_store):
self.result_text.delete(1.0, tk.END)
self.result_text.insert(tk.END, str(user_exam_store))
# Method to calculate the next unique ID for a new UserExamStore instance
def get_next_id(self):
if not self.user_exam_stores:
return 1
else:
highest_id = max(user_exam_store.id for user_exam_store in self.user_exam_stores)
return highest_id + 1
# Define the SecondWindow class, inheriting from tk.Toplevel
class SecondWindow(tk.Toplevel):
def __init__(self, parent, user_exam_store):
super().__init__(parent)
self.title("Second Window")
self.geometry("450x300")
# Store the reference to the UserExamStore instance passed to this window
self.user_exam_store = user_exam_store
# Define the data for the listboxes
exams = ["Exam A", "Exam B", "Exam C"]
languages = ["English", "French", "Spanish"]
formats = ["Compact", "Normal", "Extended"]
# Create and populate the listboxes for exams, languages, and formats
self.exams_listbox = tk.Listbox(self, selectmode=tk.MULTIPLE, exportselection=False)
self.languages_listbox = tk.Listbox(self, selectmode=tk.MULTIPLE, exportselection=False)
self.formats_listbox = tk.Listbox(self, selectmode=tk.MULTIPLE, exportselection=False)
for exam in exams:
self.exams_listbox.insert(tk.END, exam)
for language in languages:
self.languages_listbox.insert(tk.END, language)
for format in formats:
self.formats_listbox.insert(tk.END, format)
# Create the submit button and set its command to the on_submit method
submit_button = tk.Button(self, text="Submit", command=self.on_submit)
# Place the listboxes and submit button using grid layout
self.exams_listbox.grid(row=0, column=0, padx=10, pady=10)
self.languages_listbox.grid(row=0, column=1, padx=10, pady=10)
self.formats_listbox.grid(row=0, column=2, padx=10, pady=10)
submit_button.grid(row=1, column=1, pady=10)
# Method to handle the submit button click event
def on_submit(self):
# Retrieve the selected options from each listbox and store them in the UserExamStore instance
self.user_exam_store.exams = [self.exams_listbox.get(i) for i in self.exams_listbox.curselection()]
self.user_exam_store.languages = [self.languages_listbox.get(i) for i in self.languages_listbox.curselection()]
self.user_exam_store.formats = [self.formats_listbox.get(i) for i in self.formats_listbox.curselection()]
# Update the listbox in the main window with the new UserExamStore instance
self.master.update_listbox(self.user_exam_store)
# Close the second window
self.destroy()
main_window = MainWindow()
main_window.mainloop()
@SoulFireMage
Copy link
Author

There is now a second gist that will demonstrate how to take this further, using the built in sqlite in your python installation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment