clEntry.py

00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # ----------------------------------------------------------------------------
00005 # pyjama - python jamendo audioplayer
00006 # Copyright (c) 2008 Daniel Nögel
00007 #
00008 # This program is free software: you can redistribute it and/or modify
00009 # it under the terms of the GNU General Public License as published by
00010 # the Free Software Foundation, either version 3 of the License, or
00011 # (at your option) any later version.
00012 #
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 # ----------------------------------------------------------------------------
00020 
00021 import sys
00022 import getopt
00023 import gtk
00024 
00025 ###################################################################
00026 #
00027 # an entry dialog - taken from:
00028 # http://www.rexx.com/~dkuhlman/python_201/python_201.html#SECTION008220000000000000000
00029 #
00030 class EntryDialog( gtk.Dialog):
00031     def __init__(self, message="", default_text='', modal= True):
00032         gtk.Dialog.__init__(self)
00033         self.connect("destroy", self.quit)
00034         self.connect("delete_event", self.quit)
00035         if modal:
00036             self.set_modal(True)
00037         box = gtk.VBox(spacing=10)
00038         box.set_border_width(10)
00039         self.vbox.pack_start(box)
00040         box.show()
00041         if message:
00042             label = gtk.Label(message)
00043             box.pack_start(label)
00044             label.show()
00045         self.entry = gtk.Entry()
00046         self.entry.set_text(default_text)
00047         self.entry.connect("activate", self.click)
00048         box.pack_start(self.entry)
00049         self.entry.show()
00050         self.entry.grab_focus()
00051         button = gtk.Button("OK")
00052         button.connect("clicked", self.click)
00053         button.set_flags(gtk.CAN_DEFAULT)
00054         self.action_area.pack_start(button)
00055         button.show()
00056         button.grab_default()
00057         button = gtk.Button("Cancel")
00058         button.connect("clicked", self.quit)
00059         button.set_flags(gtk.CAN_DEFAULT)
00060         self.action_area.pack_start(button)
00061         button.show()
00062         self.ret = None
00063     def quit(self, w=None, event=None):
00064         self.hide()
00065         self.destroy()
00066         gtk.main_quit()
00067     def click(self, button):
00068         self.ret = self.entry.get_text()
00069         self.quit()
00070 
00071 def input_box(title="Input Box", message="", default_text='',
00072         modal= True):
00073     win = EntryDialog(message, default_text, modal=modal)
00074     win.set_title(title)
00075     win.show()
00076     gtk.main()
00077     return win.ret
00078 
00079 def test():
00080     result = input_box(title='Test #2',
00081         message='Enter a valuexxx:',
00082         default_text='a default value')
00083     if result is None:
00084         print 'Canceled'
00085     else:
00086         print 'result: "%s"' % result
00087 
00088 USAGE_TEXT = """
00089 Usage:
00090     python simple_dialog.py [options]
00091 Options:
00092     -h, --help      Display this help message.
00093 Example:
00094     python simple_dialog.py
00095 """
00096 
00097 def usage():
00098     print USAGE_TEXT
00099     sys.exit(-1)
00100 
00101 def main():
00102     args = sys.argv[1:]
00103     try:
00104         opts, args = getopt.getopt(args, 'h', ['help'])
00105     except:
00106         usage()
00107     relink = 1
00108     for opt, val in opts:
00109         if opt in ('-h', '--help'):
00110             usage()
00111     if len(args) != 0:
00112         usage()
00113     test()
00114 
00115 if __name__ == '__main__':
00116     main()
00117     #import pdb
00118     #pdb.run('main()')
00119 

Generated on Thu Jun 4 19:08:24 2009 for Pyjama by  doxygen 1.5.8