Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Last active May 19, 2021 00:43

Revisions

  1. mariocesar revised this gist Oct 12, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion note.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    #!/usr/bin/python
    # encoding: utf-8
    # -*- coding: utf-8 -*-
    # -*- Mode: python; c-basic-offset: 4 -*-
    # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

    """
    Simple note taking desktop app (Python Gtk3)
  2. mariocesar revised this gist Oct 12, 2014. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion note.py
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,12 @@
    # encoding: utf-8

    """
    LICENSE:
    Simple note taking desktop app (Python Gtk3)
    @author: Mario César Señoranis Ayala <mariocesar@creat1va.com>
    @license: GPLv3
    Copyright (C) 2014 Mario César Señoranis Ayala <mariocesar@creat1va.com>
    This program or library is free software; you can redistribute it
  3. mariocesar revised this gist Oct 11, 2014. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions note.py
    Original file line number Diff line number Diff line change
    @@ -95,13 +95,12 @@ def __init__(self, application):
    self.grid.attach(scrolledwindow, 0, 1, 3, 1)

    def checkout_text_buffer(self):

    if not os.path.exists(self.application.notes_file_path):
    if os.path.exists(self.application.notes_file_path):
    with file(self.application.notes_file_path, 'r+') as f:
    self.textbuffer.set_text(f.read())
    else:
    file(self.application.notes_file_path, 'a').close()

    with file(self.application.notes_file_path, 'r+') as f:
    self.textbuffer.set_text(f.read())

    def commit_text_buffer(self):
    text = self.textbuffer.get_text(
    self.textbuffer.get_start_iter(),
  4. mariocesar revised this gist Oct 11, 2014. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions note.py
    Original file line number Diff line number Diff line change
    @@ -3,19 +3,19 @@

    """
    LICENSE:
    Copyright (C) 2014 Mario César Señoranis Ayala <mariocesar@creat1va.com>
    This program or library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    @@ -58,7 +58,6 @@ def __init__(self, application):


    # Signals

    self.restore_position()
    self.connect('delete_event', self.save_position)

    @@ -96,7 +95,11 @@ def __init__(self, application):
    self.grid.attach(scrolledwindow, 0, 1, 3, 1)

    def checkout_text_buffer(self):
    with file(self.application.notes_file_path, 'w+') as f:

    if not os.path.exists(self.application.notes_file_path):
    file(self.application.notes_file_path, 'a').close()

    with file(self.application.notes_file_path, 'r+') as f:
    self.textbuffer.set_text(f.read())

    def commit_text_buffer(self):
    @@ -158,4 +161,4 @@ def on_quit(self, widget, data):

    if __name__ == '__main__':
    application = Application()
    application.run(None)
    application.run(None)
  5. mariocesar revised this gist Oct 11, 2014. 1 changed file with 18 additions and 18 deletions.
    36 changes: 18 additions & 18 deletions note.py
    Original file line number Diff line number Diff line change
    @@ -3,23 +3,23 @@

    """
    LICENSE:
    Copyright (C) 2014 Mario César Señoranis Ayala <mariocesar@creat1va.com>
    This program or library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA.
    Copyright (C) 2014 Mario César Señoranis Ayala <mariocesar@creat1va.com>
    This program or library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA.
    """

    import os
    @@ -40,7 +40,6 @@
    background-color: #FFD700;
    color: #000;
    }
    """

    class NoteWindow(Gtk.Window):
    @@ -59,6 +58,7 @@ def __init__(self, application):


    # Signals

    self.restore_position()
    self.connect('delete_event', self.save_position)

  6. mariocesar revised this gist Oct 11, 2014. 1 changed file with 28 additions and 22 deletions.
    50 changes: 28 additions & 22 deletions note.py
    Original file line number Diff line number Diff line change
    @@ -3,27 +3,27 @@

    """
    LICENSE:
    Copyright (C) 2014 Mario César Señoranis Ayala <mariocesar@creat1va.com>
    This program or library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA.
    Copyright (C) 2014 Mario César Señoranis Ayala <mariocesar@creat1va.com>
    This program or library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA.
    """

    import os
    from gi.repository import Gtk, Gdk, Gio
    from gi.repository import Gtk, Gdk, Gio, GLib

    css = """
    #text_view, #note_window {
    @@ -66,7 +66,7 @@ def __init__(self, application):

    hb = Gtk.HeaderBar()
    hb.set_show_close_button(True)
    hb.props.title = "Tomar notas"
    hb.props.title = "Note"
    self.set_titlebar(hb)

    self.grid = Gtk.Grid()
    @@ -96,7 +96,7 @@ def __init__(self, application):
    self.grid.attach(scrolledwindow, 0, 1, 3, 1)

    def checkout_text_buffer(self):
    with file(os.path.expanduser('~/.notes'), 'r+') as f:
    with file(self.application.notes_file_path, 'w+') as f:
    self.textbuffer.set_text(f.read())

    def commit_text_buffer(self):
    @@ -105,7 +105,7 @@ def commit_text_buffer(self):
    self.textbuffer.get_end_iter(),
    False)

    with file(os.path.expanduser('~/.notes'), 'w+') as f:
    with file(self.application.notes_file_path, 'w+') as f:
    f.write(text)

    def restore_position(self):
    @@ -124,12 +124,19 @@ def on_text_buffer_change(self, widget):


    class Application(Gtk.Application):
    data_dir = os.path.join(GLib.get_user_data_dir(), 'notes')

    def do_activate(self):
    css_provider = Gtk.CssProvider()
    css_provider.load_from_data(css.encode('UTF-8'))
    screen = Gdk.Screen.get_default()

    self.settings = Gio.Settings("org.gnome.notes")
    self.notes_file_path = os.path.join(self.data_dir, 'notes.txt')

    if not os.path.exists(self.data_dir):
    os.makedirs(self.data_dir)

    self.window = NoteWindow(self)

    context = self.window.get_style_context()
    @@ -150,6 +157,5 @@ def on_quit(self, widget, data):
    self.quit()

    if __name__ == '__main__':
    Gdk.set_program_class("notes")
    application = Application()
    application.run(None)
  7. mariocesar revised this gist Oct 11, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions note.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    #!/usr/bin/python
    # encoding: utf-8

    """
    LICENSE:
  8. mariocesar revised this gist Oct 11, 2014. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions note.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,25 @@
    #!/usr/bin/python
    """
    LICENSE:
    Copyright (C) 2014 Mario César Señoranis Ayala <mariocesar@creat1va.com>
    This program or library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA.
    """

    import os
    from gi.repository import Gtk, Gdk, Gio

  9. mariocesar revised this gist Oct 11, 2014. 1 changed file with 35 additions and 8 deletions.
    43 changes: 35 additions & 8 deletions note.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/python
    import os
    from gi.repository import Gtk, Gdk
    from gi.repository import Gtk, Gdk, Gio

    css = """
    #text_view, #note_window {
    @@ -21,14 +21,25 @@
    """

    class NoteWindow(Gtk.Window):
    def __init__(self):
    def __init__(self, application):
    super(NoteWindow, self).__init__()
    self.application = application

    self.set_name('note_window')
    self.set_title('Tomar notas')
    self.set_title('Note')
    self.set_resizable(True)
    self.set_keep_above(False)
    self.set_keep_above(True)
    self.set_size_request(460, 500)
    self.set_border_width(10)
    self.set_gravity(Gdk.Gravity.SOUTH_EAST)
    self.set_type_hint(Gdk.WindowTypeHint.NORMAL)


    # Signals
    self.restore_position()
    self.connect('delete_event', self.save_position)

    # UI

    hb = Gtk.HeaderBar()
    hb.set_show_close_button(True)
    @@ -43,6 +54,8 @@ def __init__(self):

    self.add(self.grid)

    # Text View

    self.textview = Gtk.TextView()
    self.textview.set_name('text_view')
    self.textview.set_wrap_mode(Gtk.WrapMode.WORD)
    @@ -72,6 +85,17 @@ def commit_text_buffer(self):
    with file(os.path.expanduser('~/.notes'), 'w+') as f:
    f.write(text)

    def restore_position(self):
    try:
    x, y = self.application.settings['window-position']
    except ValueError:
    self.set_position(Gtk.WindowPosition.CENTER)
    else:
    self.move(x, y)

    def save_position(self, widget, event):
    self.application.settings['window-position'] = widget.get_position()

    def on_text_buffer_change(self, widget):
    self.commit_text_buffer()

    @@ -82,14 +106,16 @@ def do_activate(self):
    css_provider.load_from_data(css.encode('UTF-8'))
    screen = Gdk.Screen.get_default()

    window = NoteWindow()
    context = window.get_style_context()
    self.settings = Gio.Settings("org.gnome.notes")
    self.window = NoteWindow(self)

    context = self.window.get_style_context()
    context.add_provider_for_screen(
    screen, css_provider,
    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

    self.add_window(window)
    window.show_all()
    self.add_window(self.window)
    self.window.show_all()

    def do_startup(self):
    Gtk.Application.do_startup(self)
    @@ -101,5 +127,6 @@ def on_quit(self, widget, data):
    self.quit()

    if __name__ == '__main__':
    Gdk.set_program_class("notes")
    application = Application()
    application.run(None)
  10. mariocesar revised this gist Oct 11, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions org.gnome.notes.gschema.xml
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    <!---
    copy this file to /usr/share/glib-2.0/schemas/, then run `sudo glib-compile-schemas /usr/share/glib-2.0/schemas/`
    --->
    <schemalist>
    <schema id="org.gnome.notes" path="/org/gnome/notes/" gettext-domain="notes">
    <key name="window-position" type="ai">
  11. mariocesar revised this gist Oct 11, 2014. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions org.gnome.notes.gschema.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <schemalist>
    <schema id="org.gnome.notes" path="/org/gnome/notes/" gettext-domain="notes">
    <key name="window-position" type="ai">
    <default>[]</default>
    </key>
    </schema>
    </schemalist>
  12. mariocesar revised this gist Oct 11, 2014. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions note.desktop
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    ; Don't forget to change the Icon and Exec path to your needs
    ; And save this file into the ~/.local/share/applications/ directory
    [Desktop Entry]
    Version=1.0
    Type=Application
    Name=Notes
    Icon=/home/mariocesar/bin/notes.png
    Exec=/home/mariocesar/bin/notes.py
    Categories=Accessories;
    Terminal=false
  13. mariocesar revised this gist Oct 11, 2014. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions note.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/python
    import os
    from gi.repository import Gtk, Gdk, Pango
    from gi.repository import Gtk, Gdk

    css = """
    #text_view, #note_window {
    @@ -46,14 +46,11 @@ def __init__(self):
    self.textview = Gtk.TextView()
    self.textview.set_name('text_view')
    self.textview.set_wrap_mode(Gtk.WrapMode.WORD)
    #self.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(255, 255, 255, 1))

    self.textbuffer = self.textview.get_buffer()
    self.textbuffer.connect("changed", self.save_buffer_content)
    self.textbuffer.connect("changed", self.on_text_buffer_change)

    with file(os.path.expanduser('~/.notes'), 'r+') as f:
    text = f.read()
    self.textbuffer.set_text(text)
    self.checkout_text_buffer()

    scrolledwindow = Gtk.ScrolledWindow()
    scrolledwindow.set_hexpand(True)
    @@ -62,8 +59,11 @@ def __init__(self):

    self.grid.attach(scrolledwindow, 0, 1, 3, 1)

    def checkout_text_buffer(self):
    with file(os.path.expanduser('~/.notes'), 'r+') as f:
    self.textbuffer.set_text(f.read())

    def save_buffer_content(self, widget):
    def commit_text_buffer(self):
    text = self.textbuffer.get_text(
    self.textbuffer.get_start_iter(),
    self.textbuffer.get_end_iter(),
    @@ -72,6 +72,9 @@ def save_buffer_content(self, widget):
    with file(os.path.expanduser('~/.notes'), 'w+') as f:
    f.write(text)

    def on_text_buffer_change(self, widget):
    self.commit_text_buffer()


    class Application(Gtk.Application):
    def do_activate(self):
  14. mariocesar created this gist Oct 11, 2014.
    102 changes: 102 additions & 0 deletions note.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,102 @@
    #!/usr/bin/python
    import os
    from gi.repository import Gtk, Gdk, Pango

    css = """
    #text_view, #note_window {
    background-color: #FFFFE0;
    }
    #text_view {
    font-family: Liberation Mono;
    font-size: 9px;
    }
    #text_view:selected,
    #text_view:selected:focus {
    background-color: #FFD700;
    color: #000;
    }
    """

    class NoteWindow(Gtk.Window):
    def __init__(self):
    super(NoteWindow, self).__init__()
    self.set_name('note_window')
    self.set_title('Tomar notas')
    self.set_resizable(True)
    self.set_keep_above(False)
    self.set_size_request(460, 500)
    self.set_border_width(10)

    hb = Gtk.HeaderBar()
    hb.set_show_close_button(True)
    hb.props.title = "Tomar notas"
    self.set_titlebar(hb)

    self.grid = Gtk.Grid()
    self.grid.margin_left = 20
    self.grid.margin_right = 20
    self.grid.margin_top = 20
    self.grid.margin_bottom = 20

    self.add(self.grid)

    self.textview = Gtk.TextView()
    self.textview.set_name('text_view')
    self.textview.set_wrap_mode(Gtk.WrapMode.WORD)
    #self.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(255, 255, 255, 1))

    self.textbuffer = self.textview.get_buffer()
    self.textbuffer.connect("changed", self.save_buffer_content)

    with file(os.path.expanduser('~/.notes'), 'r+') as f:
    text = f.read()
    self.textbuffer.set_text(text)

    scrolledwindow = Gtk.ScrolledWindow()
    scrolledwindow.set_hexpand(True)
    scrolledwindow.set_vexpand(True)
    scrolledwindow.add(self.textview)

    self.grid.attach(scrolledwindow, 0, 1, 3, 1)


    def save_buffer_content(self, widget):
    text = self.textbuffer.get_text(
    self.textbuffer.get_start_iter(),
    self.textbuffer.get_end_iter(),
    False)

    with file(os.path.expanduser('~/.notes'), 'w+') as f:
    f.write(text)


    class Application(Gtk.Application):
    def do_activate(self):
    css_provider = Gtk.CssProvider()
    css_provider.load_from_data(css.encode('UTF-8'))
    screen = Gdk.Screen.get_default()

    window = NoteWindow()
    context = window.get_style_context()
    context.add_provider_for_screen(
    screen, css_provider,
    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

    self.add_window(window)
    window.show_all()

    def do_startup(self):
    Gtk.Application.do_startup(self)

    def do_shutdown(self):
    Gtk.Application.do_shutdown(self)

    def on_quit(self, widget, data):
    self.quit()

    if __name__ == '__main__':
    application = Application()
    application.run(None)