0

Hello my toggle is not working,

Working code: when I press x a 45 second timer starts. After 45 seconds the timer goes invisible, and then when I press x again nothing happens.

What I want to achieve: after 45 sec I want to click x again to start the timer again and keep continuing this:

from tkinter import *
import keyboard
from playsound import playsound

root = Tk()

root.geometry("+0+0")
root.overrideredirect(True)
root.wm_attributes("-topmost", True)
root.wm_attributes("-alpha", 0.01)
root.resizable(0, 0)

seconds = 45

toggle_button = 'x'

enabled = False

def countdown(time):
    if time > 0:
        mins, secs = divmod(time, 60)

        def color_change(t_time):
            if t_time > 10:
                return 'green'
            elif 7 <= t_time <= 10:
                return 'yellow'
            elif t_time < 7:
                return 'red'

        timer_display.config(text="{:02d}:{:02d}".format(mins, secs),
                             fg=color_change(time)), root.after(1000, countdown, time - 1)
    else:
        root.wm_attributes('-alpha', 0.01)


def start_countdown():
    root.wm_attributes('-alpha', 0.7)
    countdown(seconds)


timer_display = Label(root, font=('Trebuchet MS', 30, 'bold'), bg='black')
timer_display.pack()

last_state = False


while True:
    key_down = keyboard.is_pressed(toggle_button)
    # If the toggle button is pressed, toggle the enabled value and print
    if key_down != last_state:
        last_state = key_down
        if last_state:
            enabled = True
            if enabled:
                start_countdown()
                print("Activated")
                playsound('count.mp3')
            else:
                start_countdown()
        root.mainloop()
Owen Singh
  • 15
  • 7
  • The `tkinter` and `keyboard` module are incompatible. – martineau Aug 08 '20 at 16:24
  • dude its working theres something wrong in the loop program is working for one time finr but the toggle is not working. I think it is achievable i will wait for other people to reply – Owen Singh Aug 08 '20 at 16:52
  • @OwenSingh Your whileloop needs to break as soon as you call the `mainloop`. Maybe you will take a look at this https://stackoverflow.com/questions/63118430/create-a-main-loop-with-tkinter/63118515#63118515 – Thingamabobs Aug 08 '20 at 17:20
  • One reason the modules don't play together well because both handle the keyboard at a low-level. Use one _or_ the other. – martineau Aug 08 '20 at 17:25

1 Answers1

1

In your code, the tkinter loop is blocking the main loop. You need to exit the tk loop when the timer is complete. You also need to start the tk loop only if you're starting the timer, otherwise the tk loop will never exit.

Here's the working code:

import tkinter as tkr
import keyboard
from playsound import playsound

root = None
timer_display = None

root = tkr.Tk()
root.geometry("+0+0")
root.overrideredirect(True)
root.wm_attributes("-topmost", True)
root.wm_attributes("-alpha", 0.01)
root.resizable(0, 0)

timer_display = tkr.Label(root, font=('Trebuchet MS', 30, 'bold'), bg='black')
timer_display.pack()

seconds = 45

toggle_button = 'x'

enabled = False

def countdown(time):
    if time > 0:
        mins, secs = divmod(time, 60)

        def color_change(t_time):
            if t_time > 10:
                return 'green'
            elif 7 <= t_time <= 10:
                return 'yellow'
            elif t_time < 7:
                return 'red'

        timer_display.config(text="{:02d}:{:02d}".format(mins, secs),
                             fg=color_change(time)), root.after(1000, countdown, time - 1)
    else:
        root.wm_attributes('-alpha', 0.01)
        root.quit()  # exit tk root loop


def start_countdown():
    root.wm_attributes('-alpha', 0.7)
    countdown(seconds)

last_state = False


while True:
    key_down = keyboard.is_pressed(toggle_button)
    # If the toggle button is pressed, toggle the enabled value and print
    if key_down != last_state:
        last_state = key_down
        if last_state:
            enabled = True
            if enabled:
                start_countdown()
                print("Activated")
                playsound('count.mp3')
            else:
                start_countdown()
            root.mainloop()  # timer will exit this loop
Mike67
  • 11,175
  • 2
  • 7
  • 15
  • Thanks mikey boy you are the best i was trying it for more than 2 days everybody on forums and discord server of python and many places said its impossible you cant use both together but you did it thanks mate you are the best – Owen Singh Aug 08 '20 at 18:04