here2share

# Tk_five_dice.py

Jul 11th, 2019 (edited)
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.70 KB | None | 0 0
  1. # Tk_Five_Dice.py
  2.  
  3. import math
  4. import time
  5. from Tkinter import *
  6. import random
  7. import tkFileDialog
  8. from PIL import ImageTk, Image, ImageGrab
  9. import base64, zlib
  10. import re
  11. import os
  12.  
  13. def grab_screen(area):
  14.     # left, upper, right, bottom = area
  15.     x = root.winfo_rootx() + canvas.winfo_x() + 120
  16.     y = root.winfo_rooty() + canvas.winfo_y() + 20
  17.     # xx = x + canvas.winfo_width()
  18.     # yy = y + canvas.winfo_height()
  19.     image = ImageGrab.grab(bbox=(x, y, x+60, y+60))
  20.     photo = ImageTk.PhotoImage(image)
  21.     return photo
  22. 0
  23. def str_2_b64(s):
  24.     compressed = zlib.compress(s)
  25.     b64 = base64.encodestring(compressed)
  26.     return b64
  27. 0
  28. def b64_2_str(b64):
  29.     b64 = base64.decodestring(b64)
  30.     s = zlib.decompress(b64)
  31.     return s
  32. 0
  33. e255 = {}
  34. ddd = 5
  35. for n in range(0,256,ddd):
  36.     e255[n/ddd] = n
  37. e255[n/ddd] = 255
  38. def d(rgb):
  39.     z = e255[int(sum(rgb[:3])/3)/ddd]
  40.     if len(rgb) == 4:
  41.         return (z,z,z,rgb[-1]/ddd*ddd)
  42.     return (z,z,z,255)
  43. width,height=680,680
  44. bgc = '#bbbbbb'
  45.  
  46. root = Tk()
  47. width,height=60*5,100
  48. # root.geometry('%dx%d+60+60'%(width,height))
  49. canvas = Canvas(root,width=width, height=height)
  50. root.title('Tk Five Dice')
  51. canvas.grid()
  52.  
  53. abc = list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
  54. xyz = abc[:]
  55.  
  56. blank = '''
  57. eJzt10GOwyAMBdC9DxMltJ1pzxMwYA8h91/1p2nVdFeGrS19IVl+giWm4VncUC9D+yFL0VS/qqRl
  58. kSNeF00cQmCOMaWUcxZR1T8UDhE00I6RH0NJl/WN15LZe3/A8onlgDHIuawvDLu1vsc+7Jq2N++2
  59. AUNvL6dBqoZ5nlswxoNWAa4l+nbsY6kDcZXt4lY8B6ls2LBhw4YNGzZs2HAL7vq49n2Zuz7rfWtC
  60. 34LStxr1LWX/XQfdeSR3+kXOyETOXZEL4shNN+QHOSGYGzE3Ym6caLpdkcsd1RSlNQ=='''[1:]
  61.  
  62. dot = '''
  63. eJxFj1kOwjAMRP/nMJYdKMtx2PcWkgaCT88EKFgaKZN5thMRkb64l8yDQCT5t1K1dOU5eIh7TPl7
  64. IcjentsulbfNKHGz2V+u93dc4OfFcnvq8oeGt6v1bkgdj3g8XLr46S0clW5xmJzrot9eLpLef9XX
  65. R/Z/V61I/n9BYYqgGCnGikYxUUwVM8W8RhRzI2AkjIiRMUJGyojZvPZTdQ65QC6MqxoEsqFpXuXs
  66. YV4='''[1:]
  67.  
  68. blank = b64_2_str(blank)
  69. dot = b64_2_str(dot)
  70.  
  71. def abc2rgb(zzz):
  72.     zzz = zzz.replace('.','(0,0,0,0),')
  73.     ttt = zzz.splitlines()[::-1]
  74.     ttt = [t for t in ttt if len(t) < 4]
  75.     zzz = zzz.replace('\n'.join(ttt[::-1]),'')
  76.     for z in abc:
  77.         if not ttt:
  78.             break
  79.         t = ttt.pop(0)
  80.         zzz = zzz.replace('\n'+t, '')
  81.         if len(t) < 4:
  82.             t = ','.join((t,t,t))
  83.         zzz = zzz.replace(z, '('+t+',255)'+',')
  84.     return eval('['+zzz[:-1]+']')
  85. 0
  86. data = abc2rgb(blank)
  87.  
  88. w,h = 60,60
  89. img = Image.new('RGBA', (w,h))
  90. img.putdata(data)
  91. photo = img.convert("RGBA")
  92. tki = ImageTk.PhotoImage(photo)
  93. canvas.create_image(width/2-(w/2),height/2, image=tki, anchor='w', tag='blank')
  94. root.update()
  95.  
  96. data = abc2rgb(dot)
  97.  
  98. data = [tuple([max(210,r),max(80,g),max(80,b),a]) for r,g,b,a in data]
  99.  
  100. w,h = 14,14
  101. img2 = Image.new('RGBA', (w,h))
  102. img2.putdata(data)
  103. photo2 = img2.convert("RGBA")
  104. tki2 = ImageTk.PhotoImage(photo2)
  105.  
  106. # canvas area
  107. area = [width/2-(60/2),height/2,width/2+(60/2),height/2+60]
  108.  
  109. places = {  '1':(-1,-1), '2':( 0,-1), '3':( 1,-1),
  110.             '4':(-1, 0), '5':( 0, 0), '6':( 1, 0),
  111.             '7':(-1, 1), '8':( 0, 1), '9':( 1, 1)
  112. }
  113. values = {  1:'5', 2:'37', 3:'375', 4:'1379',  
  114.             5:'13795', 6:'137946', 7:'1379465',  
  115.             8:'13794628', 9:'123456789'
  116. }
  117. p = 17
  118. dice_img = {}
  119. for z in [9]+range(9,0,-1):
  120.     for v in values[z]:
  121.         ww,hh = places[v]
  122.         canvas.create_image(width/2-(w/2)+(ww*p),height/2+(hh*p), image=tki2, anchor='w', tag='dots')
  123.     t = time.time()+0.1
  124.     while t > time.time():
  125.         0
  126.     root.update()
  127.     ### f = dir+"dice_side_"+str(z)+".png"
  128.     ### dice_img[z] = grab_screen_area_to(f,area)
  129.     dice_img[z] = grab_screen(area)
  130.     canvas.delete('dots')
  131.  
  132. canvas.delete('blank')
  133. # help(dice_img[1])
  134. 0
  135. def dice_roll():
  136.     if die['roll'] == 2:
  137.         if 'ROLL' not in str([lock[z]['text'] for z in lock]):
  138.             print "Must Unselect Which Dices To Roll Again"
  139.             return
  140.         die['roll'] = 1
  141.         t = '----'
  142.         color = 'grey'
  143.     else:
  144.         die['roll'] = 2
  145.         t = '--LOCKED--'
  146.         color = 'green'
  147.     for z in range(0,5):
  148.         if lock[z]['text'] != '--LOCKED--':
  149.             die[z] = random.randint(1,6)
  150.             die_btn[z].config(image=dice_img[die[z]])
  151.             dict_roll.append(z)
  152.         lock[z].config(text=t, bg=color)
  153.        
  154. 0
  155. def dice_lock(btn):
  156.     if die['roll'] == 2:
  157.         if lock[btn]['text'] == '--REROLL--':
  158.             lock[btn].config(text='--LOCKED--', bg='green')
  159.         else:
  160.             lock[btn].config(text='--REROLL--', bg=bgc)
  161. 0
  162. frame_1 = Frame(canvas)
  163.  
  164. dict_roll = []
  165. die_btn = {}
  166. v = {}
  167. lock = {}
  168. die = {'roll':1}
  169. for z in range(5):
  170.     v[z] = StringVar()
  171.     lock[z] = Label(frame_1, text='----', bg='grey', width=9)
  172.     lock[z].grid(row=1, column=z)
  173.     die_btn[z] = Button(frame_1, image=dice_img[9], command=lambda x=z:dice_lock(x), height=60, width=60)  # ZZZ image
  174.     die_btn[z].grid(row=0, column=z, sticky=N+S+E+W)
  175. 0
  176. roll_button = Button(frame_1, text='Roll Dice', command=dice_roll, bg='orange', fg='white')
  177. roll_button.grid(row=2, sticky=N+S+E+W)
  178.  
  179. frame_1.pack()
  180.  
  181. root.mainloop()
Add Comment
Please, Sign In to add comment