Skip to content

Instantly share code, notes, and snippets.

@setzer22
Last active December 27, 2015 15:30
Show Gist options
  • Save setzer22/3c9ade86d083a1a6931a to your computer and use it in GitHub Desktop.
Save setzer22/3c9ade86d083a1a6931a to your computer and use it in GitHub Desktop.
A simple script that displays a GTK window asking for values to the provided list of keys. Usage: pygtkeyvals Title [Keys]
#!/usr/bin/python2
#The MIT License (MIT)
#Copyright (c) 2015 setzer22
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
#associated documentation files (the "Software"), to deal in the Software without restriction, including
#without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
#following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial
#portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
#LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import pygtk
pygtk.require('2.0')
import gtk
class PyGTKeyVal:
def delete_event(self, widget, event, data=None):
print "delete event occurred"
return False
def destroy(self, widget, data=None):
print "destroy signal occurred"
gtk.main_quit()
def ok(self, widget, data=None):
for label in self.buttons:
print label.get_text()
gtk.main_quit()
def cancel(sef, widget, data=None):
print "CANCEL"
gtk.main_quit()
def __init__(self, title, labels):
self.title = title
self.labels = labels
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("delete_event", self.delete_event)
self.window.connect("destroy", self.destroy)
self.window.set_border_width(10)
self.box = gtk.VBox(False, 0)
self.window.add(self.box)
self.title_label = gtk.Label()
self.title_label.set_use_markup(gtk.TRUE)
self.title_label.set_markup('<span size="20000">'+self.title+'</span>')
self.title_label.set_alignment(0, 0.5)
self.title_label.show()
self.box.pack_start(self.title_label, False, False, 5)
self.form_box = gtk.HBox(False, 0)
self.labels_box = gtk.VBox(False, 0)
self.entries_box = gtk.VBox(False, 0)
self.buttons = []
for i in range(0,len(self.labels)):
label = gtk.Label(self.labels[i])
label.set_alignment(0,0.5)
self.labels_box.pack_start(label, True, True, 5)
self.buttons.append(gtk.Entry())
self.entries_box.pack_start(self.buttons[i], True, True, 5)
self.buttons[i].show()
label.show()
self.cancel_button = gtk.Button("Cancel")
self.cancel_button.connect("clicked", self.cancel, None)
self.cancel_button.show()
self.ok_button = gtk.Button("Ok")
self.ok_button.connect("clicked", self.ok, None)
self.ok_button.show()
self.buttons_box = gtk.HBox(False, 0)
self.buttons_box.pack_start(self.cancel_button, True, True, 5)
self.buttons_box.pack_start(self.ok_button, True, True, 5)
self.buttons_box.show()
self.labels_box.show()
self.entries_box.show()
self.form_box.show()
self.form_box.pack_start(self.labels_box, True, True, 5)
self.form_box.pack_start(self.entries_box, True, True, 5)
self.box.pack_start(self.form_box, True, True, 5)
self.box.pack_start(self.buttons_box, True, True, 5)
self.window.set_default(self.ok_button)
self.box.show()
self.window.show()
def main(self):
gtk.main()
if __name__ == "__main__":
import sys
title = sys.argv[1]
labels = sys.argv[2:]
pygtkeyval = PyGTKeyVal(title, labels)
pygtkeyval.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment