【问题标题】:Moving a sine wave in Tkinter在 Tkinter 中移动正弦波
【发布时间】:2014-02-15 17:03:10
【问题描述】:

我需要知道如何使用 Tkinter 在屏幕上看到移动的正弦波 我已经用 pygame 做到了,但我认为用 Tkinter 做到这一点很复杂。 我认为我需要做的第一件事是定义一个绘制正弦波的函数,然后定义其他函数来为这个绘制提供运动。那是对的吗?还是我想错了?如何在屏幕上移动正弦波?

我从 matplotlib.org 中提取的代码是:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)        # x-array
line, = ax.plot(x, np.sin(x))

def animate(i):
    line.set_ydata(np.sin(x+i/10.0))  # update the data
    return line,

#Init only required for blitting to give a clean slate.
def init():
    line.set_ydata(np.ma.array(x, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init, interval=25, blit=True)
plt.show()

【问题讨论】:

    标签: python python-2.7 animation tkinter trigonometry


    【解决方案1】:

    我不理解“移动”,但很可能会使用 matplotlib。

    import matplotlib.pyplot as plt
    
    fig = plt.figure(frameon=False)
    ax = fig.add_axes([0, 0, 1, 1])
    ax.axis('off')
    
    ax.plot(range(10))
    plt.show()
    

    【讨论】:

    • 我已经使用 matplotlib,现在我想使用 Tkinter 制作正弦波动画
    • 我不明白。但是你可以生成一个图片列表,并在 Tkinter 中连续显示它们。但是 matplotlib 解决方案有什么问题?
    • 因为我需要绘制不止一个正弦波(七个)。我不能有任何轴或按钮,我也必须将窗口的大小更改为全屏。我必须只可视化正弦波,仅此而已。
    • Matplotlib 是要走的路。它可以完成你提到的所有事情。
    • 你能给我代码,我尝试删除轴并失败为什么是我尝试使用 Tkinter 的原因
    【解决方案2】:

    在许多其他帖子的帮助下,我终于移动了七个正弦波。这是最终结果:

    import pygame
    import time
    import math
    import random
    
    canvas_width=1308
    canvas_height=720
    canvas_s=1004
    
    rojo=pygame.Color(255,0,0)
    verde=pygame.Color(0,255,0)
    azul=pygame.Color(0,0,255)
    amarillo=pygame.Color(255,255,0,0)
    marron=pygame.Color(85,65,0)
    morado=pygame.Color(255,0,255)
    naranja=pygame.Color(255,128,0)
    lin=pygame.Color(255,255,255)
    background_color=pygame.Color(0,0,0)
    
    pygame.init()
    # initialization of font module and creating a font to draw with
    pygame.font.init()
    fontdir=pygame.font.match_font('TimesNewRoman',False,False)
    myfont = pygame.font.Font(fontdir,60)
    pygame.display.set_caption("Monitor Signos Vitales")
    
    screen=pygame.display.set_mode((canvas_width,canvas_height))
    screen.fill(background_color)
    
    surface=pygame.Surface((canvas_width,canvas_s))
    surface.fill(background_color)
    
    # creating the random list and their corresponding surfaces
    random_list=[random.randrange(0,1000) for x in range(5)]
    text_list=[myfont.render(str(x),True,lin) for x in random_list]
    
    running=True
    while running:
        pos=(1220,0)
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                running=False
    
        surface.fill(background_color)
    
        for text in text_list:
            surface.blit(text,pos)
            pos=(pos[0],pos[1]+180)
    
        pygame.draw.line(surface, lin, (0, 90), (1004, 90))
        pygame.draw.line(surface, lin, (0, 180), (1004, 180))
        pygame.draw.line(surface, lin, (0, 270), (1004, 270))
        pygame.draw.line(surface, lin, (0, 360), (1004, 360))
        pygame.draw.line(surface, lin, (0, 450), (1004, 450))
        pygame.draw.line(surface, lin, (0, 540), (1004, 540))
        pygame.draw.line(surface, lin, (0, 630), (1004, 630))
        pygame.draw.line(surface, lin, (1004, 0), (1004, 720))
        pygame.draw.line(surface, lin, (1004, 180), (1308, 180))
        pygame.draw.line(surface, lin, (1004, 360), (1308, 360))
        pygame.draw.line(surface, lin, (1004, 540), (1308, 540))
    
        frecuency=2;frecuency0=4;frecuency1=8;frecuency2=16;frecuency3=5;frecuency4=10;frecuency5=15
        amplitude=30
        speed=2
        for x0 in range(0,canvas_s):
            y0=int((canvas_height/2)+amplitude*math.sin(frecuency*((float(x0)/canvas_s)*(2*math.pi)+(speed*time.time()))+270)-270)
            surface.set_at((x0,y0),amarillo)
        for x1 in range(0,canvas_s):
            y1=int((canvas_height/2)+amplitude*math.sin(frecuency0*((float(x1)/canvas_s)*(2*math.pi)+(speed*time.time()))+180)-180)
            surface.set_at((x1,y1),verde)
        for x2 in range(0,canvas_s):
            y2=int((canvas_height/2)+amplitude*math.sin(frecuency1*((float(x2)/canvas_s)*(2*math.pi)+(speed*time.time()))+90)-90)
            surface.set_at((x2,y2),naranja)
        for x3 in range(0,canvas_s):
            y3=int((canvas_height/2)+amplitude*math.sin(frecuency2*((float(x3)/canvas_s)*(2*math.pi)+(speed*time.time()))))
            surface.set_at((x3,y3),azul)
        for x4 in range(0,canvas_s):
            y4=int((canvas_height/2)+amplitude*math.sin(frecuency3*((float(x4)/canvas_s)*(2*math.pi)+(speed*time.time()))-90)+90)
            surface.set_at((x4,y4),rojo)
        for x5 in range(0,canvas_s):
            y5=int((canvas_height/2)+amplitude*math.sin(frecuency4*((float(x5)/canvas_s)*(2*math.pi)+(speed*time.time()))-180)+180)
            surface.set_at((x5,y5),marron)
        for x6 in range(0,canvas_s):
            y6=int((canvas_height/2)+amplitude*math.sin(frecuency5*((float(x6)/canvas_s)*(2*math.pi)+(speed*time.time()))-270)+270)
            surface.set_at((x6,y6),morado)
    
        screen.blit(surface,(0,0))
        pygame.display.flip()
    

    P.D:请投票

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-17
      • 2019-09-18
      相关资源
      最近更新 更多