rpiutsproyect
Posts: 11
Joined: Mon Jan 20, 2014 3:08 pm

Pygame to Tkinter

Thu Feb 13, 2014 3:50 pm

My problem is that I need to change the code that I do with pygame by one in Tkinter and I do not how to do that. To be clear I need to do in Tkinter exactly what I am doing in pygame.

Code: Select all

import pygame
import time
import math

canvas_width=1308
canvas_height=720
canvas_s=1000

rojo=pygame.Color(255,0,0)
verde=pygame.Color(0,255,0)
azul=color=pygame.Color(0,0,255)
amarillo=pygame.Color(255,255,0,0)
marron=color=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()
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)

running=True
while running:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False

    surface.fill(background_color)
    pygame.draw.line(surface, lin, (0, 90), (1000, 90))
    pygame.draw.line(surface, lin, (0, 180), (1000, 180))
    pygame.draw.line(surface, lin, (0, 270), (1000, 270))
    pygame.draw.line(surface, lin, (0, 360), (1000, 360))
    pygame.draw.line(surface, lin, (0, 450), (1000, 450))
    pygame.draw.line(surface, lin, (0, 540), (1000, 540))
    pygame.draw.line(surface, lin, (0, 630), (1000, 630))

    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()
I base on the code of this page http://ericeastwood.com/blog/7/animated ... nd-tkinter, but the second code that is on the page do not work to me because show me
TclError: can't parse color #000000#000000#000000...ffff00#000000#000000#000000, also Error in line 26 in sine_wave_anim
I send a message to the person that wrote that code and he sent to me this code:

Code: Select all

from Tkinter import Tk, Canvas, PhotoImage, mainloop
import math
import time
# Used to store debug file
#import os
#BASE_DIR = os.path.realpath(os.path.dirname(__file__))
# Some config width height settings
canvas_width = 640
canvas_height = 480
# Create a window
window = Tk()
# Set the window title
window.wm_title("Sine Wave")
# Put a canvas on the window
canvas = Canvas(window, width=canvas_width, height=canvas_height, bg="#000000")
canvas.pack()
# Create a image, this acts as the canvas
img = PhotoImage(width=canvas_width, height=canvas_height)
# Put the image on the canvas
canvas.create_image(canvas_width/2, canvas_height/2, image=img, state="normal")
# Create some text
# See doc: http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_text-method
text = canvas.create_text(canvas_width/2, canvas_height/2, font=("Arial", 12), fill="#ffffff", text="Woah text here")
canvas.pack() # show it on the canvas
def sine_wave_anim():
	# Animate the text back and forth
	canvas.coords(text, (canvas_width/2) + (canvas_width/2)*math.sin(.5*time.time()), canvas_height/2)
	# Update sine wave
	frequency = 4
	amplitude = 50 # in px
	speed = 1
	# We create a blank area for what where we are going to draw
	color_table = [["#000000" for x in range(0, canvas_width)] for y in range(0, amplitude*2)]
	# And draw on that area
	for x in range(0, canvas_width):
		y = int(amplitude + amplitude*math.sin(frequency*((float(x)/canvas_width)*(2*math.pi) + (speed*time.time()))))
		color_table[y][x] = "#ffff00"
		# Don't individually put pixels as tkinter sucks at this
		#img.put("#ffff00", (x, y))
	# Then batch put it on the canvas 
	# tkinter is extremely inefficient doing it one by one
	img.put(''.join("{" + (" ".join(str(color) for color in row)) + "} " for row in color_table), (0, int(canvas_height/2 - amplitude)))
	# Debug the color_table
	#with open(os.path.join(BASE_DIR, 'output.txt'), "w+") as text_file:
	#	text_file.write(''.join("{" + (" ".join(str(color) for color in row)) + "} " for row in color_table))
	# Continue the animation as fast as possible. A value of 0 (milliseconds), blocks everything.
	window.after(1, sine_wave_anim)
# Start off the anim
sine_wave_anim()
mainloop()
But he code do not work the same way that the first code, this time do not have any Error, the problem is that the program do not execute (the screen is left thinking) until I press ctrl+c (KeyboardInterrupt) and then appears the sine wave and a text without movement.

Return to “General discussion”