I am making a time-tracker using a tkinter entry widget that allows me to enter whatever activity I am doing and then logs the note and corresponding time of day in a csv file. I also used keyboard to create a keyboard shortcut - 'ctrl + q' to open the widget when the python script is running. My question is, while 'ctrl + q' opens/creates the widget on my screen, how can I also get 'ctrl + q' to make a widget that is already open but behind other windows jump to the front of the screen? That way I will be able to both open the widget in the first place as well as make it visible if already open with the same keyboard shortcut.
Here's what I tried. When I run this, hitting 'ctrl + q' opens the entry widget, but if I put another open window on top of the widget, hitting 'ctrl + q' again does not bring it to the front.
while True:
try:
if keyboard.is_pressed('ctrl + q'):
def timenote(): # this gets the text entered into the entry widget, saves, the text and datetime to csv, displays both on the entry widget, and then clears the entry box
timenotedict = {}
Now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
mtext = time_entry.get()
timenotedict[mtext] = Now
label2text = Now + ' ' + mtext
mlabel2 = Label(root, text= label2text).pack()
with open('C:/Users/Me/Documents/timelog_test.csv', 'a', newline='') as csv_file:
writer = csv.writer(csv_file)
for key, value in timenotedict.items():
writer.writerow([key, value])
mEntry.delete(0, END)
root = Tk()
time_entry = StringVar()
root.geometry('250x100')
root.title('Time Tracker')
mlabel = Label(root, text = 'Timekeeping Note').pack()
mbutton = Button(root, text = 'Log', command = timenote).pack()
mEntry = Entry(root, textvariable = time_entry)
mEntryPacked = mEntry.pack()
#below is the problematic part
try:
if keyboard.is_pressed('ctrl + q'):
root.lift()
else:
pass
except:
break
root.mainloop()
else:
pass
except:
break
I am pretty new to using tkinter so perhaps I'm missing something about how it works here? I am also not any sort of expert in python; I was just tasked with coming up with an easier way to track time at my job and this is my attempt, but that means there's a very real possibility my code could problematic in other respects which I am missing. Any thoughts would be much appreciated. Thanks!