Skip to content

Instantly share code, notes, and snippets.

@Xnuvers007
Created May 24, 2025 19:19
Show Gist options
  • Save Xnuvers007/d4b3d5b17be9dbc3964f36616e89bfd0 to your computer and use it in GitHub Desktop.
Save Xnuvers007/d4b3d5b17be9dbc3964f36616e89bfd0 to your computer and use it in GitHub Desktop.
To Do List
import tkinter as tk
from tkinter import messagebox
username_entry = None
password_entry = None
login_button = None
login_status_text = ""
widgets = {}
def login():
global login_status_text
username = username_entry.get()
password = password_entry.get()
valid_users = {"user1": "pass1", "user2": "pass2"}
for user, passw in valid_users.items():
if username == user and password == passw:
login_status_text = "Login successful!"
widgets["result_label"].config(text=login_status_text)
show_todo_list(username_entry.master.master)
return
login_status_text = "Invalid credentials."
widgets["result_label"].config(text=login_status_text)
def show_todo_list(root):
for widget in root.winfo_children():
widget.destroy()
tk.Label(root, text="My Tasks").pack(pady=10)
input_frame = tk.Frame(root)
input_frame.pack(pady=5)
task_entry = tk.Entry(input_frame, width=30)
task_entry.pack(side="left", padx=5)
def add_task():
task = task_entry.get()
if task:
widgets["tasks_listbox"].insert(tk.END, task)
task_entry.delete(0, tk.END)
def delete_task():
selected_task_index = widgets["tasks_listbox"].curselection()
if selected_task_index:
widgets["tasks_listbox"].delete(selected_task_index)
else:
messagebox.showinfo("Info", "Please select a task to delete")
task_entry.bind('<Return>', lambda event: add_task())
tk.Button(input_frame, text="Add Task", command=add_task).pack(side="left")
tasks_listbox = tk.Listbox(root, width=50, height=10)
tasks_listbox.pack(pady=5)
widgets["tasks_listbox"] = tasks_listbox
button_frame = tk.Frame(root)
button_frame.pack(pady=5)
tk.Button(button_frame, text="Delete Task", command=delete_task).pack(side="left", padx=5)
tk.Button(button_frame, text="Logout", command=lambda: show_login_screen(root)).pack(side="left")
def show_login_screen(root):
global username_entry, password_entry, login_button
for widget in root.winfo_children():
widget.destroy()
tk.Label(root, text="Welcome Back").pack(pady=10)
form_frame = tk.Frame(root)
form_frame.pack(pady=10)
tk.Label(form_frame, text="Username").pack(anchor="w")
username_entry = tk.Entry(form_frame)
username_entry.pack(fill="x", pady=5)
tk.Label(form_frame, text="Password").pack(anchor="w")
password_entry = tk.Entry(form_frame, show="*")
password_entry.pack(fill="x", pady=5)
login_button = tk.Button(form_frame, text="LOGIN", command=login)
login_button.pack(pady=10)
result_label = tk.Label(form_frame, text="")
result_label.pack()
widgets["result_label"] = result_label
root.bind('<Return>', lambda event: login())
if __name__ == "__main__":
root = tk.Tk()
root.title("Task Manager")
root.geometry("400x500")
show_login_screen(root)
root.mainloop()
import unittest
import tkinter as tk
import main
class WhiteboxTests(unittest.TestCase):
def setUp(self):
self.root = tk.Tk()
main.show_login_screen(self.root)
def tearDown(self):
self.root.destroy()
def simulate_login(self, username, password):
main.username_entry.delete(0, tk.END)
main.username_entry.insert(0, username)
main.password_entry.delete(0, tk.END)
main.password_entry.insert(0, password)
main.login_button.invoke()
def check_login(self, username, password, expected_status, scenario_desc):
main.username_entry.delete(0, tk.END)
main.password_entry.delete(0, tk.END)
main.username_entry.insert(0, username)
main.password_entry.insert(0, password)
main.login()
actual_status = main.login_status_text
print(f"\nSCENARIO: {scenario_desc}")
print(f" Username: {username}")
print(f" Password: {password}")
print(f" Expected: {expected_status}")
print(f" Actual: {actual_status}")
self.assertEqual(actual_status, expected_status)
def test_login_success(self):
self.check_login("user1", "pass1", "Login successful!", "Username dan password benar")
def test_login_wrong_password(self):
self.check_login("user1", "wrongpass", "Invalid credentials.", "Username benar, password salah")
def test_login_wrong_username(self):
self.check_login("wronguser", "pass1", "Invalid credentials.", "Username salah, password benar")
def test_login_both_wrong(self):
self.check_login("wrong", "wrong", "Invalid credentials.", "Username dan password salah")
def test_login_empty_fields(self):
self.check_login("", "", "Invalid credentials.", "Username dan password kosong")
def test_add_task(self):
self.simulate_login("user1", "pass1")
task_entry = None
add_button = None
for widget in self.root.winfo_children():
if isinstance(widget, tk.Frame):
for child in widget.winfo_children():
if isinstance(child, tk.Entry):
task_entry = child
if isinstance(child, tk.Button) and child.cget("text") == "Add Task":
add_button = child
self.assertIsNotNone(task_entry, "Task entry tidak ditemukan")
self.assertIsNotNone(add_button, "Add button tidak ditemukan")
test_task = "Kerjakan tugas unittest"
task_entry.insert(0, test_task)
add_button.invoke()
tasks_listbox = main.widgets.get("tasks_listbox")
task_in_list = tasks_listbox.get(0)
print(f"\nSCENARIO: Menambahkan task ke list")
print(f" Task yang diinput: {test_task}")
print(f" Task pertama di list: {task_in_list}")
self.assertEqual(task_in_list, test_task, "Task tidak ditambahkan dengan benar")
def test_delete_task(self):
self.simulate_login("user1", "pass1")
task_entry = None
add_button = None
delete_button = None
for widget in self.root.winfo_children():
if isinstance(widget, tk.Frame):
for child in widget.winfo_children():
if isinstance(child, tk.Entry):
task_entry = child
elif isinstance(child, tk.Button):
if child.cget("text") == "Add Task":
add_button = child
elif child.cget("text") == "Delete Task":
delete_button = child
self.assertTrue(task_entry and add_button and delete_button, "UI komponen tidak lengkap")
test_task = "Task untuk dihapus"
task_entry.insert(0, test_task)
add_button.invoke()
listbox = main.widgets.get("tasks_listbox")
listbox.selection_set(0)
delete_button.invoke()
print(f"\nSCENARIO: Menghapus task dari list")
print(f" Task yang dihapus: {test_task}")
print(f" Jumlah task setelah dihapus: {listbox.size()}")
self.assertEqual(listbox.size(), 0, "Task tidak terhapus dari list")
def test_logout_functionality(self):
self.simulate_login("user1", "pass1")
logout_button = None
for widget in self.root.winfo_children():
if isinstance(widget, tk.Frame):
for child in widget.winfo_children():
if isinstance(child, tk.Button) and child.cget("text") == "Logout":
logout_button = child
self.assertIsNotNone(logout_button, "Logout button tidak ditemukan")
logout_button.invoke()
found_login_label = False
for widget in self.root.winfo_children():
if isinstance(widget, tk.Label) and widget.cget("text") == "Welcome Back":
found_login_label = True
break
print(f"\nSCENARIO: Logout kembali ke layar login")
print(f" Ditemukan label 'Welcome Back': {found_login_label}")
self.assertTrue(found_login_label, "Tidak kembali ke layar login setelah logout")
if __name__ == "__main__":
unittest.main()
@Xnuvers007
Copy link
Author

UniteTestingBlackboxAndWhiteboxTestingToDoListApps

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