#๐Ÿ”’ Cant import ttk

8 messages ยท Page 1 of 1 (latest)

terse knotBOT
#

@sacred tusk

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sacred tusk
#

import tkinter as tk
import tkinter.ttk as ttk
import json
import os

FILE_PATH = "tasks.json"
BG_COLOR = "#f0f0f0"
PRIORITY_COLOR = "#800000"
DONE_COLOR = "#008000"
NORMAL_COLOR = "#000000"

tasks = []

def load_tasks():
global tasks
if os.path.exists(FILE_PATH):
try:
with open(FILE_PATH, "r") as f:
tasks = json.load(f)
except Exception as e:
print("Error loading tasks:", e)

def save_tasks():
try:
with open(FILE_PATH, "w") as f:
json.dump(tasks, f)
except Exception as e:
print("Error saving tasks:", e)

terse knotBOT
#

Hey @sacred tusk!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
sacred tusk
#

def add_task():
task_text = task_entry.get()
if not task_text.strip():
error_label.config(text="Error: Task cannot be empty!")
return
error_label.config(text="")
tasks.append({"task": task_text, "completed": False, "priority": False})
task_entry.delete(0, tk.END)
refresh_tasks()
save_tasks()

def refresh_tasks():
for widget in task_frame.winfo_children():
widget.destroy()
for i, task in enumerate(tasks):
color = PRIORITY_COLOR if task["priority"] else DONE_COLOR if task["completed"] else NORMAL_COLOR
lbl = tk.Label(task_frame, text=task["task"], fg=color, bg=BG_COLOR)
lbl.grid(row=i, column=0, sticky="w")
btn_done = ttk.Button(task_frame, text="Done", command=lambda i=i: toggle_complete(i))
btn_done.grid(row=i, column=1)
btn_priority = ttk.Button(task_frame, text="Priority", command=lambda i=i: toggle_priority(i))
btn_priority.grid(row=i, column=2)

def toggle_complete(index):
tasks[index]["completed"] = not tasks[index]["completed"]
refresh_tasks()
save_tasks()

def toggle_priority(index):
tasks[index]["priority"] = not tasks[index]["priority"]
refresh_tasks()
save_tasks()

root = tk.Tk()
root.title("Simple To-Do App")
root.configure(bg=BG_COLOR)

input_frame = tk.Frame(root, bg=BG_COLOR)
input_frame.pack(pady=10)

task_entry = tk.Entry(input_frame, width=30)
task_entry.grid(row=0, column=0, padx=5)

add_btn = ttk.Button(input_frame, text="Add Task", command=add_task)
add_btn.grid(row=0, column=1, padx=5)

error_label = tk.Label(input_frame, text="", fg="red", bg=BG_COLOR)
error_label.grid(row=1, column=0, columnspan=2)

task_frame = tk.Frame(root, bg=BG_COLOR)
task_frame.pack(pady=10)

load_tasks()
refresh_tasks()

root.mainloop()

#

how can i import ttk?

terse knotBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.