Sep-12-2017, 07:31 AM
Hi all,
A newbie to Python & Tkinter here, although have used VB6 & Bash scripts.
Included (hopefully) is my code for a simple sound organiser for use in
a amateur dramatics group I'm involved in.
Idea is start at sound 1, play it then enable sound 2 and disable sound 1
Extra buttons to allow to go back (lots of coding) or forward one sound,
(possible alternative solution) or stop the sound altogether.
Screen shows as required, (button labels to be modified at a later date,)
but pressing Btn1, the attached error happens.
I can't work out why!!!!
Note on draft screen all indents are correct, yet on preview screen all
the indentation is lost. (I Ctrl-C then Ctrl-V the code from my system)
###### CODE ################
A newbie to Python & Tkinter here, although have used VB6 & Bash scripts.
Included (hopefully) is my code for a simple sound organiser for use in
a amateur dramatics group I'm involved in.
Idea is start at sound 1, play it then enable sound 2 and disable sound 1
Extra buttons to allow to go back (lots of coding) or forward one sound,
(possible alternative solution) or stop the sound altogether.
Screen shows as required, (button labels to be modified at a later date,)
but pressing Btn1, the attached error happens.
I can't work out why!!!!
Note on draft screen all indents are correct, yet on preview screen all
the indentation is lost. (I Ctrl-C then Ctrl-V the code from my system)
###### CODE ################
#!/usr/bin/python3 from tkinter import * from tkinter import ttk from tkinter import font import os def window(main, tytle): # Creates a box positioned centrally main.title(tytle) main.update_idletasks wydth = ((main.winfo_screenwidth() // 10 ) * 9 ) #width of box = 9/10 screenwidth hite = ((main.winfo_screenheight() // 5 ) * 4) #height of box = 4/5 screenheight x = (main.winfo_screenwidth() // 2 ) - (wydth // 2 ) # x posn of box is 1/2 screen width minus 1/2 box width y = (main.winfo_screenheight() // 2 ) - ( hite // 2 ) # y posn of box = 1/2 screen height minus 1/2 box height main.geometry('{}x{}+{}+{}'.format(wydth, hite, x, y )) def btn_press(): print("it's working") if Btn1.cget('bg') == "yellow": print("1 is yellow") os.system('/usr/bin/mpg321 /home/john/Music/Sounds/sound1.mp3 &') Btn1.configure( bg = "green", state=DISABLED) Btn2.configure( bg = "yellow", state=NORMAL) elif Btn2.cget('bg') == "yellow": print("2 is yellow") os.system('/usr/bin/mpg321 /home/john/Music/Sounds/sound2.mp3 &') Btn2.configure( bg = "green", state=DISABLED) Btn3.configure( bg = "yellow", state=NORMAL) elif Btn3.cget('bg') == "yellow": print("3 is yellow") os.system('/usr/bin/mpg321 /home/john/Music/Sounds/sound3.mp3 &') Btn3.configure( bg = "green", state=DISABLED) Btn4.configure( bg = "yellow", state=NORMAL) elif Btn4.cget('bg') == "yellow": print("4 is yellow") os.system('/usr/bin/mpg321 /home/john/Music/Sounds/sound4.mp3 &') Btn4.configure( bg = "green", state=DISABLED) Btn5.configure( bg = "yellow", state=NORMAL) elif Btn5.cget('bg') == "yellow": print("5 is yellow") os.system('/usr/bin/mpg321 /home/john/Music/Sounds/sound5.mp3 &') Btn5.configure( bg = "green", state=DISABLED) Btn6.configure( bg = "yellow", state=NORMAL) else: print("6 is yellow") os.system('/usr/bin/mpg321 /home/john/Music/Sounds/sound6.mp3 &') Btn6.configure( bg = "green", state=DISABLED) Btn1.configure( bg = "yellow", state=NORMAL) def back_one(): print("Go back one button") stop_mp3() if Btn6.cget('bg') == "yellow": print("6 is yellow") Btn6.configure( bg = "green", state=DISABLED) Btn5.configure( bg = "yellow", state=NORMAL) elif Btn5.cget('bg') == "yellow": print("5 is yellow") Btn5.configure( bg = "green", state=DISABLED) Btn4.configure( bg = "yellow", state=NORMAL) elif Btn4.cget('bg') == "yellow": print("4 is yellow") Btn4.configure( bg = "green", state=DISABLED) Btn3.configure( bg = "yellow", state=NORMAL) elif Btn3.cget('bg') == "yellow": print("3 is yellow") Btn3.configure( bg = "green", state=DISABLED) Btn2.configure( bg = "yellow", state=NORMAL) elif Btn2.cget('bg') == "yellow": print("2 is yellow") Btn2.configure( bg = "green", state=DISABLED) Btn1.configure( bg = "yellow", state=NORMAL) elif Btn1.cget('bg') == "yellow": print("1 is yellow") Btn1.configure( bg = "green", state=DISABLED) Btn6.configure( bg = "yellow", state=NORMAL) def stop_mp3(): print("Stop the mp3") os.system('psnum=$(ps -aef | grep mpg321) ; psnum=$(echo ${psnum#*" "}) ; psnum=$(echo ${psnum%%" "*}) ; kill $psnum ') def fwd_one(): print("Go forward one") stop_mp3() list = ["Btn1.cget('bg')","Btn2.cget('bg')","Btn3.cget('bg')","Btn4.cget('bg')","Btn5.cget('bg')", "Btn6.cget('bg')"] for item in list: #print(item) if item == "yellow": print(item +" is it") break else: print(item) # root is the main window that surrounds the interface # This creates a Tk object root = Tk() helv36 = font.Font(family="Helvetica",size=36,weight="bold") helv14 = font.Font(family="Helvetica",size=14,weight="bold") def btn_creation(): list = ['Btn1','Btn2','Btn3','Btn4','Btn5','Btn6','Btn7','Btn8','Btn9'] rctr=0 cctr=0 stayt=NORMAL btncmd=btn_press bgcol="yellow" for item in list: item = Button(root, text=item, height=3, width=14, fg = "black" , bg = bgcol, activebackground = "red", font = helv36 , state=stayt, command=btncmd) item.grid(row=rctr, column=cctr) cctr=cctr+1 bgcol="green" if cctr == 3 : cctr=0 rctr=rctr+1 if rctr != 2: stayt=DISABLED if rctr == 2: bgcol="white" stayt=NORMAL if cctr == 0: btncmd=back_one if cctr == 1: btncmd=fwd_one bgcol="blue" # Call the above function window(root, "Sound maker") btn_creation() # This keeps the root window visible # and the program running root.mainloop()###### ERROR FROM CLI ##############
Error:john@john-HP:~$ Sound_mkr.py
it's working
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__
return self.func(*args)
File "/home/john/bin/Sound_mkr.py", line 25, in btn_press
if Btn1.cget('bg') == "yellow":
NameError: name 'Btn1' is not defined