0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Code

focus.py
import tkinter
import matplotlib.pyplot as plt
from matplotlib.colors import rgb2hex
import time
import sched

cmap = plt.get_cmap("jet")
def score_to_rgb(score):
    return rgb2hex(cmap(1-score/10))
    
pad_font=(None, 70)
main_font=(None, 13)
text_di={'title':'title','x':'x axis', 'y':'y axis'}
mins=30
current_target=None
target_text=None

main = tkinter.Tk()
main.geometry("500x500")
 
hist_scores=[]

ninekeys='789456123'
pad_text="{} {} {}\n{} {} {}\n{} {} {}".format(*[s for s in ninekeys])
key_to_n={k:v for k, v in zip([s for s in ninekeys], [3,4,5,2,3,4,1,2,3])}
label_title=tkinter.Label(master=main, font=main_font, text=text_di['title'])
label_y = tkinter.Label(master=main, font=main_font, justify='left', anchor="w", text=text_di['y'])
label_pad = tkinter.Label(master=main, font=pad_font, text=pad_text)
label_x = tkinter.Label(master=main, font=main_font, justify='right', anchor="e", text=text_di['x'])
label_setting = tkinter.Label(master=main, font=main_font, text='pop up every {} mins'.format(str(mins)))

label_title.place(relx=0.05, rely=0)
label_y.place(relx=0.05, rely=0.05)
label_pad.place(relx=0.1, rely=0.1)
label_x.place(relx=0.15, rely=0.8)
label_setting.place(relx=0.2, rely=0.9)

def text_pad_reset():
    label_pad["text"]=pad_text
    main.deiconify()
    label_pad.focus_set()
    
def input_key(event):
    key_name = event.keysym
    if key_name in [s for s in ninekeys]:
        score=key_to_n[key_name]
        hist_scores.append(score)
        avg_score=sum(hist_scores[-5:])/len(hist_scores[-5:])
        event.widget["bg"]=score_to_rgb(score)
        event.widget["text"]='{}:{}\navg:{:.2f}'.format(key_name,score,avg_score)
        main.configure(bg=score_to_rgb(avg_score))
        label_pad.after(300, main.withdraw)
        label_pad.after(2000, text_pad_reset)
        
def when_click(event):
    popupwin(event)
    
def close_win(top, entry, event):
    event.widget["text"]=entry.get()
    top.destroy()

def popupwin(event):
    top= tkinter.Toplevel(main)
    top.geometry("750x100")
    entry= tkinter.Entry(top, width= 25)
    entry.pack()
    button= tkinter.Button(top, text="OK", command=lambda:close_win(top, entry, event))
    button.pack(pady=5, side= tkinter.TOP)

def close_win_mins(top, entry, event):
    global mins
    mins=int(entry.get())
    event.widget["text"]='pop up every {} mins'.format(str(mins))
    top.destroy()

def popupwin_mins(event):
    top= tkinter.Toplevel(main)
    top.geometry("750x100")
    entry= tkinter.Entry(top, width= 25)
    entry.pack()
    button= tkinter.Button(top, text="OK", command=lambda:close_win_mins(top, entry, event))
    button.pack(pady=5, side= tkinter.TOP)
    
def when_click_mins(event):
    popupwin_mins(event)

def on_closing():
    main.destroy()

label_pad.bind("<KeyPress>", input_key)
label_title.bind("<Button>", when_click)
label_y.bind("<Button>", when_click)
label_x.bind("<Button>", when_click)
label_setting.bind("<Button>", when_click_mins)
main.protocol("WM_DELETE_WINDOW", on_closing)
main.attributes("-topmost", True)

label_pad.focus_set()
main.mainloop()
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?