Created
July 18, 2015 13:17
-
-
Save Yamabiko/c552e7537c1887d610cd 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, 1] for i in range(0, self.canvas_width)] | |
self.cycle = 10 | |
self.create_widgets() | |
self.timer_func = False # added | |
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 = lambda : self.set_timer_func(self.start)) # revised | |
self.startButton.pack(side=tk.LEFT) | |
self.stopButton = tk.Button(self.buttonFrame, | |
text = 'STOP', | |
command = lambda : self.set_timer_func(False)) # added | |
self.stopButton.pack(side=tk.LEFT) # added | |
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 set_timer_func(self, f): # added | |
self.timer_func = f | |
def start_window(self): | |
self.tkroot.mainloop() | |
def set_wave(self, wave, norm): | |
w_len = len(wave) | |
c_len = self.canvas_width | |
for i in range(0, c_len): | |
del self.wave[0] | |
del self.wave | |
self.wave = [] | |
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.append([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.append([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.append([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): # revised | |
if 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 | |
del new_wave | |
else: | |
self.cycle = 10 | |
self.timer_func = False | |
def timer(self): # added | |
if self.timer_func != False: | |
self.timer_func() | |
self.tkroot.after(50, self.timer) | |
if __name__ == '__main__': | |
osv = OscilloViewer() | |
osv.timer() # added | |
osv.start_window() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment