我使用tkinter的标签来显示图像,并使用ctypes构建图像。 我对ctypes感到不舒服,我认为我做错了什么,而且我看到了有线问题。我会尽力解释。
代码工作1-2分钟然后崩溃,我不得不继续上传新图片以重现问题。
这是伪python代码。
self.photo = ImageTk.PhotoImage(self.image)
self.label = tk.Label(parent, image=self.photo, width=480, height=480,
bg='black')
self.label.grid(row = 0, column = 0, sticky = W)
// C function which is called by ctypes:
//int generate_image(int width, int height, char *image)
// ctypes
img_lib = ctypes.CDLL('image_gen.dll')
img_lib.generate_image.argtypes = (ctypes.c_int, ctypes.c_int,
ctypes.POINTER(ctypes.c_uint8))
// All the below code is in the draw function, which is called periodically
using Threading.
image_data = np.zeros((480 * 480 * 4), dtype=np.uint8)
ctype_image_data = image_data.astype(ctypes.c_uint8)
pointer_image_data =
ctype_image_data.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8))
ctype_pointer_image_data = ctypes.cast(pointer_image_data,
ctypes.POINTER(ctypes.c_uint8))
#this line modifies the ctype_image_data.
img_lib.generate_image(width, height, ctype_pointer_image_data)
final_image_data = ctype_image_data.reshape(480,480,4)
final_image = Image.fromarray(final_image_data, 'RGBA')
#set the label with the image
self.photo = ImageTk.PhotoImage(final_image)
self.label.configure(image=self.photo, width=0, height=0)
我看到的错误都是无关的
处理完成,退出代码为-1073741571(0xC00000FD) 进程以退出代码xxxxxxxxxx(0xC0000005)结束 处理完成,退出代码为-1073740771(0xC000041D)
TclError:屏幕距离不好“tk :: EntryAutoScan .83647664.83715176.83715496” TclError:未知选项“1” TclError:错误选项“tk :: ButtonUp”:必须是cget或configure 还有很多其他的TclError,它在大多数时候都是不同的。
所有错误都在以下行中, self.label.configure(image = self.photo,width = 0,height = 0)