Informatik10/tk_first_steps/toene_mvc.py
2021-11-28 23:33:37 +01:00

87 lines
1.8 KiB
Python

import tkinter as tk
from gensound import Sine
# Konstanten
c5= 523
b = 494
a = 440
g = 392
f = 349
e = 330
d = 294
c = 262
# ------------------------------------
# Melodie / Model
class Melody():
def __init__(self, melody = "c,e,g,c5,g,e,c"):
self.melody = melody
def play(self, duration = 0.5, amplitude = 0.5):
dur = duration * 1000
ampl = amplitude
s = Sine(frequency=440, duration=1)
tone_list = self.melody.split(',')
for tone in tone_list:
if tone == "c5":
s = s | Sine(frequency=c5, duration=dur)*0.5
elif tone == "b":
s = s | Sine(frequency=b, duration=dur)*0.5
elif tone == "a":
s = s | Sine(frequency=a, duration=dur)*0.5
elif tone == "g":
s = s | Sine(frequency=g, duration=dur)*0.5
elif tone == "f":
s = s | Sine(frequency=f, duration=dur)*0.5
elif tone == "e":
s = s | Sine(frequency=e, duration=dur)*0.5
elif tone == "d":
s = s | Sine(frequency=d, duration=dur)*0.5
elif tone == "c":
s = s | Sine(frequency=c, duration=dur)*0.5
else:
pass
s.play()
return True
# Controller
class Controller():
def __init__(self):
self.m = Melody()
def ton(self):
m = self.m
m.play()
# View
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
self.b1 = tk.Button(self)
self.b1["text"] = "Ton"
self.b1["command"] = con.ton
self.b1.pack(side="top")
self.quit = tk.Button(self, text="QUIT", fg="red",
command=root.destroy)
self.quit.pack(side="bottom")
# --- Ausführung ---
root = tk.Tk()
con = Controller()
app = Application(master=root)
app.mainloop()