简体   繁体   中英

Python 3.x multiprocessing tkinter mainloop

How do you use multiprocessing on root.mainloop ? I am using Python 3.6. I need to do lines of code after it, some requiring the object.
I do not want to create a second object , like some of the other answers for my question suggest. Here is a little code snippet (set being a JSON object):

from multiprocessing import Process
def check():
    try: sett['setup']
    except KeyError:
        sett['troubleshoot_file']=None
        check()
    else:
        if sett['setup'] is True: return
        elif type(sett['setup']) is not bool: raise TypeError('sett[\'setup\'] is not a type of boolian (\'bool\')')
    root.=Tk()
    root['bg']='blue'
    mainloop=Process(target=root.mainloop)
    mainloop.start()
    mainloop.join()
check()

However, I get this traceback:

Traceback (most recent call last):
  File "(directory)/main.py", line 41, in <module>
    check()
  File "(directory)/main.py", line 39, in check
    mainloop.start()
  File "C:\Program Files (x86)\Python36-32\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Program Files (x86)\Python36-32\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Program Files (x86)\Python36-32\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Program Files (x86)\Python36-32\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Program Files (x86)\Python36-32\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle _tkinter.tkapp objects

I have tried running:

from queue import Queue
from tkinter import Tk
from multiprocessing import Process
p=Process(target=q.get())

The interpreter then completely crashes.

You cannot use any tkinter objects across multiple processes or threads. If you need to share data between the gui and other processes you will need to set up a queue, and poll the queue from the GUI.

The reason for this is that tkinter is a wrapper around a tcl interpreter that knows nothing about python threads or processes.

You will find a link on how to do this at: docs.python.org/3.6/library/queue.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM