ForkingPickler(file, protocol).dump(obj) TypeError: 无法pickle '_tkinter.tkapp' 对象 [英] ForkingPickler(file, protocol).dump(obj) TypeError: cannot pickle '_tkinter.tkapp' object

查看:143
本文介绍了ForkingPickler(file, protocol).dump(obj) TypeError: 无法pickle '_tkinter.tkapp' 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码,我的函数从服务器接收数据块并将它们放入一个 FIFO 缓冲区,然后并行调用两个函数,

recvData() #继续从服务器接收数据块并放入FIFO缓冲区
calculate_threshold() #从FIFO缓冲区中取出数据块并进行一些计算,在GUI上实时显示,并将结果写入文件

代码

<预><代码>进口插座进口龟#导入时间导入队列导入多处理从 tkinter 导入 *类图形用户界面:def __init__(self, master):self.master = 主人master.title(碰撞检测")self.buff_data = queue.Queue()self.p1 = multiprocessing.Process(target = self.recvData)self.p1 = multiprocessing.Process(target=self.calculate_threshold)self.input_label = Label(root, text = "输入所有光栅以nm为单位设置直线波长值")self.input_label.grid(row = 0)self.core_string = "核心";self.entries = []self.label_col_inc = 0self.entry_col_inc = 1self.core_range = range(1, 5)对于 self.core_range 中的 y:self.core_text = self.core_string + str(y) + '_' + '25'self.core_label = Label(root, text = self.core_text)self.entry = 条目(根)self.core_label.grid(row=1, column=self.label_col_inc,sticky=E)self.entry.grid(row=1, column=self.entry_col_inc)self.entries.append(self.entry)self.label_col_inc += 2self.entry_col_inc += 2self.threshold_label = Label(root, text = 以纳米为单位的阈值")self.entry_threshold = 入口(根)self.threshold_label.grid(row = 2, 粘性 = E)self.entry_threshold.grid(row = 2, column = 1)self.light_label = Label(root, text = 'Status')self.light_label.grid(row = 3, column = 3)self.canvas = Canvas(root, width = 150, height = 50)self.canvas.grid(row = 4, column = 3)#绿灯self.green_light = turtle.RawTurtle(self.canvas)self.green_light.shape('圆形')self.green_light.color('灰色')self.green_light.penup()self.green_light.goto(0,0)# 红灯self.red_light = turtle.RawTurtle(self.canvas)self.red_light.shape('圆圈')self.red_light.color('灰色')self.red_light.penup()self.red_light.goto(40,0)self.data_button = Button(root, text = 获取高于阈值的数据",命令 = self.getData)self.data_button.grid(row = 5, column = 0)# 接收TCP数据块的函数def getData(self):主机 = '127.0.0.1'端口 = 5000s = socket.socket()s.connect((主机,端口))len_message = s.recv(4)bytes_length = int(len_message.decode('utf-8')) #为自制服务器recvd_data = s.recv(bytes_length)self.buff_data.put(recvd_data)self.p1.start()self.p2.start()self.p1.join()self.p2.join()def recvData(self):len_message = s.recv(4)而 len_message:bytes_length = int(len_message.decode('utf-8')) #为自制服务器recvd_data = s.recv(bytes_length)self.buff_data.put(recvd_data)len_message = s.recv(4)别的:打印('循环外')s.close()定义计算阈值(自我):rmv_data = self.buff_data.get()stringdata = rmv_data.decode('utf-8')rep_str = stringdata.replace(",", ".")splitstr = rep_str.split()# 接收波长值公司 = 34wav_threshold = []对于 self.entries 中的 y:direct_wav = float(y.get())wav = float(splitstr[inc])wav_diff = wav - direct_wav如果 wav_diff <0:wav_diff = wav_diff * (-1)wav_threshold.append(wav_diff)公司 += 56阈值 = 浮点数(self.entry_threshold.get())# 写入文件数据 = []INC1 = 0列 1 = 2列 2 = 6data.insert(0, (str(splitstr[0])))data.insert(1, (str(splitstr[1])))对于 wav_threshold 中的 x:如果(x>阈值):self.red_light.color('红色')self.green_light.color('灰色')data.insert(col1, (str(splitstr[34 + inc1])))data.insert(col2,(str(x)))别的:self.red_light.color('灰色')self.green_light.color('绿色')data.insert(col1,'-')data.insert(col2,'-')inc1 += 56列 1 += 1列 2 += 1self.write_file(数据)# 写入文件的函数def write_file(self,data):使用 open("Output.txt", "a") 作为 text_file:text_file.write('\t'.join(data[0:]))text_file.write('\n')如果 __name__ == '__main__':根 = Tk()gui = GUI(根)root.mainloop()

但我收到以下错误:

<预><代码>Tkinter 回调中的异常回溯(最近一次调用最后一次):文件C:\Users\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py",第 1883 行,在 __call__ 中返回 self.func(*args)文件C:/Users/PycharmProjects/GUI/GUI_v4.py",第 88 行,在 getData 中self.p1.start()文件C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py",第 121 行,在开始self._popen = self._Popen(self)文件C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\context.py",第 224 行,在 _Popen返回_default_context.get_context().Process._Popen(process_obj)文件C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\context.py",第 326 行,在 _Popen返回 Popen(process_obj)文件C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\popen_spawn_win32.py",第 93 行,在 __init__ 中减少.转储(process_obj,to_child)转储中的文件C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\reduction.py",第 60 行ForkingPickler(文件,协议).转储(对象)类型错误:无法腌制_tkinter.tkapp"对象回溯(最近一次调用最后一次):文件<string>",第 1 行,在 <module>文件C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py",第 116 行,在 spawn_main 中exitcode = _main(fd, parent_sentinel)文件C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py",第 126 行,在 _mainself = reduction.pickle.load(from_parent)EOFError:输入不足

有人能告诉我这里到底哪里出错了吗?

解决方案

创建新类后代码更改:

导入套接字进口龟# 导入时间导入队列导入多处理从 tkinter 导入 *类图形用户界面:def __init__(self, master):self.master = 主人master.title(碰撞检测")self.input_label = Label(root, text=输入所有光栅以nm为单位设置直线波长值")self.input_label.grid(row=0)self.core_string = "核心";self.entries = []self.label_col_inc = 0self.entry_col_inc = 1self.core_range = range(1, 5)对于 self.core_range 中的 y:self.core_text = self.core_string + str(y) + '_' + '25'self.core_label = Label(root, text=self.core_text)self.entry = 条目(根)self.core_label.grid(row=1, column=self.label_col_inc,sticky=E)self.entry.grid(row=1, column=self.entry_col_inc)self.entries.append(self.entry)self.label_col_inc += 2self.entry_col_inc += 2self.threshold_label = Label(root, text =以纳米为单位的阈值")self.entry_threshold = 入口(根)self.threshold_label.grid(row=2,sticky=E)self.entry_threshold.grid(row=2, column=1)self.light_label = Label(root, text='Status')self.light_label.grid(row=3, column=3)self.canvas = Canvas(root, width=150, height=50)self.canvas.grid(row=4, column=3)#绿灯self.green_light = turtle.RawTurtle(self.canvas)self.green_light.shape('圆形')self.green_light.color('灰色')self.green_light.penup()self.green_light.goto(0, 0)# 红灯self.red_light = turtle.RawTurtle(self.canvas)self.red_light.shape('圆圈')self.red_light.color('灰色')self.red_light.penup()self.red_light.goto(40, 0)self.data_button = Button(root, text=获取高于阈值的数据",命令=recv_data.getData)self.data_button.grid(row=5, column=0)类 recv_data:def __init__(self):self.buff_data = queue.Queue()self.p1 = multiprocessing.Process(target=self.recvData)self.p1 = multiprocessing.Process(target=self.calculate_threshold)self.host = '127.0.0.1'self.port = 5002self.s = socket.socket()self.s.connect((self.host, self.port))# 接收TCP数据块的函数def getData(self):len_message = self.s.recv(4)bytes_length = int(len_message.decode('utf-8')) #为自制服务器recvd_data = self.s.recv(bytes_length)self.buff_data.put(recvd_data)self.p1.start()self.p2.start()self.p1.join()self.p2.join()def recvData(self):len_message = self.s.recv(4)而 len_message:bytes_length = int(len_message.decode('utf-8')) #为自制服务器recvd_data = self.s.recv(bytes_length)self.buff_data.put(recvd_data)len_message = self.s.recv(4)别的:打印('循环外')self.s.close()定义计算阈值(自我):rmv_data = self.buff_data.get()stringdata = rmv_data.decode('utf-8')rep_str = stringdata.replace(",", ".")splitstr = rep_str.split()# 接收波长值公司 = 34wav_threshold = []对于 gui.entries 中的 y:direct_wav = float(y.get())wav = float(splitstr[inc])wav_diff = wav - direct_wav如果 wav_diff <0:wav_diff = wav_diff * (-1)wav_threshold.append(wav_diff)公司 += 56阈值 = 浮点数(gui.entry_threshold.get())# 写入文件数据 = []INC1 = 0列 1 = 2列 2 = 6data.insert(0, (str(splitstr[0])))data.insert(1, (str(splitstr[1])))对于 wav_threshold 中的 x:如果(x>阈值):gui.red_light.color('红色')gui.green_light.color('灰色')data.insert(col1, (str(splitstr[34 + inc1])))数据.插入(col2,(str(x)))别的:gui.red_light.color('灰色')gui.green_light.color('绿色')数据.插入(col1,'-')数据.插入(col2,'-')inc1 += 56列 1 += 1列 2 += 1self.write_file(数据)# 写入文件的函数def write_file(self, data):使用 open("Output.txt", "a") 作为 text_file:text_file.write('\t'.join(data[0:]))text_file.write('\n')如果 __name__ == '__main__':根 = Tk()gui1 = GUI(根)rec_data = recv_data()root.mainloop()

我得到的错误:

<预><代码>Tkinter 回调中的异常回溯(最近一次调用最后一次):文件C:\Users\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py",第 1883 行,在 __call__ 中返回 self.func(*args)类型错误:getData() 缺少 1 个必需的位置参数:'self'

Below is my code my function receives data blocks from the server and puts them in a FIFO buffer after which two functions are called in parallel,

recvData() #to keep receiving data blocks from the server and put in the FIFO buffer
calculate_threshold() #to remove data blocks from the FIFO buffer and perform some calculation, give a real time display on the GUI, and write the result in the file

Code


import socket
import turtle
#import timeit
import queue
import multiprocessing
from tkinter import *

class GUI:

    def __init__(self, master):
        self.master = master
        master.title("Collision Detection")
    
        self.buff_data = queue.Queue()

        self.p1 = multiprocessing.Process(target = self.recvData)
        self.p1 = multiprocessing.Process(target=self.calculate_threshold)
    
        self.input_label = Label(root, text = "Input all the gratings set straight wavelength values in nm")
        self.input_label.grid(row = 0)
    
        self.core_string = "Core "
        self.entries = []

        self.label_col_inc = 0
        self.entry_col_inc = 1
        self.core_range = range(1, 5)

        for y in self.core_range:
            self.core_text = self.core_string + str(y) + '_' + '25'
            self.core_label = Label(root, text = self.core_text)
            self.entry = Entry(root)
            self.core_label.grid(row=1, column=self.label_col_inc, sticky=E)
            self.entry.grid(row=1, column=self.entry_col_inc)
            self.entries.append(self.entry)
            self.label_col_inc += 2
            self.entry_col_inc += 2
    
        self.threshold_label = Label(root, text = "Threshold in nm")
        self.entry_threshold = Entry(root)

        self.threshold_label.grid(row = 2, sticky = E)
        self.entry_threshold.grid(row = 2, column = 1)
    
        self.light_label = Label(root, text = 'Status')
        self.light_label.grid(row = 3, column = 3)

        self.canvas = Canvas(root, width = 150, height = 50)
        self.canvas.grid(row = 4, column = 3)
    
        # Green light
        self.green_light = turtle.RawTurtle(self.canvas)
        self.green_light.shape('circle')
        self.green_light.color('grey')
        self.green_light.penup()
        self.green_light.goto(0,0)

        # Red light
        self.red_light = turtle.RawTurtle(self.canvas)
        self.red_light.shape('circle')
        self.red_light.color('grey')
        self.red_light.penup()
        self.red_light.goto(40,0)
    
        self.data_button = Button(root, text = "Get data above threshold", command = self.getData)
        self.data_button.grid(row = 5, column = 0)


# function to receive TCP data blocks
    def getData(self):
        host = '127.0.0.1'
        port = 5000
        s = socket.socket()
        s.connect((host, port))

        len_message = s.recv(4)
        bytes_length = int(len_message.decode('utf-8'))  # for the self-made server
        recvd_data = s.recv(bytes_length)
        self.buff_data.put(recvd_data)
        self.p1.start()
        self.p2.start()

        self.p1.join()
        self.p2.join()

        
    def recvData(self):
        len_message = s.recv(4)
        while len_message:
            bytes_length = int(len_message.decode('utf-8'))  # for the self-made server
            recvd_data = s.recv(bytes_length)
            self.buff_data.put(recvd_data)
            len_message = s.recv(4)

        else:
            print('out of loop')
        s.close()


    def calculate_threshold(self):
        rmv_data = self.buff_data.get()
        stringdata = rmv_data.decode('utf-8')
        rep_str = stringdata.replace(",", ".")
        splitstr = rep_str.split()

        # received wavelength values
        inc = 34
        wav_threshold = []
        for y in self.entries:
            straight_wav = float(y.get())
            wav = float(splitstr[inc])
            wav_diff = wav - straight_wav
            if wav_diff < 0:
                wav_diff = wav_diff * (-1)
            wav_threshold.append(wav_diff)
            inc += 56

        threshold = float(self.entry_threshold.get())

        # writing into the file
        data = []
        inc1 = 0
        col1 = 2
        col2 = 6

        data.insert(0, (str(splitstr[0])))
        data.insert(1, (str(splitstr[1])))

        for x in wav_threshold:
            if (x > threshold):
                self.red_light.color('red')
                self.green_light.color('grey')
                data.insert(col1, (str(splitstr[34 + inc1])))
                data.insert(col2,(str(x)))
            else:
                self.red_light.color('grey')
                self.green_light.color('green')
                data.insert(col1,'-')
                data.insert(col2,'-')
            inc1 += 56
            col1 += 1
            col2 += 1

        self.write_file(data)



# function to write into the file
    def write_file(self,data):
        with open("Output.txt", "a") as text_file:
            text_file.write('\t'.join(data[0:]))
            text_file.write('\n')

if __name__ == '__main__':
    root = Tk()
    gui = GUI(root)
    root.mainloop()

But I get the following error:


Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:/Users/PycharmProjects/GUI/GUI_v4.py", line 88, in getData
    self.p1.start()
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\context.py", line 326, in _Popen
    return Popen(process_obj)
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle '_tkinter.tkapp' object
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input

Can someone tell where exactly am I going wrong here?

解决方案

Code changed after creating a new class:

import socket
import turtle
# import timeit
import queue
import multiprocessing
from tkinter import *


class GUI:

    def __init__(self, master):
        self.master = master
        master.title("Collision Detection")

        self.input_label = Label(root, text="Input all the gratings set straight wavelength values in nm")
        self.input_label.grid(row=0)

        self.core_string = "Core "
        self.entries = []


        self.label_col_inc = 0
        self.entry_col_inc = 1
        self.core_range = range(1, 5)

        for y in self.core_range:
            self.core_text = self.core_string + str(y) + '_' + '25'
            self.core_label = Label(root, text=self.core_text)
            self.entry = Entry(root)
            self.core_label.grid(row=1, column=self.label_col_inc, sticky=E)
            self.entry.grid(row=1, column=self.entry_col_inc)
            self.entries.append(self.entry)
            self.label_col_inc += 2
            self.entry_col_inc += 2

        self.threshold_label = Label(root, text="Threshold in nm")
        self.entry_threshold = Entry(root)

        self.threshold_label.grid(row=2, sticky=E)
        self.entry_threshold.grid(row=2, column=1)

        self.light_label = Label(root, text='Status')
        self.light_label.grid(row=3, column=3)

        self.canvas = Canvas(root, width=150, height=50)
        self.canvas.grid(row=4, column=3)

        # Green light
        self.green_light = turtle.RawTurtle(self.canvas)
        self.green_light.shape('circle')
        self.green_light.color('grey')
        self.green_light.penup()
        self.green_light.goto(0, 0)

        # Red light
        self.red_light = turtle.RawTurtle(self.canvas)
        self.red_light.shape('circle')
        self.red_light.color('grey')
        self.red_light.penup()
        self.red_light.goto(40, 0)

        self.data_button = Button(root, text="Get data above threshold", command=recv_data.getData)
        self.data_button.grid(row=5, column=0)


class recv_data:
    def __init__(self):
        self.buff_data = queue.Queue()
        self.p1 = multiprocessing.Process(target=self.recvData)
        self.p1 = multiprocessing.Process(target=self.calculate_threshold)
        self.host = '127.0.0.1'
        self.port = 5002
        self.s = socket.socket()
        self.s.connect((self.host, self.port))

    # function to receive TCP data blocks
    def getData(self):
        len_message = self.s.recv(4)
        bytes_length = int(len_message.decode('utf-8'))  # for the self-made server
        recvd_data = self.s.recv(bytes_length)
        self.buff_data.put(recvd_data)
        self.p1.start()
        self.p2.start()

        self.p1.join()
        self.p2.join()

    def recvData(self):
        len_message = self.s.recv(4)
        while len_message:
            bytes_length = int(len_message.decode('utf-8'))  # for the self-made server
            recvd_data = self.s.recv(bytes_length)
            self.buff_data.put(recvd_data)
            len_message = self.s.recv(4)

        else:
            print('out of loop')
        self.s.close()

    def calculate_threshold(self):
        rmv_data = self.buff_data.get()
        stringdata = rmv_data.decode('utf-8')
        rep_str = stringdata.replace(",", ".")
        splitstr = rep_str.split()

        # received wavelength values
        inc = 34
        wav_threshold = []
        for y in gui.entries:
            straight_wav = float(y.get())
            wav = float(splitstr[inc])
            wav_diff = wav - straight_wav
            if wav_diff < 0:
                wav_diff = wav_diff * (-1)
            wav_threshold.append(wav_diff)
            inc += 56

        threshold = float(gui.entry_threshold.get())

        # writing into the file
        data = []
        inc1 = 0
        col1 = 2
        col2 = 6

        data.insert(0, (str(splitstr[0])))
        data.insert(1, (str(splitstr[1])))

        for x in wav_threshold:
            if (x > threshold):
                gui.red_light.color('red')
                gui.green_light.color('grey')
                data.insert(col1, (str(splitstr[34 + inc1])))
                data.insert(col2, (str(x)))
            else:
                gui.red_light.color('grey')
                gui.green_light.color('green')
                data.insert(col1, '-')
                data.insert(col2, '-')
            inc1 += 56
            col1 += 1
            col2 += 1

        self.write_file(data)

        # function to write into the file

    def write_file(self, data):
        with open("Output.txt", "a") as text_file:
            text_file.write('\t'.join(data[0:]))
            text_file.write('\n')

if __name__ == '__main__':
    root = Tk()
    gui1 = GUI(root)
    rec_data = recv_data()
    root.mainloop()

Error that I get:


Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
TypeError: getData() missing 1 required positional argument: 'self'

这篇关于ForkingPickler(file, protocol).dump(obj) TypeError: 无法pickle '_tkinter.tkapp' 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆