简体   繁体   English

在tkinter中将单个像素绘制到画布上

[英]Drawing individual pixels to a canvas in tkinter

I'm using Tkinter to create a window in python and a canvas to display graphics in the window. 我正在使用Tkinter在python中创建一个窗口,并使用画布在该窗口中显示图形。 This is working fine so far. 到目前为止,一切正常。

But I have a two dimensional list containing colours that I would like to directly place on the canvas. 但是我有一个二维列表,其中包含我想直接放在画布上的颜色。

Example

I have a class defined (named CRGB) that has three variables: r, g and b. 我有一个定义的类(名为CRGB),它具有三个变量:r,g和b。 These are the red, green and blue values of a colour, and are integers between 0 and 255. 这些是颜色的红色,绿色和蓝色值,并且是0到255之间的整数。

I also have a two-dimensional list, which contains CRGB objects with the colour data. 我还有一个二维列表,其中包含带有颜色数据的CRGB对象。

I then have a Canvas (defined in a variable called screenCanvas) which is the same size as the 2D list. 然后,我有一个Canvas(在名为screenCanvas的变量中定义),其大小与2D列表的大小相同。

How would I transfer the pixels from the 2D list to the canvas? 如何将像素从2D列表传输到画布?

Notes: I would like the code to work on Mac AND Windows, and not use any external libraries (libraries not included in Python by default.) 注意:我希望代码可以在Mac和Windows上运行,并且不使用任何外部库(默认情况下Python中不包含的库。)

The canvas has no way to draw an individual pixel, except to draw a line that is exactly one pixel long and one pixel wide. 画布无法绘制单个像素,只能绘制一条恰好一个像素长和一个像素宽的线。

If you only need to place pixels, and don't need the other features of the canvas widget, you can use a PhotoImage object. 如果需要放置像素,并且不需要画布小部件的其他功能,则可以使用PhotoImage对象。 An instance of PhotoImage has methods for setting individual pixels. PhotoImage的实例具有用于设置单个像素的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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