Advertisement
Guest User

Python Chord Transposer

a guest
Jan 11th, 2021
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.12 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import ttk as ttk
  3.  
  4. root = Tk()
  5. root.title("Chord Transposer")
  6.  
  7. # Code Book
  8. book1 = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
  9. book2 = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B', 'C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B']
  10.  
  11. chord_boxes = []
  12.  
  13. chord_row = 0
  14. n = 1
  15.  
  16. def cnumber_size(event):
  17.     # Grid Setiing
  18.     global cns
  19.     cns = event.widget.get()
  20.     for i in range(int(cns)):
  21.         chord_frame.grid_columnconfigure(i, weight=1)
  22.         chord_frame.grid_columnconfigure(i+int(cns), weight=0)
  23.  
  24. def sharpflat(event):
  25.     sf = event.widget.get()
  26.     for chord in chord_boxes:    
  27.         o_chord = globals()['{}'.format(chord)].get()
  28.        
  29.         # No Chord
  30.         if not o_chord:
  31.             continue
  32.  
  33.         # Slash Index
  34.         if o_chord.find('/') == -1:
  35.             slash_index = None
  36.             slash_chord = None
  37.         else:
  38.             slash_index = o_chord.find('/')
  39.  
  40.         # Slash Chord
  41.         if slash_index != None:
  42.             slash_chord = o_chord[slash_index+1:]
  43.        
  44.         # Chord Root
  45.         if len(o_chord) == 1:
  46.             chord_root = o_chord
  47.         elif o_chord[1] == '#' or o_chord[1] == 'b':
  48.             chord_root = o_chord[:2]
  49.         else:
  50.             chord_root = o_chord[0]
  51.  
  52.         # Tension
  53.         if slash_index is None:
  54.             tension = o_chord[o_chord.index(chord_root[-1])+1:]
  55.         else:
  56.             tension = o_chord[o_chord.index(chord_root[-1])+1:slash_index]
  57.        
  58.         # Check #/b
  59.         if sf == opt_sharpflat[0]: # ♭
  60.             if len(chord_root) == 1:
  61.                 pass
  62.             elif chord_root[1] == '#':
  63.                 chord_root = book2[book1.index(chord_root)]
  64.             elif chord_root[1] == 'b':
  65.                 pass
  66.  
  67.             if slash_chord is None:
  68.                 pass
  69.             elif len(slash_chord) == 1:
  70.                 pass
  71.             elif slash_chord[1] == '#':
  72.                 slash_chord = book2[book1.index(slash_chord)]
  73.             elif slash_chord[1] == 'b':
  74.                 pass
  75.        
  76.         elif sf == opt_sharpflat[1]: # #
  77.             if len(chord_root) == 1:
  78.                 pass
  79.             elif chord_root[1] == 'b':
  80.                 chord_root = book1[book2.index(chord_root)]
  81.             elif chord_root[1] == '#':
  82.                 pass
  83.  
  84.             if slash_chord is None:
  85.                 pass
  86.             elif len(slash_chord) == 1:
  87.                 pass
  88.             elif slash_chord[1] == 'b':
  89.                 slash_chord = book1[book2.index(slash_chord)]
  90.             elif slash_chord[1] == '#':
  91.                 pass
  92.  
  93.         # Convert Chord #/♭
  94.         if len(tension) == 0:
  95.             convert_chord = chord_root
  96.         else:
  97.             convert_chord = '{0}{1}'.format(chord_root, tension)
  98.        
  99.         if slash_index == None:
  100.             convert_chord = convert_chord
  101.         else:
  102.             convert_chord = '{0}{1}/{2}'.format(chord_root, tension, slash_chord)
  103.        
  104.         globals()['{}'.format(chord)].delete(0, END)
  105.         globals()['{}'.format(chord)].insert(0, (convert_chord))
  106.  
  107. def font_size(event):
  108.     fs = event.widget.get()
  109.     if fs == '작게':
  110.         for chord in chord_boxes:
  111.             globals()['{}'.format(chord)].configure(font='TkDefaultFont 9 bold')
  112.     elif fs == '보통':
  113.         for chord in chord_boxes:
  114.             globals()['{}'.format(chord)].configure(font='TkDefaultFont 12 bold')
  115.     elif fs == '크게':
  116.         for chord in chord_boxes:
  117.             globals()['{}'.format(chord)].configure(font='TkDefaultFont 14 bold')
  118.  
  119. def add_chord_box():
  120.     global chord_row, n
  121.    
  122.     # Add Chord Box
  123.     for x in range(int(cmb_cnumber.get())):
  124.        
  125.         # Font Size Setting
  126.         fs = cmb_font.get()
  127.         if fs == '작게':
  128.             fs = 9
  129.         elif fs == '보통':
  130.             fs = 12
  131.         elif fs == '크게':
  132.             fs = 14
  133.        
  134.         globals()['chord{}'.format(n)] = Entry(chord_frame, width=10, justify='center')
  135.         globals()['chord{}'.format(n)].grid(row=chord_row, column=x, sticky=W+E, padx=(2, 2), pady=(2, 2))
  136.         globals()['chord{}'.format(n)].configure(font='TkDefaultFont {} bold'.format(fs))
  137.         chord_boxes.append('chord{}'.format(n))
  138.         n += 1
  139.     chord_row += 1
  140.  
  141. def del_chord_box():
  142.     global n
  143.     for i in range(int(cmb_cnumber.get())):
  144.         if n <= 1:
  145.             break
  146.         n -= 1
  147.         globals()['chord{}'.format(n)].destroy()
  148.         chord_boxes.pop()
  149.    
  150. def transposeup():
  151.     for chord in chord_boxes:
  152.         o_chord = globals()['{}'.format(chord)].get()
  153.  
  154.         # No Chord
  155.         if not o_chord:
  156.             continue
  157.  
  158.         # Slash Index
  159.         if o_chord.find('/') == -1:
  160.             slash_index = None
  161.         else:
  162.             slash_index = o_chord.find('/')
  163.  
  164.         # Slash Chord
  165.         if slash_index != None:
  166.             slash_chord = o_chord[slash_index+1:]
  167.        
  168.         # Chord Root
  169.         if len(o_chord) == 1:
  170.             chord_root = o_chord
  171.         elif o_chord[1] == '#' or o_chord[1] == 'b':
  172.             chord_root = o_chord[:2]
  173.         else:
  174.             chord_root = o_chord[0]
  175.  
  176.         # Tension
  177.         if slash_index is None:
  178.             tension = o_chord[o_chord.index(chord_root[-1])+1:]
  179.         else:
  180.             tension = o_chord[o_chord.index(chord_root[-1])+1:slash_index]
  181.        
  182.         # Transpose Chord Root
  183.         if len(chord_root) == 1:
  184.             tp_up_root = book2[book2.index(chord_root) + 1]
  185.         elif chord_root[1] == '#':
  186.             tp_up_root = book1[book1.index(chord_root) + 1]
  187.         elif chord_root[1] == 'b':
  188.             tp_up_root = book2[book2.index(chord_root) + 1]
  189.         else:
  190.             tp_up_root = book2[book2.index(chord_root) + 1]
  191.  
  192.         # Transpose Slash Chord
  193.         if slash_index == None:
  194.             tp_up_slash = None
  195.         elif len(slash_chord) == 1:
  196.             tp_up_slash = book2[book2.index(slash_chord) + 1]
  197.         elif slash_chord[1] == '#':
  198.             tp_up_slash = book1[book1.index(slash_chord) + 1]
  199.         elif slash_chord[1] == 'b':
  200.             tp_up_slash = book2[book2.index(slash_chord) + 1]
  201.         else:
  202.             tp_up_slash = book2[book2.index(slash_chord) + 1]
  203.  
  204.         # Convert Chord #/♭
  205.         if cmb_sharpflat.get() == opt_sharpflat[0]: # ♭
  206.             if len(tp_up_root) == 1:
  207.                 pass
  208.             elif tp_up_root[1] == '#':
  209.                 tp_up_root = book2[book1.index(tp_up_root)]
  210.             elif tp_up_root[1] == 'b':
  211.                 pass
  212.  
  213.             if tp_up_slash is None:
  214.                 pass
  215.             elif len(tp_up_slash) == 1:
  216.                 pass
  217.             elif tp_up_slash[1] == '#':
  218.                 tp_up_slash = book2[book1.index(tp_up_slash)]
  219.             elif tp_up_slash[1] == 'b':
  220.                 pass
  221.        
  222.         elif cmb_sharpflat.get() == opt_sharpflat[1]:
  223.             if len(tp_up_root) == 1:
  224.                 pass
  225.             elif tp_up_root[1] == 'b':
  226.                 tp_up_root = book1[book2.index(tp_up_root)]
  227.             elif tp_up_root[1] == '#':
  228.                 pass
  229.  
  230.             if tp_up_slash is None:
  231.                 pass
  232.             elif len(tp_up_slash) == 1:
  233.                 pass
  234.             elif tp_up_slash[1] == 'b':
  235.                 tp_up_slash = book1[book2.index(tp_up_slash)]
  236.             elif tp_up_slash[1] == '#':
  237.                 pass
  238.  
  239.         # Transpose Chord
  240.         if len(tension) == 0:
  241.             tp_up_chord = tp_up_root
  242.         else:
  243.             tp_up_chord = '{0}{1}'.format(tp_up_root, tension)
  244.        
  245.         if slash_index == None:
  246.             tp_up_chord = tp_up_chord
  247.         else:
  248.             tp_up_chord = '{0}{1}/{2}'.format(tp_up_root, tension, tp_up_slash)
  249.        
  250.         globals()['{}'.format(chord)].delete(0, END)
  251.         globals()['{}'.format(chord)].insert(0, (tp_up_chord))
  252.  
  253. def transposedown():
  254.     for chord in chord_boxes:
  255.         o_chord = globals()['{}'.format(chord)].get()
  256.  
  257.         # No Chord
  258.         if not o_chord:
  259.             continue
  260.  
  261.         # Slash Index
  262.         if o_chord.find('/') == -1:
  263.             slash_index = None
  264.         else:
  265.             slash_index = o_chord.find('/')
  266.  
  267.         # Slash Chord
  268.         if slash_index != None:
  269.             slash_chord = o_chord[slash_index+1:]
  270.        
  271.         # Chord Root
  272.         if len(o_chord) == 1:
  273.             chord_root = o_chord
  274.         elif o_chord[1] == '#' or o_chord[1] == 'b':
  275.             chord_root = o_chord[:2]
  276.         else:
  277.             chord_root = o_chord[0]
  278.  
  279.         # Tension
  280.         if slash_index is None:
  281.             tension = o_chord[o_chord.index(chord_root[-1])+1:]
  282.         else:
  283.             tension = o_chord[o_chord.index(chord_root[-1])+1:slash_index]
  284.        
  285.         # Transpose Chord Root
  286.         if len(chord_root) == 1:
  287.             tp_down_root = book2[book2.index(chord_root) - 1]
  288.         elif chord_root[1] == '#':
  289.             tp_down_root = book1[book1.index(chord_root) - 1]
  290.         elif chord_root[1] == 'b':
  291.             tp_down_root = book2[book2.index(chord_root) - 1]
  292.         else:
  293.             tp_down_root = book2[book2.index(chord_root) - 1]
  294.  
  295.         # Transpose Slash Chord
  296.         if slash_index == None:
  297.             tp_down_slash = None
  298.         elif len(slash_chord) == 1:
  299.             tp_down_slash = book2[book2.index(slash_chord) - 1]
  300.         elif slash_chord[1] == '#':
  301.             tp_down_slash = book1[book1.index(slash_chord) - 1]
  302.         elif slash_chord[1] == 'b':
  303.             tp_down_slash = book2[book2.index(slash_chord) - 1]
  304.         else:
  305.             tp_down_slash = book2[book2.index(slash_chord) - 1]
  306.  
  307.         # Convert Chord #/♭
  308.         if cmb_sharpflat.get() == opt_sharpflat[0]: # b
  309.             if len(tp_down_root) == 1:
  310.                 pass
  311.             elif tp_down_root[1] == '#':
  312.                 tp_down_root = book2[book1.index(tp_down_root)]
  313.             elif tp_down_root[1] == 'b':
  314.                 pass
  315.  
  316.             if tp_down_slash is None:
  317.                 pass
  318.             elif len(tp_down_slash) == 1:
  319.                 pass
  320.             elif tp_down_slash[1] == '#':
  321.                 tp_down_slash = book2[book1.index(tp_down_slash)]
  322.             elif tp_down_slash[1] == 'b':
  323.                 pass
  324.        
  325.         elif cmb_sharpflat.get() == opt_sharpflat[1]:
  326.             if len(tp_down_root) == 1:
  327.                 pass
  328.             elif tp_down_root[1] == 'b':
  329.                 tp_down_root = book1[book2.index(tp_down_root)]
  330.             elif tp_down_root[1] == '#':
  331.                 pass
  332.  
  333.             if tp_down_slash is None:
  334.                 pass
  335.             elif len(tp_down_slash) == 1:
  336.                 pass
  337.             elif tp_down_slash[1] == 'b':
  338.                 tp_down_slash = book1[book2.index(tp_down_slash)]
  339.             elif tp_down_slash[1] == '#':
  340.                 pass
  341.  
  342.         # Transpose Chord
  343.         if len(tension) == 0:
  344.             tp_down_chord = tp_down_root
  345.         else:
  346.             tp_down_chord = '{0}{1}'.format(tp_down_root, tension)
  347.        
  348.         if slash_index == None:
  349.             tp_down_chord = tp_down_chord
  350.         else:
  351.             tp_down_chord = '{0}{1}/{2}'.format(tp_down_root, tension, tp_down_slash)
  352.  
  353.         globals()['{}'.format(chord)].delete(0, END)
  354.         globals()['{}'.format(chord)].insert(0, (tp_down_chord))
  355.  
  356. # Command Frame (Chord Entry Add/Delete)
  357. command_frame = Frame(root)
  358. command_frame.pack(fill='x', padx=5, pady=5)
  359.  
  360. btn_add_chord = Button(command_frame, padx=5, pady=5, width=15, text='코드 입력칸 추가', command=add_chord_box)
  361. btn_add_chord.pack(side = 'left')
  362.  
  363. btn_del_chord = Button(command_frame, padx=5, pady=5, width=15, text='코드 입력칸 제거', command=del_chord_box)
  364. btn_del_chord.pack(side = 'right')
  365.  
  366. # Option Frame
  367. option_frame = LabelFrame(root, text='표시 옵션')
  368. option_frame.pack(fill='both', padx=5, pady=5, ipady=5)
  369.  
  370. # 1. Code Number Option
  371. # Chord Number Label
  372. lbl_cnumber = Label(option_frame, text='한 줄 당 코드 수', width=14)
  373. lbl_cnumber.pack(side='left', fill='none', expand=True, pady=5)
  374.  
  375. # Chord Number Combo
  376. opt_cnumber = ['4', '8']
  377. cmb_cnumber = ttk.Combobox(option_frame, state='readonly', values=opt_cnumber, width=3, justify='center')
  378. cmb_cnumber.current(0)
  379. cmb_cnumber.pack(side='left', fill='none', expand=True, padx=5, pady=5)
  380. cmb_cnumber.bind("<<ComboboxSelected>>", cnumber_size)
  381.  
  382. # 2. ♭/# Option
  383. # ♭/# Label
  384. lbl_sharpflat = Label(option_frame, text='♭ / # 변환', width=8)
  385. lbl_sharpflat.pack(side='left', fill='none', expand=True, padx=5, pady=5)
  386.  
  387. # ♭/# Combo
  388. opt_sharpflat = ['♭', '#']
  389. cmb_sharpflat = ttk.Combobox(option_frame, state='readonly', values=opt_sharpflat, width=3, justify='center')
  390. cmb_sharpflat.current(0)
  391. cmb_sharpflat.pack(side='left', fill='none', expand=True, padx=5, pady=5)
  392. cmb_sharpflat.bind("<<ComboboxSelected>>", sharpflat)
  393.  
  394. # 3. Font Size Option
  395. # Font Size Label
  396. lbl_font = Label(option_frame, text='글자 크기', width=8)
  397. lbl_font.pack(side='left', fill='none', expand=True, padx=5, pady=5)
  398.  
  399. # Font Size Combo
  400. opt_font = ['작게', '보통', '크게']
  401. cmb_font = ttk.Combobox(option_frame, state='readonly', values=opt_font, width=5, justify='center')
  402. cmb_font.current(1)
  403. cmb_font.pack(side='left', fill='none', expand=True, padx=5, pady=5)
  404. cmb_font.bind("<<ComboboxSelected>>", font_size)
  405.  
  406. # Chord Frame
  407. chord_frame = LabelFrame(root, text='코드 입력칸')
  408. chord_frame.pack(fill='both', expand=True, padx=5, pady=5)
  409.  
  410. # Default Grid Setting
  411. for i in range(4):
  412.     chord_frame.grid_columnconfigure(i, weight=1)
  413.  
  414. # Transpose Frame
  415. transpose_frame = LabelFrame(root, text='Transpose')
  416. transpose_frame.pack(fill='x', padx=5, pady=5, ipady=5)
  417.  
  418. btn_transpose_up = Button(transpose_frame, padx=5, pady=5, text='UP ↑', width=15, command=transposeup)
  419. btn_transpose_up.pack(side='left', fill='x', padx=5, pady=5)
  420.  
  421. btn_transpose_down = Button(transpose_frame, padx=5, pady=5, text='DOWN ↓', width=15, command=transposedown)
  422. btn_transpose_down.pack(side='right', fill='x', padx=5, pady=5)
  423.  
  424. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement