0

I got this error a little bit ago, so I started putting code into main, which temporarily fixes the problem. But now I have run into a roadblock as it seems to fail to run code outside of my script? For anyone wondering, it will be a screenshot tool, i have code working for the screenshot part and i am halfway through the cropping part but i wanted to merge the two together haha

main:

if __name__ == '__main__':
    def listen():
        with Listener(
                on_press=on_press,
                on_release=on_release) as listener:
                listener.join()
    def on_press(key):
        print('{0} pressed'.format(
            key))
    def on_release(key):
        print('{0} release'.format(
            key))
        if key == Key.print_screen:

            im = pysc.grab()
            im.save('P:/Python/temp/screenshot.png')            
            print("-------------------  Screenshot exported  -------------------")


            WIDTH, HEIGHT = 1919, 1079
            BACKGROUND = 'grey'
            TITLE = ' '

            root.title(TITLE)
            root.geometry('%sx%s' % (WIDTH, HEIGHT))
            root.configure(background=BACKGROUND)

            root.attributes("-fullscreen", True)

            app = Application(root, background=BACKGROUND)

            app.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.TRUE)
            app.mainloop()

    listen()

Error:

  File "P:\Python\screenshotselection - Copy.py", line 217, in on_release
    root.title(TITLE)
  File "C:\Program Files (x86)\Python38-32\lib\tkinter\__init__.py", line 2217, in wm_title
    return self.tk.call('wm', 'title', self._w, string)
RuntimeError: main thread is not in main loop
martineau
  • 119,623
  • 25
  • 170
  • 301
  • It's very likely that `pynput` isn't compatible with `tkinter` because they both want to handle user-input events. tkinter does this in its `mainloop()`. – martineau Nov 22 '19 at 19:54
  • Possible duplicate of [How to run pynput.Listener simultaneously with tkinter.Tk().mainloop()](https://stackoverflow.com/questions/56871975/how-to-run-pynput-listener-simultaneously-with-tkinter-tk-mainloop) – stovfl Nov 22 '19 at 19:57
  • @martineau that would make sense. what is the best way you know of to handle keyboard input? – Incineratinq Nov 22 '19 at 19:57
  • @stovfl by the looks of it, tkinter has a way of handling keyboard inputs so i will give that a try, thanks – Incineratinq Nov 22 '19 at 19:59
  • 2
    You might be able to do it with just tkinter. It has a ways of binding "events" to functions. Here's some (slightly outdated) [documentation](https://web.archive.org/web/20190509213522id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/events.html). – martineau Nov 22 '19 at 20:00
  • Why are you using a listener? Tkinter has native support for event handling. You don't need to use a listener. – Bryan Oakley Nov 22 '19 at 20:26
  • I found more documentation on events+binding [here](https://effbot.org/tkinterbook/tkinter-events-and-bindings.htm) and it doesn't look like you're able to listen without focus, and i want the program to listen infinitely in the background. Is anyone aware of a way to do this with tkinter? – Incineratinq Nov 23 '19 at 13:23

0 Answers0