0

I'm trying to write a program that I can hide and show via hotkeys. I managed to get the application to show and hide using the library "keyboard", however due to the "wait" function of the library, it prevents the Text box from functioning correctly. I have tried using the key bindings within Tkinter, however I had a different problem, whereby once the program was hidden or another application was selected, I couldn't return the focus to the hidden window via the hotkey.

import Tkinter as Tk
import keyboard

class MyApp(object):

    def __init__(self, parent):
        self.root = parent
        self.root.title("Main frame")

        self.frame = Tk.Frame(parent)
        self.frame.pack()

        self.editor = Tk.Text(self.frame)
        self.editor.pack()
        self.editor.config(font="Courier 12")
        self.editor.focus_set()


        keyboard.add_hotkey('ctrl+alt+s', self.show)
        keyboard.add_hotkey('ctrl+alt+h', self.hide)
        keyboard.wait()

        self.root.withdraw() 


    def show(self):
        self.root.update()
        self.root.deiconify()

    def hide(self):
        self.root.withdraw()


if __name__ == "__main__":
    root = Tk.Tk()
    root.geometry("800x600")
    app = MyApp(root)
    root.mainloop()

Any assistance would be great :)

1 Answer 1

4

Just drop this wait command, its an additional mainloop, which is not needed as Tkinter does its job. I tried to fix your problem with threading, but as I wanted to check exactly what is NOT working, I accidentially made what I suppose you wanted to. So the Code is:

import tkinter as tk
import keyboard

class App(tk.Tk):

    def __init__(self):
        super().__init__()
        self.geometry("800x600")
        self.title("Main frame")

        self.editor = Tk.Text(self)
        self.editor.pack()
        self.editor.config(font="Courier 12")
        self.editor.focus_set()


        keyboard.add_hotkey('ctrl+alt+s', self.show)
        keyboard.add_hotkey('ctrl+alt+h', self.hide)


    def show(self):
        self.update()
        self.deiconify()

    def hide(self):
        self.update()
        self.withdraw()


if __name__ == "__main__":
    App().mainloop()

I hope this works for you. I'd also recommend changing this key settings. Testing with those in PyZo is IMPOSSIBLE! It always tries to "save as...", which I don't want to...

6
  • Hi, I'm afraid your code doesn't work for me. The hotkeys no longer work, and when used they appear to crash the application :( Commented May 28, 2018 at 18:09
  • Maybe. I Tried in python 3.5, there it worked fine. Which error do you get? Which Pythoversion? Obviously 2.x, but which one? I'll try doing it with threading then...
    – Grimmauld
    Commented May 28, 2018 at 18:17
  • I'm using python 2.7. I got no error, just that the hotkeys dont appear to make the application show or hide and when I press them it places the application in a "not responding" state. Commented May 28, 2018 at 19:06
  • Don't worry! I upgraded to python 3.6 and your code appears to be working now :) thank you Commented May 28, 2018 at 19:09
  • nice :-) Should I keep trying with threading, as it is kind of a challenge, or is this solved?
    – Grimmauld
    Commented May 28, 2018 at 19:19

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.