Advertisement
here2share

# Tk_random_colors.py

Aug 8th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. # Tk_random_colors.py
  2.  
  3. from Tkinter import *
  4. from random import randint
  5.  
  6. def color_to_hex(r,g,b):
  7.         return '#%02x%02x%02x' % (r,g,b)
  8.  
  9. def random_color():
  10.         r = randint(0,255)
  11.         g = randint(0,255)
  12.         b = randint(0,255)
  13.         return color_to_hex(r,g,b)
  14.  
  15. def color_poll():
  16.     global alarm
  17.     #print
  18.     c.itemconfig(r, fill=random_color())
  19.     if keep_going:
  20.         alarm = c.after(1000, color_poll)
  21.  
  22. def change_color():
  23.     global keep_going, alarm
  24.     if not keep_going:
  25.         keep_going = True
  26.         b['text']='STOP';b['fg']='red';b['relief']=SUNKEN
  27.         color_poll()
  28.     else:
  29.         keep_going = False; c.after_cancel(alarm); alarm = None
  30.         b['text']='GO';b['fg']='green';b['relief']=RAISED
  31. def main(repetitions=2):
  32.     hexagon = HexagonGenerator(40)
  33.     image = create_canvas(hexagon.pattern_size, repetitions)
  34.     draw = Draw(image)
  35.     for row in range(hexagon.rows(image.size[1])):
  36.         colors = [random_color() for _ in range(repetitions)]
  37.         for column in range(repetitions + 1):
  38.             color = colors[column % repetitions]
  39.             draw.polygon(list(hexagon(row, column)), Brush(color))
  40.     for column, color in enumerate(colors):
  41.         draw.polygon(list(hexagon(-1, column)), Brush(color))
  42.     draw.flush()
  43.     image.show()
  44.  
  45. root = Tk()
  46. c = Canvas(root)
  47. c['width'] = 400; c['height'] = 400
  48. r = c.create_rectangle(0,0,400,400)
  49. global keep_going, alarm
  50. keep_going = False; alarm = None
  51. b = Button(root, command=change_color)
  52. b['text'] = 'GO';b['fg']='green';b['font']='Arial 16';b['relief']=RAISED
  53. c.pack(); b.pack(); root.mainloop()
  54. color_poll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement