Skip to content

Instantly share code, notes, and snippets.

@christophlsa
Created November 21, 2013 23:08
Show Gist options
  • Save christophlsa/7591531 to your computer and use it in GitHub Desktop.
Save christophlsa/7591531 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from gi.repository import Gtk, GLib, Pango
from time import sleep
import threading
import random
class PyRandomWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Random Select")
self.set_position(Gtk.WindowPosition.CENTER)
self.set_default_size(800, 600)
self.box = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL)
self.add(self.box)
# button for starting random selection
self.select_button = Gtk.Button()
label = Gtk.Label()
label.set_markup('<big><b>ZUFALL JETZT!!11</b></big>')
self.select_button.add(label)
self.select_button.connect("clicked", self.on_select_button_clicked)
self.box.pack_start(self.select_button, False, False, 0)
# list with one column with an editable text
self.names_store = Gtk.ListStore(str)
self.names_tree = Gtk.TreeView(self.names_store)
self.names_renderer = Gtk.CellRendererText(editable=True, font="Sans Regular 24", xalign=0.5, alignment=Pango.Alignment.CENTER)
self.names_renderer.connect("edited", self.on_cell_edited)
self.names_tree.append_column(Gtk.TreeViewColumn("Name", self.names_renderer, text=0))
self.box.pack_start(self.names_tree, True, True, 0)
self.listbutton_box = Gtk.Box(spacing=6)
self.box.pack_start(self.listbutton_box, False, False, 0)
# button for adding new list entry
self.add_button = Gtk.Button(label="+")
self.add_button.connect("clicked", self.on_add_button_clicked)
self.listbutton_box.pack_start(self.add_button, True, True, 0)
# button for removing selected list entry
self.del_button = Gtk.Button(label="-")
self.del_button.connect("clicked", self.on_del_button_clicked)
self.listbutton_box.pack_start(self.del_button, True, True, 0)
def on_add_button_clicked(self, widget):
self.names_store.append(['Name'])
def on_del_button_clicked(self, widget):
model, treeiter = self.names_tree.get_selection().get_selected()
if treeiter != None:
self.names_store.remove(treeiter)
def on_select_button_clicked(self, widget):
threading.Thread(target=self.select_random).start()
def select_random(self):
iterations = random.randint(20, 50) # how many items in list will be selected
speed = 0.05 # seconds to wait untill next selection (will be increased each step)
selection = self.names_tree.get_selection()
selection.unselect_all() # unselect all
iter = self.names_store.get_iter_first()
if iter == None: # if no items
return
for i in range(0, iterations): # go over all list items
if iter == None:
iter = self.names_store.get_iter_first() # if last item start from beginning
selection.select_iter(iter) # select and wait
sleep(speed)
speed += 0.01
iter = self.names_store.iter_next(iter)
def on_cell_edited(self, widget, path, text):
self.names_store[path][0] = text
win = PyRandomWindow()
GLib.threads_init()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment