Skip to content

Instantly share code, notes, and snippets.

@samclane
Last active May 30, 2018 18:48
Show Gist options
  • Save samclane/5f983f23cfc4d40b9930343c1a73b2e3 to your computer and use it in GitHub Desktop.
Save samclane/5f983f23cfc4d40b9930343c1a73b2e3 to your computer and use it in GitHub Desktop.
My nice canvas for drawing bulb icons manually.
class BulbIconList(Frame):
def __init__(self, *args):
self.window_width = 285
self.icon_width = 50
self.icon_height = 75
super().__init__(*args, width=self.window_width, height=self.icon_height)
self.pad = 5
self.scrollx = 0
self.scrolly = 0
self.bulb_dict = {}
self.canvas = Canvas(self, width=self.window_width, height=self.icon_height,
scrollregion=(0, 0, self.scrollx, self.scrolly))
hbar = Scrollbar(self, orient=HORIZONTAL)
hbar.pack(side=BOTTOM, fill=X)
hbar.config(command=self.canvas.xview)
self.canvas.config(width=self.window_width, height=self.icon_height)
self.canvas.config(xscrollcommand=hbar.set)
self.canvas.pack(side=LEFT, expand=True, fill=BOTH)
self.current_icon_width = 0
def draw_bulb_icon(self, bulb):
# Get label
label = bulb.get_label()
# Make room on canvas
self.scrollx += self.icon_width
self.canvas.configure(scrollregion=(0, 0, self.scrollx, self.scrolly))
# Build icon
rect = self.canvas.create_rectangle(self.current_icon_width + (self.icon_width / 4) + self.pad,
self.icon_height / 2 + self.pad,
self.current_icon_width + (3 * self.icon_width / 4) - self.pad,
self.icon_height / 2 - self.pad, fill='grey',
width=0, tags=[label])
circle = self.canvas.create_oval(self.current_icon_width + self.pad, self.pad,
self.current_icon_width + self.icon_width - self.pad,
self.icon_height / 2 - self.pad, outline='black', fill='black', width=3,
tags=[label])
oval = self.canvas.create_arc(self.current_icon_width + self.pad, self.pad,
self.current_icon_width + self.icon_width - self.pad,
(self.icon_height / 2) - self.pad,
fill=tuple2hex(HSBKtoRGB(Color(*bulb.get_color()))), style=PIESLICE,
extent=359 * bulb.get_color()[2] / 65535, width=0, tags=[label])
text = self.canvas.create_text(self.current_icon_width + self.pad, self.icon_height / 2 + self.pad,
text=label[:8], anchor=NW, tags=[label])
self.bulb_dict[label] = BulbIcon(circle, oval, rect, text)
# update sizing info
self.current_icon_width += self.icon_width
def update_icon(self, bulb):
icon = self.bulb_dict[bulb.get_label()]
try:
self.canvas.itemconfig(icon.oval, fill=tuple2hex(HSBKtoRGB(Color(*bulb.get_color()))),
extent=359 * bulb.get_color()[2] / 65535)
except WorkflowException:
print("Workflow Exception Caught")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment