Created
February 13, 2012 20:38
-
-
Save jayrambhia/1820074 to your computer and use it in GitHub Desktop.
A simple sticky note application using pygtk!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygtk | |
pygtk.require('2.0') | |
import gtk | |
import os | |
class TextBox: | |
def __init__(self): | |
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) | |
self.window.set_size_request(200,250) | |
self.window.connect("destroy", self.close_application) | |
self.window.set_title("sticky note") | |
self.window.set_border_width(4) | |
self.box1 = gtk.EventBox() | |
self.box1.connect('leave_notify_event',self.save_text) | |
self.window.add(self.box1) | |
self.box1.show() | |
self.textview = gtk.TextView() | |
self.textbuffer = self.textview.get_buffer() | |
self.textview.set_editable(True) | |
self.box1.add(self.textview) | |
self.textview.show() | |
if os.path.isfile(os.path.join(os.getcwd(),'stickynote.txt')): | |
infile = open('stickynote.txt','r') | |
if infile: | |
string = infile.read() | |
infile.close() | |
self.textbuffer.set_text(string) | |
self.window.show() | |
def close_application(self, widget): | |
file = open('stickynote.txt','w') | |
startiter = self.textbuffer.get_start_iter() | |
enditer = self.textbuffer.get_end_iter() | |
text = self.textbuffer.get_text(startiter, enditer) | |
file.write(text) | |
file.close() | |
gtk.main_quit() | |
def save_text(self, widget, data=None): | |
file = open('stickynote.txt','w') | |
startiter = self.textbuffer.get_start_iter() | |
enditer = self.textbuffer.get_end_iter() | |
text = self.textbuffer.get_text(startiter, enditer) | |
file.write(text) | |
file.close() | |
return | |
def main(): | |
gtk.main() | |
if __name__ == '__main__': | |
TextBox() | |
main() |
New feature added. Every time mouse pointer goes out of the window(box), text will get saved. This is because, if machine is shut down or logged out, the process is killed and text does not get stored.
I ported this to PyGObject with python three. In addition, I fixed the alternating tabs and spaces, added a shebang, and fixed a bug where using ^C wouldn't end the process on linux.
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import os
class TextBox:
def __init__(self):
self.window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
self.window.set_size_request(200,250)
self.window.connect("destroy", self.close_application)
self.window.set_title("sticky note")
self.window.set_border_width(4)
self.box1 = Gtk.EventBox()
self.box1.connect('leave_notify_event',self.save_text)
self.window.add(self.box1)
self.box1.show()
self.textview = Gtk.TextView()
self.textbuffer = self.textview.get_buffer()
self.textview.set_editable(True)
self.box1.add(self.textview)
self.textview.show()
if os.path.isfile(os.path.join(os.getcwd(),'stickynote.txt')):
infile = open('stickynote.txt','r')
if infile:
string = infile.read()
infile.close()
self.textbuffer.set_text(string)
self.window.show()
def close_application(self, widget):
file = open('stickynote.txt','w')
startiter = self.textbuffer.get_start_iter()
enditer = self.textbuffer.get_end_iter()
_ = 'This is a throwaway variable.'
text = self.textbuffer.get_text(startiter, enditer, _)
file.write(text)
file.close()
Gtk.main_quit()
def save_text(self, widget, data=None):
file = open('stickynote.txt','w')
startiter = self.textbuffer.get_start_iter()
enditer = self.textbuffer.get_end_iter()
_ = 'This is a throwaway variable.'
text = self.textbuffer.get_text(startiter, enditer, _)
file.write(text)
file.close()
return
def main():
Gtk.main()
if __name__ == '__main__':
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
TextBox()
main()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
save this in your home directory. And add following command to your .profile file
python stickynotes.py &
Don't forget to add "&"!
And whenever you log in, It would be there.