Exception in Tkinter callback Traceback (most recent call last): File "D:\Download\lib\tkinter\__init__.py", line 1921, in __call__ return self.func(*args) File "D:\study\作业\image\main.py", line 329, in <lambda> button4 = Button(root2, text='执行程序', relief=GROOVE, font=('微软雅黑', 14),command=lambda:mediumbutton(img))#执行程序按钮 File "D:\study\作业\image\main.py", line 319, in mediumbutton img_medium1 = merge(img) File "D:\study\作业\image\main.py", line 302, in merge zhifangtu1 = channel_hist(test) File "D:\study\作业\image\main.py", line 288, in channel_hist img = Image.frombytes('RGBA',canvas.get_width_height(),buf) File "D:\Download\lib\site-packages\PIL\Image.py", line 2843, in frombytes im.frombytes(data, decoder_name, args) File "D:\Download\lib\site-packages\PIL\Image.py", line 795, in frombytes s = d.decode(data) TypeError: argument 1 must be read-only bytes-like object, not memoryview
时间: 2023-08-12 18:07:57 浏览: 59
【数据驱动】复杂网络的数据驱动控制附Matlab代码.rar
这个错误是因为 `frombytes()` 方法需要一个 bytes-like object 参数,但是你传递的是一个 memoryview 对象。你可以将 memoryview 对象转换为 bytes 对象,然后再传递给 `frombytes()` 方法。
请将代码中的一行修改为如下代码:
```
img = Image.frombytes('RGBA', canvas.get_width_height(), bytes(buf))
```
修改后的代码中,我们使用 `bytes()` 方法将 `buf` 转换为 bytes 对象,然后再传递给 `frombytes()` 方法。这样就可以避免 TypeError 异常了。
请尝试修改代码并重新运行程序,看看是否可以解决问题。
阅读全文