# coding=UTF-8
import pygtk
import threading
pygtk.require('2.0')
import gtk
import gtk.glade
import os
import time
import pygame
import inspect
from WinBtnSetup import *
class Grid_Button(gtk.Button):
def __init__(self,parent_grid,img_path=None,text=None,font=None,color=None,mode=None,synth_line=None,sound=None,grid=None):
super(Grid_Button, self).__init__() # initializing gtk.button
self.show()
self.set_extension_events(gtk.gdk.EXTENSION_EVENTS_ALL)
# USABLE VARIABLES
self.image_path=img_path
self.text=text
self.synth_line=synth_line
self.font=font
self.font_color=color
self.sound=sound
self.grid_link=grid
self.parent_grid=parent_grid
if mode ==None:
self.mode="SYNTH"
else:
self.mode=mode
self.t=None
self.width=100
self.height=100
# ~~~~~~~~~~~~~~~~
self.box = gtk.VBox(False,2)
self.box.show()
self.add(self.box)
if self.image_path!=None:
pixbuf = gtk.gdk.pixbuf_new_from_file(self.image_path)
pixbuf = pixbuf.scale_simple(self.width, self.height, gtk.gdk.INTERP_BILINEAR)
self.image = gtk.image_new_from_pixbuf(pixbuf)
self.image.show()
else:
self.image=gtk.Image()
self.box.pack_start(self.image,False,False)
self.label=gtk.Label()
if self.text!=None:
text=''+self.text + ""
else:
text='Configure me'
self.label.set_markup(text)
self.label.show()
self.box.pack_start(self.label,False,False)
# -----------------------------------------------------------------------------------------------------------------------
self.connect("button_press_event",self.button_clicked)
self.playmode="mplayer"
#self.set_events()
#self.set_image(image)
#def __init__
def set_values(self,path,text,font,color,synth_line,sound,grid_link,mode):
self.image_path=path
self.text=text
self.synth_line=synth_line
self.font=font
self.font_color=color
self.sound=sound
self.grid_link=grid_link
self.mode=mode
# print grid_link
if self.text!=None:
text=''+self.text + ""
else:
text='Configure me'
# self.label=gtk.Label()
self.label.set_markup(text)
pixbuf = gtk.gdk.pixbuf_new_from_file(self.image_path)
pixbuf = pixbuf.scale_simple(self.width, self.height, gtk.gdk.INTERP_BILINEAR)
self.image.set_from_pixbuf(pixbuf)
#def set_values
def destroy(self, widget, data=None):
gtk.main_quit()
#def destroy
def print_variables(self,data=None):
print ""
print("# ID: %s"%self.parent_grid.id)
print ("# Image Path: %s"%self.image_path)
print("# Text: %s"%self.text)
print("# Synth line: %s"%self.synth_line)
file=open("/tmp/voice.txt","wb")
file.write(self.synth_line)
file.close()
if self.mode=="SYNTH":
resultado = os.system('festival --tts /tmp/voice.txt --language spanish')
os.remove("/tmp/voice.txt")
if self.mode=="MEDIA":
if self.t==None:
self.t = threading.Thread(target=self.play_sound, args=())
self.t.start()
if self.mode=="OPEN":
self.parent_grid.add_grid(self.grid_link,self.parent_grid.id)
if self.mode=="GOBACK":
self.parent_grid.core.winmain.label_manager.set_current_page(self.parent_grid.parent_grid)
#def print_variables
def play_sound(self):
if self.playmode=="pygame_wav":
sound = pygame.mixer.Sound(self.sound)
clock = pygame.time.Clock()
sound.play()
while pygame.mixer.get_busy():
clock.tick(60)
if self.playmode=="pygame_mp3":
clock = pygame.time.Clock()
pygame.mixer.music.load(self.sound)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
clock.tick(60)
if self.playmode=="mplayer":
resultado = os.system('mplayer %s'%self.sound)
self.t=None
def button_clicked(self,widget, event):
if event.button==1:
print event.device.name
if event.button == 3:
print "Configuration Window"
# print "%s %s"%(self.parent_grid.id,self.parent_grid.is_an_opened_file)
winbtnsetup=WinBtnSetup(self)
winbtnsetup.txtName.set_text(self.text)
winbtnsetup.txtSpeech.set_text(self.synth_line)
winbtnsetup.btnfont.set_font_name(self.font)
if self.image_path != None:
winbtnsetup.chsimage.set_filename(self.image_path)
winbtnsetup.btncolor.set_color(gtk.gdk.color_parse(self.font_color))
if self.sound != None:
winbtnsetup.chsmedia.set_filename(self.sound)
if self.grid_link != None:
winbtnsetup.chsopengrid.set_filename(self.grid_link)
if self.mode=="MEDIA":
winbtnsetup.rdbMedia.set_active(True)
if self.mode=="OPEN":
winbtnsetup.rdbOpenGrid.set_active(True)
if self.mode=="GOBACK":
winbtnsetup.rdbCloseGrid.set_active(True)
#self.imgpreview=self.image
#def button_clicked
#class grid_button
'''if __name__=="__main__":
print ">> DEBUG <<"
button_list=[]
table=gtk.Table(3,3,True)
table.show()
temp=0
for item in range(9):
button=Grid_Button()
button.set_label("%s"%(item+1))
button.connect("clicked",button.print_variables)
button.show()
if item==0 or item==1 or item==2:
table.attach(button,item,item+1,0,1)
if item==3 or item==4 or item==5:
table.attach(button,item-3,item-3+1,1,2)
if item==6 or item==7 or item==8:
table.attach(button,item-6,item-6+1,2,3)
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(400, 100)
window.connect("destroy", button.destroy)
window.set_border_width(10)
window.show()
window.add(table)
gtk.main()'''