Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_Five_Dice.py
- import math
- import time
- from Tkinter import *
- import random
- import tkFileDialog
- from PIL import ImageTk, Image, ImageGrab
- import base64, zlib
- import re
- import os
- def grab_screen(area):
- # left, upper, right, bottom = area
- x = root.winfo_rootx() + canvas.winfo_x() + 120
- y = root.winfo_rooty() + canvas.winfo_y() + 20
- # xx = x + canvas.winfo_width()
- # yy = y + canvas.winfo_height()
- image = ImageGrab.grab(bbox=(x, y, x+60, y+60))
- photo = ImageTk.PhotoImage(image)
- return photo
- 0
- def str_2_b64(s):
- compressed = zlib.compress(s)
- b64 = base64.encodestring(compressed)
- return b64
- 0
- def b64_2_str(b64):
- b64 = base64.decodestring(b64)
- s = zlib.decompress(b64)
- return s
- 0
- e255 = {}
- ddd = 5
- for n in range(0,256,ddd):
- e255[n/ddd] = n
- e255[n/ddd] = 255
- def d(rgb):
- z = e255[int(sum(rgb[:3])/3)/ddd]
- if len(rgb) == 4:
- return (z,z,z,rgb[-1]/ddd*ddd)
- return (z,z,z,255)
- width,height=680,680
- bgc = '#bbbbbb'
- root = Tk()
- width,height=60*5,100
- # root.geometry('%dx%d+60+60'%(width,height))
- canvas = Canvas(root,width=width, height=height)
- root.title('Tk Five Dice')
- canvas.grid()
- abc = list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
- xyz = abc[:]
- blank = '''
- eJzt10GOwyAMBdC9DxMltJ1pzxMwYA8h91/1p2nVdFeGrS19IVl+giWm4VncUC9D+yFL0VS/qqRl
- kSNeF00cQmCOMaWUcxZR1T8UDhE00I6RH0NJl/WN15LZe3/A8onlgDHIuawvDLu1vsc+7Jq2N++2
- AUNvL6dBqoZ5nlswxoNWAa4l+nbsY6kDcZXt4lY8B6ls2LBhw4YNGzZs2HAL7vq49n2Zuz7rfWtC
- 34LStxr1LWX/XQfdeSR3+kXOyETOXZEL4shNN+QHOSGYGzE3Ym6caLpdkcsd1RSlNQ=='''[1:]
- dot = '''
- eJxFj1kOwjAMRP/nMJYdKMtx2PcWkgaCT88EKFgaKZN5thMRkb64l8yDQCT5t1K1dOU5eIh7TPl7
- IcjentsulbfNKHGz2V+u93dc4OfFcnvq8oeGt6v1bkgdj3g8XLr46S0clW5xmJzrot9eLpLef9XX
- R/Z/V61I/n9BYYqgGCnGikYxUUwVM8W8RhRzI2AkjIiRMUJGyojZvPZTdQ65QC6MqxoEsqFpXuXs
- YV4='''[1:]
- blank = b64_2_str(blank)
- dot = b64_2_str(dot)
- def abc2rgb(zzz):
- zzz = zzz.replace('.','(0,0,0,0),')
- ttt = zzz.splitlines()[::-1]
- ttt = [t for t in ttt if len(t) < 4]
- zzz = zzz.replace('\n'.join(ttt[::-1]),'')
- for z in abc:
- if not ttt:
- break
- t = ttt.pop(0)
- zzz = zzz.replace('\n'+t, '')
- if len(t) < 4:
- t = ','.join((t,t,t))
- zzz = zzz.replace(z, '('+t+',255)'+',')
- return eval('['+zzz[:-1]+']')
- 0
- data = abc2rgb(blank)
- w,h = 60,60
- img = Image.new('RGBA', (w,h))
- img.putdata(data)
- photo = img.convert("RGBA")
- tki = ImageTk.PhotoImage(photo)
- canvas.create_image(width/2-(w/2),height/2, image=tki, anchor='w', tag='blank')
- root.update()
- data = abc2rgb(dot)
- data = [tuple([max(210,r),max(80,g),max(80,b),a]) for r,g,b,a in data]
- w,h = 14,14
- img2 = Image.new('RGBA', (w,h))
- img2.putdata(data)
- photo2 = img2.convert("RGBA")
- tki2 = ImageTk.PhotoImage(photo2)
- # canvas area
- area = [width/2-(60/2),height/2,width/2+(60/2),height/2+60]
- places = { '1':(-1,-1), '2':( 0,-1), '3':( 1,-1),
- '4':(-1, 0), '5':( 0, 0), '6':( 1, 0),
- '7':(-1, 1), '8':( 0, 1), '9':( 1, 1)
- }
- values = { 1:'5', 2:'37', 3:'375', 4:'1379',
- 5:'13795', 6:'137946', 7:'1379465',
- 8:'13794628', 9:'123456789'
- }
- p = 17
- dice_img = {}
- for z in [9]+range(9,0,-1):
- for v in values[z]:
- ww,hh = places[v]
- canvas.create_image(width/2-(w/2)+(ww*p),height/2+(hh*p), image=tki2, anchor='w', tag='dots')
- t = time.time()+0.1
- while t > time.time():
- 0
- root.update()
- ### f = dir+"dice_side_"+str(z)+".png"
- ### dice_img[z] = grab_screen_area_to(f,area)
- dice_img[z] = grab_screen(area)
- canvas.delete('dots')
- canvas.delete('blank')
- # help(dice_img[1])
- 0
- def dice_roll():
- if die['roll'] == 2:
- if 'ROLL' not in str([lock[z]['text'] for z in lock]):
- print "Must Unselect Which Dices To Roll Again"
- return
- die['roll'] = 1
- t = '----'
- color = 'grey'
- else:
- die['roll'] = 2
- t = '--LOCKED--'
- color = 'green'
- for z in range(0,5):
- if lock[z]['text'] != '--LOCKED--':
- die[z] = random.randint(1,6)
- die_btn[z].config(image=dice_img[die[z]])
- dict_roll.append(z)
- lock[z].config(text=t, bg=color)
- 0
- def dice_lock(btn):
- if die['roll'] == 2:
- if lock[btn]['text'] == '--REROLL--':
- lock[btn].config(text='--LOCKED--', bg='green')
- else:
- lock[btn].config(text='--REROLL--', bg=bgc)
- 0
- frame_1 = Frame(canvas)
- dict_roll = []
- die_btn = {}
- v = {}
- lock = {}
- die = {'roll':1}
- for z in range(5):
- v[z] = StringVar()
- lock[z] = Label(frame_1, text='----', bg='grey', width=9)
- lock[z].grid(row=1, column=z)
- die_btn[z] = Button(frame_1, image=dice_img[9], command=lambda x=z:dice_lock(x), height=60, width=60) # ZZZ image
- die_btn[z].grid(row=0, column=z, sticky=N+S+E+W)
- 0
- roll_button = Button(frame_1, text='Roll Dice', command=dice_roll, bg='orange', fg='white')
- roll_button.grid(row=2, sticky=N+S+E+W)
- frame_1.pack()
- root.mainloop()
Add Comment
Please, Sign In to add comment