Tkinter:主线程不在主循环中

时间:2019-11-22 19:50:51

标签: python python-3.x tkinter pynput

我早些时候遇到了这个错误,所以我开始将代码放入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()

错误:

  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

0 个答案:

没有答案