From 52a0b9808001b4e84ea8640af510b9a03df50098 Mon Sep 17 00:00:00 2001 From: khamidou Date: Thu, 11 Aug 2011 16:09:06 +0200 Subject: [PATCH] added a chord display functionality --- app.py | 58 ++++++++++++++++++++++++++++++++++++++++++------------- scales.py | 2 +- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/app.py b/app.py index b14bf2b..11775f2 100755 --- a/app.py +++ b/app.py @@ -17,23 +17,29 @@ class ScalesFrame(Frame): def handler(): global current_scale, current_mode current_scale = text - print "current scale %s, current mode %s" % (current_scale, current_mode) - s = Scale(scale=current_scale, mode=current_mode) - self.app.outputLabel.configure(text=str(s)) return handler def createWidgets(self): + self.v = IntVar() for i, button in enumerate(scales): - self.button = Radiobutton(self, text=str(button), command=self.closure(str(button))) + self.button = Radiobutton(self, text=str(button), command=self.closure(str(button)), + variable=self.v, value=i) self.button.deselect() self.button.grid(row=0, column=i) class ModeFrame(Frame): + scales = [ {"name": "Major", "formula" : [2, 2, 1, 2, 2, 2, 1], "chords": "I ii iii IV V vidim vii"}, + {"name": "Harmonic Minor", "formula" : [2, 1, 2, 2, 1, 3, 1]}, + {"name": "Natural Minor", "formula" : [2, 1, 2, 2, 1, 2, 2]}, + ] + def __init__(self, master=None, app=None): Frame.__init__(self, master) self.app = app self.grid() - self.createWidgets() + self.createWidgets(master) + self.currentSelec = None + self.poll() def closure(self, text): def handler(): @@ -45,12 +51,28 @@ class ModeFrame(Frame): return handler - def createWidgets(self): - for i, button in enumerate(modes): - self.button = Radiobutton(self, text=str(button), command=self.closure(str(button))) - self.button.deselect() - self.button.grid(row=0, column=i) + def createWidgets(self, master): + self.listbox = Listbox(master) + for scale in self.scales: + self.listbox.insert(END, scale["name"]) + self.listbox.grid() + + def poll(self): + # necessary because of a limitation + # of tkinter + global current_scale # ok, crappy design. + now = self.listbox.curselection() + #if now != self.currentSelec and now != (): + if now != (): + self.app.updateWidgets(self.scales[int(now[0])], current_scale) + self.currentSelec = now + self.after(250, self.poll) + + def update(self, currentSelection): + # notify the parent class of the change + # in the list + return class Application(Frame): def __init__(self, master=None): @@ -59,21 +81,31 @@ class Application(Frame): self.createWidgets() def createWidgets(self): - self.outputLabel = Label(self, text="Placeholder") + self.outputLabel = Label(self, text="Select a scale and a mode") - self.scalesLabel = LabelFrame(self, text="Scale") + self.scalesLabel = LabelFrame(self, text="Root note") self.scalesLabel.grid() self.scalesFrame = ScalesFrame(master=self.scalesLabel, app=self) - self.modesLabel = LabelFrame(self, text="Modes") + self.modesLabel = LabelFrame(self, text="Scale") self.modesLabel.grid() self.modesFrame = ModeFrame(master=self.modesLabel, app=self) + self.derivedChordsFrame = LabelFrame(self, text="Derived Chords") + self.derivedChordsFrame.grid() + self.derivedChordsLabel = Label(master=self.derivedChordsFrame, text="ho") + self.derivedChordsLabel.pack() self.outputLabel.grid() self.quitButton = Button(self, text="Quit", command=self.quit) self.quitButton.grid() + + def updateWidgets(self, currentListSelection, currentNote): + s = Scale(currentNote, currentListSelection["formula"]) + self.outputLabel.configure(text=str(s)) + if "chords" in currentListSelection: + self.derivedChordsLabel.configure(text=currentListSelection["chords"]) if __name__ == "__main__": root = Tk() diff --git a/scales.py b/scales.py index 9ee7a24..bdb70e4 100644 --- a/scales.py +++ b/scales.py @@ -83,7 +83,7 @@ class Scale: i = 0 j = 0 while i < 11 and j < 6: # reduce(lambda x,y: x+y, self.values) - n = self.formula[i] + n = self.formula[j] retscale += notes[i+n] i += n j += 1 # Don't forget to increment j to avoid looping. -- 2.43.0