Created
July 18, 2015 12:18
-
-
Save Yamabiko/372b829a6c3ba3886110 to your computer and use it in GitHub Desktop.
Python 2.x Tkinter 波形を少しずつ変えて描画しようとしたプログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
# | |
# Written by yamabiko | |
# 2015/July/03 | |
import Tkinter as tk | |
import math | |
canvas_width = 600 | |
canvas_height = 480 | |
class OscilloViewer(object): | |
def __init__(self, | |
width = canvas_width, | |
height = canvas_height, | |
right_bottom_offset = -2, | |
left_top_offset = 1): | |
self.tkroot = tk.Tk() | |
self.tkroot.title("Oscillo view ver. 0.1") | |
self.canvas_width = width | |
self.canvas_height = height | |
self.rbo = right_bottom_offset | |
self.lto = left_top_offset | |
self.wave = [0 for i in range(0, self.canvas_width)] | |
self.cycle = 10 | |
self.create_widgets() | |
def create_widgets(self): | |
self.oscanvas = tk.Canvas(self.tkroot, | |
bg = '#000000', | |
width = self.canvas_width, | |
height = self.canvas_height) | |
self.oscanvas.pack() | |
self.buttonFrame = tk.Frame(self.tkroot) | |
self.buttonFrame.pack(anchor=tk.CENTER) | |
self.startButton = tk.Button(self.buttonFrame, | |
text = 'START', | |
command = self.start) | |
self.startButton.pack(side=tk.LEFT) | |
self.quitButton = tk.Button(self.buttonFrame, | |
text = 'Quit', | |
command = self.tkroot.quit) | |
self.quitButton.pack(side=tk.LEFT) | |
self.set_wave([1 for i in range(0, self.canvas_width)], 2) | |
self.redraw() | |
def start_window(self): | |
self.tkroot.mainloop() | |
def set_wave(self, wave, norm): | |
w_len = len(wave) | |
c_len = self.canvas_width | |
if w_len < c_len: | |
count = 1 | |
span = c_len / (w_len - 1) | |
for x in range(0, self.canvas_width): | |
if count * span < x: | |
count += 1 | |
normalized = int(wave[count - 1] * float(self.anvas_height) / float(norm)) | |
self.wave[x] = [normalized % self.canvas_height, | |
normalized % self.canvas_height + 1] | |
elif w_len == c_len: | |
for x in range(0, self.canvas_width): | |
normalized = int(wave[x] * float(self.canvas_height) / float(norm)) | |
self.wave[x] = [normalized % self.canvas_height, | |
normalized % self.canvas_height + 1] | |
else: | |
for x in range(0, self.canvas_width): | |
left_end = int(float(x) * (float(w_len) / float(c_len))) | |
right_end = min([ | |
int(float(x + 1) * (float(w_len) / float(c_len))), | |
w_len]) | |
sliced = wave[left_end:right_end] | |
normalized_max = int(max(sliced) * float(self.canvas_height) / float(norm)) | |
normalized_min = int(min(sliced) * float(self.canvas_height) / float(norm)) | |
self.wave[x] = [normalized_min % self.canvas_height, | |
normalized_max % self.canvas_height + 1] | |
def redraw(self): | |
self.oscanvas.create_rectangle(self.lto, self.lto, | |
self.canvas_width - self.rbo, | |
self.canvas_height - self.rbo, | |
fill = '#000000') | |
for x in range(0, self.canvas_width): | |
self.oscanvas.create_line(x + self.lto, | |
self.wave[x][0] + self.lto, | |
x + self.lto, | |
self.wave[x][1] + self.lto, | |
fill = 'yellow') | |
def start(self): | |
while self.cycle < 200: | |
new_wave = [(math.sin(float(i) / float(self.cycle) * 3.141592) * 0.8 + 1.0) * 500.0 | |
for i in range(0, 2000)] | |
self.set_wave(new_wave, 1000) | |
self.redraw() | |
self.cycle += 1 | |
self.cycle = 10 | |
if __name__ == '__main__': | |
osv = OscilloViewer() | |
osv.start_window() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment