简体   繁体   English

Python:GUI - 在实时 GUI 中绘制、读取像素

[英]Python: GUI - plotting, reading from pixels in real-time GUI

I've got a project in the works.我有一个项目正在进行中。 I'm a rookie, roommate is a software engineer and suggested I used python for this project.我是菜鸟,室友是软件工程师,建议我在这个项目中使用python。 My questions are listed below.我的问题列在下面。 first, here is an overview of what I am attempting to accomplish.首先,这是我试图完成的任务的概述。

project overview:项目概况:

An array of addressable RGB led matrices, say, 50 leds x 50 leds (250 leds).一组可寻址的 RGB LED 矩阵,例如 50 个 LED x 50 个 LED(250 个 LED)。 The led matrix is connected to and ran by an arduino which will receive the matrix's pattern information from a decentralized program. led 矩阵连接到一个 arduino 并由它运行,该 arduino 将从分散的程序接收矩阵的模式信息。 (We'll worry about the arduino's function later) (后面会担心arduino的功能)

The purpose of the program is to generate and send pattern information for each addressable LED to the arduino.该程序的目的是生成每个可寻址 LED 的模式信息并将其发送到 arduino。

The program will host a GUI in order to alter and visualize the outgoing or current matrix colormap and pattern in real-time (ie. turn on/off strobe effect, turn on/off fade effect).该程序将托管一个 GUI,以便实时更改和可视化输出或当前矩阵颜色图和图案(即打开/关闭频闪效果、打开/关闭淡入淡出效果)。 The program will then read from the gui to generate and translate RGB values to send to the arduino.然后程序将从 gui 中读取以生成并转换 RGB 值以发送到 arduino。

Here is where I am at, and I need guidance.这就是我所在的地方,我需要指导。 As of now, I am focusing on getting the GUI to work properly before moving on to the next parts of this project.到目前为止,我的重点是让 GUI 正常工作,然后再继续这个项目的下一部分。

I'm using matplotlib in hopes that I can create a plot of 50x50 squares (or pixels) and retain control over each individuals point's value and struggling greatly.我正在使用 matplotlib,希望我可以创建一个 50x50 正方形(或像素)的图,并保留对每个点的值的控制,并且非常挣扎。 Ideally, I would be able to draw to the plot 30 times a second, or however many times so that it would appear to be updating in "real-time".理想情况下,我可以每秒绘制绘图 30 次,或者多次绘制,以便它看起来“实时”更新。

Here is some sample code so you can get a better understanding of what I am trying to accomplish:这是一些示例代码,以便您可以更好地了解我要完成的任务:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import cm
from numpy.random import random

fig = plt.figure()
matrix = random((50,50))
plt.imshow(matrix, interpolation='nearest', cmap=cm.spectral)


def update(data):
    print("IN UPDATE LOOP")
    matrix = random((50,50))
    return matrix

def data_gen():
    print("IN DATA_GEN LOOP")
    while True: yield np.random.rand(10)


ani = animation.FuncAnimation(fig, update, data_gen, interval=1000)
plt.imshow(matrix, interpolation='nearest', cmap=cm.spectral)
plt.show()
plt.draw()

Photo of matrix with random values assigned to each square随机值分配给每个方块的矩阵照片

Grid won't update, not sure why...网格不会更新,不知道为什么...

Why is my grid not updating?为什么我的网格没有更新?

Ignoring the first two questions as they are not really on topic here, the problem with the code is that you never actually update the image.忽略前两个问题,因为它们不是真正的主题,代码的问题在于您从未真正更新图像。 This should be done in the animating function.这应该在动画功能中完成。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import cm
from numpy.random import random

fig = plt.figure()
matrix = random((50,50))
im = plt.imshow(matrix, interpolation='nearest', cmap=cm.Spectral)

def update(data):
    im.set_array(data)

def data_gen():
    while True: 
        yield random((50,50))

ani = animation.FuncAnimation(fig, update, data_gen, interval=1000)

plt.show()

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

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