Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_random_colors.py
- from Tkinter import *
- from random import randint
- def color_to_hex(r,g,b):
- return '#%02x%02x%02x' % (r,g,b)
- def random_color():
- r = randint(0,255)
- g = randint(0,255)
- b = randint(0,255)
- return color_to_hex(r,g,b)
- def color_poll():
- global alarm
- #print
- c.itemconfig(r, fill=random_color())
- if keep_going:
- alarm = c.after(1000, color_poll)
- def change_color():
- global keep_going, alarm
- if not keep_going:
- keep_going = True
- b['text']='STOP';b['fg']='red';b['relief']=SUNKEN
- color_poll()
- else:
- keep_going = False; c.after_cancel(alarm); alarm = None
- b['text']='GO';b['fg']='green';b['relief']=RAISED
- def main(repetitions=2):
- hexagon = HexagonGenerator(40)
- image = create_canvas(hexagon.pattern_size, repetitions)
- draw = Draw(image)
- for row in range(hexagon.rows(image.size[1])):
- colors = [random_color() for _ in range(repetitions)]
- for column in range(repetitions + 1):
- color = colors[column % repetitions]
- draw.polygon(list(hexagon(row, column)), Brush(color))
- for column, color in enumerate(colors):
- draw.polygon(list(hexagon(-1, column)), Brush(color))
- draw.flush()
- image.show()
- root = Tk()
- c = Canvas(root)
- c['width'] = 400; c['height'] = 400
- r = c.create_rectangle(0,0,400,400)
- global keep_going, alarm
- keep_going = False; alarm = None
- b = Button(root, command=change_color)
- b['text'] = 'GO';b['fg']='green';b['font']='Arial 16';b['relief']=RAISED
- c.pack(); b.pack(); root.mainloop()
- color_poll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement