Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!
Paste
Pasted as Python by swift ( 14 years ago )
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
A graphical clock that displays the time as binary representation.
Values are represented as follows::
8 * * *
4 * * * * *
2 * * * * * *
1 * * * * * *
hrs min sec
:License: GNU General Public License
"""
from datetime import datetime
from threading import Thread
import time
import Tkinter as tk
# lamp images (as base64-encoded GIF data)
LAMP_IMAGES = (
# off
'''
R0lGODlhGAAYAKIEAJqamnt7e0NDQwAAAP///wAAAAAAAAAAACH5BAEAAAQALAAAAAAYABgAAANe
SLrc/jBKNsaMFYB66dDgZk1ZaHJPaa6i87HwSAXwKsjLEOw1IPw4hW7HM/2OQcKQWDw6k0tmwPl0
SYlUpPU6zd62V++3EWWKoeWdF+UqU9mYJTIpqUzhnRw9z+8/EgA7
''',
# on
'''
R0lGODlhGAAYAKIEAOxISOAWFncPDwAAAP///wAAAAAAAAAAACH5BAEAAAQALAAAAAAYABgAAANe
SLrc/jBKNsaMFYB66dDgZk1ZaHJPaa6i87HwSAXwKsjLEOw1IPw4hW7HM/2OQcKQWDw6k0tmwPl0
SYlUpPU6zd62V++3EWWKoeWdF+UqU9mYJTIpqUzhnRw9z+8/EgA7
''',
)
def decimal_to_binary(decimal):
"""Convert a decimal (base 10) to a binary (base 2) number."""
def tmp():
n = decimal
if n == 0:
yield 0
while n > 0:
yield n % 2
n >>= 1
return ''.join(reversed(map(str, tmp())))
def time_as_matrix():
"""Return current time represented as binary matrix."""
decimal = map(int, datetime.now().strftime('%H%M%S'))
binary = [decimal_to_binary(n).rjust(4, '0') for n in decimal]
return tuple(tuple(map(int, ns)) for ns in zip(*binary))
class Lamp(tk.Label):
"""An indicator lamp widget."""
def __init__(self, frame, images):
tk.Label.__init__(self, frame)
self._images = tuple(images)
self.switch(False)
def switch(self, value):
"""Turn the lamp on or off."""
# Keep a reference to the image to prevent it from getting
# lost (i. e. garbage collected, as far as I understood a
# documentation note on this issue I can't find anymore).
self.image = self._images[int(bool(value))]
# Update the image.
self.config(image=self.image,bg="black")
class BinaryClock(tk.Frame):
"""A graphical binary clock."""
def __init__(self, master=None):
"""Build a grid of lamps according to the given matrix."""
tk.Frame.__init__(self, master, background = "black")
self.stop = False
# Load and prepare lamp images.
images = [tk.PhotoImage(data=data) for data in LAMP_IMAGES]
def generate_column(row_index, row_data):
for column_index, column_data in enumerate(row_data):
if column_data:
lamp = Lamp(self, images)
lamp.grid(row=row_index, column=column_index)
yield lamp
else:
yield
def generate_row(matrix):
for row_index, row_data in enumerate(matrix):
yield tuple(generate_column(row_index, row_data))
# Turn matrix into grid of lamps.
matrix = (
(0, 1, 0, 1, 0, 1),
(0, 1, 1, 1, 1, 1),
(1, 1, 1, 1, 1, 1),
(1, 1, 1, 1, 1, 1),
)
self.lamps = tuple(generate_row(matrix))
self.grid(sticky=tk.N + tk.S + tk.W + tk.E)
def refresh_lamps(self, matrix):
"""Refresh the status of the lamp grid."""
for row_idx, row in enumerate(matrix):
for column_idx, field in enumerate(row):
try:
self.lamps[row_idx][column_idx].switch(field)
except AttributeError:
pass
def update(self):
"""Update the lamps every second to the current time."""
while not self.stop:
try:
self.refresh_lamps(time_as_matrix())
time.sleep(1)
except KeyboardInterrupt:
pass
def main():
root = tk.Tk()
root.resizable(0, 0)
root.overrideredirect(True)
popup = tk.Menu(root, tearoff=0)
popup.add_command(label="Run")
popup.add_separator()
popup.add_command(label="Quit", command=root.destroy) # , command=next) etc...
def do_popup(event):
# display the popup menu
try:
popup.tk_popup(event.x_root, event.y_root, 0)
finally:
# make sure to release the grab (Tk 8.0a1 only)
popup.grab_release()
app = BinaryClock(root)
app.master.title('Binary Clock')
app.master.geometry('165x110+1-20')
app.master.config(padx=2, pady=2, bg="black")
app.grid(sticky=tk.N + tk.S + tk.W + tk.E)
app.master.bind("<Button-3>", do_popup)
Thread(target=app.update).start()
try:
root.mainloop()
finally:
app.stop = True
if __name__ == '__main__':
main()
Revise this Paste
Children: 35548