0

I can't change the colour of the frame area..

CODE::

import time
import threading
import sqlite3
import tkinter as tk
from tkinter import ttk, Text
import os

class PomodoroTimer:

    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry("2400x1400")
        #3024 × 1964
        self.root.title("Focus")
        self.root.tk_setPalette(background='#85BB65', foreground='#85BB65')

        self.tabs = ttk.Notebook(self.root)
        self.tabs.pack(fill="both", pady=10, expand=True)



        self.style = ttk.Style()
        self.style.configure("TFrame", background="#004B49")
        self.style.configure("TLabel", foreground="#bfe0f2")
        self.style.configure("TButton", background="#bfe0f2", foreground="#85BB65")
        self.style.configure("TNotebook.Tab", background="#1dcfb1", foreground="#85BB65")
        self.style.configure("TNotebook", background="#1dcfb1")
        self.style.configure("TNotebook.Tab", padding=[30, 5])

        self.tab1 = ttk.Frame(self.tabs, style="TFrame")
        self.tab2 = ttk.Frame(self.tabs, style="TFrame")
        self.tab3 = ttk.Frame(self.tabs, style="TFrame")

        self.pomodoro_timer_label = ttk.Label(self.tab1, text="25:00", font=("impact", 200), style="TLabel")
        self.pomodoro_short_break_label = ttk.Label(self.tab2, text="05:00", font=("impact", 200), style="TLabel")
        self.pomodoro_long_break_label = ttk.Label(self.tab3, text="15:00", font=("impact", 200), style="TLabel")

        self.pomodoro_timer_label.pack(pady=20)
        self.pomodoro_short_break_label.pack(pady=20)
        self.pomodoro_long_break_label.pack(pady=20)

        self.tabs.add(self.tab1, text="Pomodoro")
        self.tabs.add(self.tab2, text="Short Break")
        self.tabs.add(self.tab3, text="Long Break")

        self.grid_layout = ttk.Frame(self.root)
        self.grid_layout.pack(pady=10)

        self.start_button = ttk.Button(self.grid_layout, text="Start", command=self.start_timer_thread, style="TButton")
        self.skip_button = ttk.Button(self.grid_layout, text="Skip", command=self.skip_clock, style="TButton")
        self.reset_button = ttk.Button(self.grid_layout, text="Reset", command=self.reset_clock, style="TButton")
        self.open_note_button = ttk.Button(self.grid_layout, text="Open Note", command=self.open_note, style="TButton")
        self.open_calculator_button = ttk.Button(self.grid_layout, text="Open Calculator", command=self.op_calc, style="TButton")

        self.start_button.grid(row=0, column=0, padx=5)
        self.skip_button.grid(row=0, column=1, padx=5)
        self.reset_button.grid(row=0, column=2, padx=5)
        self.open_note_button.grid(row=0, column=3, padx=5)
        self.open_calculator_button.grid(row=0, column=4, padx=5)

        self.pomodoro_counter_label = ttk.Label(self.grid_layout, text="Pomodoros: 0", font=("impact", 60), style="TLabel")
        self.pomodoro_counter_label.grid(row=1, columnspan=3, column=0)

        self.pomodoros = 0
        self.skipped = False
        self.stopped = False
        self.current_timer = None

        self.note_window = None
        self.note_text = None
        self.note_file = "note.txt"

        if os.path.exists(self.note_file):
            with open(self.note_file, "r") as file:
                self.note_text = file.read()

        self.root.mainloop()

    def start_timer_thread(self):
        if self.current_timer is None or not self.current_timer.is_alive():
            self.current_timer = threading.Thread(target=self.start_timer)
            self.current_timer.start()

    def start_timer(self):
        self.stopped = False
        self.skipped = False
        timer_id = self.tabs.index(self.tabs.select()) + 1

        if timer_id == 1:
            full_seconds = 60 * 25
            label = self.pomodoro_timer_label
        elif timer_id == 2:
            full_seconds = 60 * 5
            label = self.pomodoro_short_break_label
        elif timer_id == 3:
            full_seconds = 60 * 15
            label = self.pomodoro_long_break_label
        else:
            print("Invalid timer id")
            return

        while full_seconds > 0 and not self.stopped:
            minutes, seconds = divmod(full_seconds, 60)
            label.config(text=f"{minutes:02d}:{seconds:02d}")
            self.root.update()
            time.sleep(1)
            full_seconds -= 1

        if not self.stopped or self.skipped:
            self.pomodoros += 1
            self.pomodoro_counter_label.config(text=f"Pomodoros: {self.pomodoros}")

            if self.pomodoros % 4 == 0:
                self.tabs.select(2)
            else:
                self.tabs.select(0)

            self.current_timer = None
            self.start_timer()

    def reset_clock(self):
        self.stopped = True
        self.skipped = False
        timer_id = self.tabs.index(self.tabs.select()) + 1
        if timer_id == 1:
            self.pomodoro_timer_label.config(text="25:00")
        elif timer_id == 2:
            self.pomodoro_short_break_label.config(text="05:00")
        elif timer_id == 3:
            self.pomodoro_long_break_label.config(text="15:00")

    def skip_clock(self):
        self.stopped = True
        self.skipped = True
        timer_id = self.tabs.index(self.tabs.select()) + 1
        if timer_id == 1:
            self.pomodoros += 1
            self.pomodoro_counter_label.config(text=f"Pomodoros: {self.pomodoros}")
        self.tabs.select(0)
        self.start_timer()

    def open_note(self):
        window = tk.Tk()
        window.title("NOTES")
        window.geometry("1000x700")

        note_entry = tk.Text(window)
        note_entry.pack()

        def save_note():
            note = note_entry.get("1.0", tk.END)
            conn = sqlite3.connect("notes.db")
            cursor = conn.cursor()
            cursor.execute("CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT)")
            cursor.execute("INSERT INTO notes (content) VALUES (?)", (note,))
            conn.commit()
            conn.close()

        save_button = tk.Button(window, text="Save Note", command=save_note,foreground="#85BB65",font="Impact")
        save_button.pack()

        def view_notes():
            conn = sqlite3.connect("notes.db")
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM notes")
            notes = cursor.fetchall()
            conn.close()

            view_window = tk.Toplevel(window)
            view_window.title("View Notes")
            view_text = tk.Text(view_window)
            for note in notes:
                view_text.insert(tk.END, note[1] + "\n")
            view_text.pack()

        view_button = tk.Button(window, text="View Notes", command=view_notes,foreground="#85BB65",font="Impact")
        view_button.pack()

    def op_calc(self):
        # pip install tkinter
        import tkinter as tk
        import tkinter.messagebox
        from tkinter.constants import SUNKEN

        window = tk.Tk()
        window.title('Calculator')
        frame = tk.Frame(master=window, bg="#85BB65", padx=10)
        frame.pack()
        entry = tk.Entry(master=frame, relief=SUNKEN, borderwidth=3, width=30)
        entry.grid(row=0, column=0, columnspan=3, ipady=2, pady=2)

        def myclick(number):
            entry.insert(tk.END, number)

        def equal():
            try:
                y = str(eval(entry.get()))
                entry.delete(0, tk.END)
                entry.insert(0, y)
            except:
                tkinter.messagebox.showinfo("Error", "Syntax Error")

        def clear():
            entry.delete(0, tk.END)

        button_1 = tk.Button(master=frame, text='1', padx=15,
                             pady=5, width=3, command=lambda: myclick(1))
        button_1.grid(row=1, column=0, pady=2)
        button_2 = tk.Button(master=frame, text='2', padx=15,
                             pady=5, width=3, command=lambda: myclick(2))
        button_2.grid(row=1, column=1, pady=2)
        button_3 = tk.Button(master=frame, text='3', padx=15,
                             pady=5, width=3, command=lambda: myclick(3))
        button_3.grid(row=1, column=2, pady=2)
        button_4 = tk.Button(master=frame, text='4', padx=15,
                             pady=5, width=3, command=lambda: myclick(4))
        button_4.grid(row=2, column=0, pady=2)
        button_5 = tk.Button(master=frame, text='5', padx=15,
                             pady=5, width=3, command=lambda: myclick(5))
        button_5.grid(row=2, column=1, pady=2)
        button_6 = tk.Button(master=frame, text='6', padx=15,
                             pady=5, width=3, command=lambda: myclick(6))
        button_6.grid(row=2, column=2, pady=2)
        button_7 = tk.Button(master=frame, text='7', padx=15,
                             pady=5, width=3, command=lambda: myclick(7))
        button_7.grid(row=3, column=0, pady=2)
        button_8 = tk.Button(master=frame, text='8', padx=15,
                             pady=5, width=3, command=lambda: myclick(8))
        button_8.grid(row=3, column=1, pady=2)
        button_9 = tk.Button(master=frame, text='9', padx=15,
                             pady=5, width=3, command=lambda: myclick(9))
        button_9.grid(row=3, column=2, pady=2)
        button_0 = tk.Button(master=frame, text='0', padx=15,
                             pady=5, width=3, command=lambda: myclick(0))
        button_0.grid(row=4, column=1, pady=2)

        button_add = tk.Button(master=frame, text="+", padx=15,
                               pady=5, width=3, command=lambda: myclick('+'))
        button_add.grid(row=5, column=0, pady=2)

        button_subtract = tk.Button(
            master=frame, text="-", padx=15, pady=5, width=3, command=lambda: myclick('-'))
        button_subtract.grid(row=5, column=1, pady=2)

        button_multiply = tk.Button(
            master=frame, text="*", padx=15, pady=5, width=3, command=lambda: myclick('*'))
        button_multiply.grid(row=5, column=2, pady=2)

        button_div = tk.Button(master=frame, text="/", padx=15,
                               pady=5, width=3, command=lambda: myclick('/'))
        button_div.grid(row=6, column=0, pady=2)

        button_clear = tk.Button(master=frame, text="clear",
                                 padx=15, pady=5, width=12, command=clear)
        button_clear.grid(row=6, column=1, columnspan=2, pady=2)

        button_equal = tk.Button(master=frame, text="=", padx=15,
                                 pady=5, width=9, command=equal)
        button_equal.grid(row=7, column=0, columnspan=3, pady=2)

        window.mainloop()

The marked area is the colour I want to change the colour of. (I have marked it with black pen) enter image description here

I Just want to change the colour.

1
  • Is there another instance of Tk() created when creating instance of PomodoroTimer? Try changing self.style = ttk.Style() to self.style = ttk.Style(master=self.root).
    – acw1668
    Commented Oct 27, 2023 at 6:23

2 Answers 2

0

You have several instances of Tk(), which may cause problems.

def __init__(self):
    self.root = tk.Tk()

def open_note(self):
    window = tk.Tk()

def op_calc(self):
    window = tk.Tk()

See Why are multiple instances of Tk discouraged?

Each Tcl interpreter is an isolated sandbox. An object in one sandbox cannot interact with objects in another. The most common manifestation of that is that a StringVar created in one interpreter is not visible in another. The same goes for widgets -- you can't create widgets in one interpreter that has as a parent widget in another interpreter. Images are a third case: images created in one cannot be used in another.

Also see this discussion of application structure: What is the best way to structure a Tkinter application?

Hope this may be of help.

0

I can't change the colour of the frame area..

Use variable then prefix. You can use any variable name, but don't use the same variable for each Frame.

Change this on line 23:

self.style.configure("TFrame", background="red")

to:

self.style.configure("My.TFrame", background="red") #Add variable

Change this on line 31:

self.tab1 = ttk.Frame(self.tabs, style="TFrame")

to:

self.tab1 = ttk.Frame(self.tabs, style="My.TFrame")

Screenshot:

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.