0

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!

br997
  • 13
  • 3
  • 1
    system (Windows/LInux/Mac) sends pressed keys only to active window. And you can't change it with `tkinter`. Other tools can register global shortcut in system which can works in any place. There is also module `pynput` which can listen for key in every place and when you click some combination then it may runs code - and this code would have to find your window and move it to top. But this is other problem - how to ask system to move window to top :). – furas Oct 15 '19 at 20:02
  • [pynput - monitoring keyboard](https://pythonhosted.org/pynput/keyboard.html#monitoring-the-keyboard) – furas Oct 15 '19 at 20:04
  • [Windows 7: how to bring a window to the front no matter what other window has focus?](https://stackoverflow.com/questions/6312627/windows-7-how-to-bring-a-window-to-the-front-no-matter-what-other-window-has-fo) – furas Oct 15 '19 at 20:09

0 Answers0