Somewhat fancy voice command recognition software
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

gui.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # This is part of Kaylee
  2. # -- this code is licensed GPLv3
  3. # Copyright 2015-2017 Clayton G. Hobbs
  4. # Portions Copyright 2013 Jezra
  5. import sys
  6. import gi
  7. from gi.repository import GObject
  8. # Gtk
  9. gi.require_version('Gtk', '3.0')
  10. from gi.repository import Gtk, Gdk
  11. class GTKInterface(GObject.GObject):
  12. __gsignals__ = {
  13. 'command': (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
  14. (GObject.TYPE_STRING,))
  15. }
  16. def __init__(self, args, continuous):
  17. GObject.GObject.__init__(self)
  18. self.continuous = continuous
  19. # Make a window
  20. self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
  21. self.window.connect("delete_event", self.delete_event)
  22. # Give the window a name
  23. self.window.set_title("Kaylee")
  24. self.window.set_resizable(False)
  25. layout = Gtk.VBox()
  26. self.window.add(layout)
  27. # Make a listen/stop button
  28. self.lsbutton = Gtk.Button("Listen")
  29. layout.add(self.lsbutton)
  30. # Make a continuous button
  31. self.ccheckbox = Gtk.CheckButton("Continuous Listen")
  32. layout.add(self.ccheckbox)
  33. # Connect the buttons
  34. self.lsbutton.connect("clicked", self.lsbutton_clicked)
  35. self.ccheckbox.connect("clicked", self.ccheckbox_clicked)
  36. # Add a label to the UI to display the last command
  37. self.label = Gtk.Label()
  38. layout.add(self.label)
  39. # Create an accellerator group for this window
  40. accel = Gtk.AccelGroup()
  41. # Add the ctrl+q to quit
  42. accel.connect(Gdk.keyval_from_name('q'), Gdk.ModifierType.CONTROL_MASK,
  43. Gtk.AccelFlags.VISIBLE, self.accel_quit)
  44. # Lock the group
  45. accel.lock()
  46. # Add the group to the window
  47. self.window.add_accel_group(accel)
  48. def ccheckbox_clicked(self, widget):
  49. checked = self.ccheckbox.get_active()
  50. self.lsbutton.set_sensitive(not checked)
  51. if checked:
  52. self.lsbutton_stopped()
  53. self.emit('command', "continuous_listen")
  54. self.set_icon_active()
  55. else:
  56. self.emit('command', "stop")
  57. self.set_icon_inactive()
  58. def lsbutton_stopped(self):
  59. self.lsbutton.set_label("Listen")
  60. def lsbutton_clicked(self, button):
  61. val = self.lsbutton.get_label()
  62. if val == "Listen":
  63. self.emit("command", "listen")
  64. self.lsbutton.set_label("Stop")
  65. # Clear the label
  66. self.label.set_text("")
  67. self.set_icon_active()
  68. else:
  69. self.lsbutton_stopped()
  70. self.emit("command", "stop")
  71. self.set_icon_inactive()
  72. def run(self):
  73. # Set the default icon
  74. self.set_icon_inactive()
  75. self.window.show_all()
  76. if self.continuous:
  77. self.set_icon_active()
  78. self.ccheckbox.set_active(True)
  79. def accel_quit(self, accel_group, acceleratable, keyval, modifier):
  80. self.emit("command", "quit")
  81. def delete_event(self, x, y):
  82. self.emit("command", "quit")
  83. def finished(self, text):
  84. # If the continuous isn't pressed
  85. if not self.ccheckbox.get_active():
  86. self.lsbutton_stopped()
  87. self.set_icon_inactive()
  88. self.label.set_text(text)
  89. def set_icon_active_asset(self, i):
  90. self.icon_active = i
  91. def set_icon_inactive_asset(self, i):
  92. self.icon_inactive = i
  93. def set_icon_active(self):
  94. Gtk.Window.set_default_icon_from_file(self.icon_active)
  95. def set_icon_inactive(self):
  96. Gtk.Window.set_default_icon_from_file(self.icon_inactive)