Advertisement
here2share

# Tk_img_opacity_trick.py

Jun 5th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.12 KB | None | 0 0
  1. # Tk_img_opacity_trick.py -- has a few minor drawbacks
  2.  
  3. from Tkinter import *
  4. from cStringIO import StringIO
  5. from PIL import ImageTk, Image
  6. import base64, zlib
  7.  
  8. root = Tk()
  9. ws = root.winfo_screenwidth() # width of the screen
  10. hs = root.winfo_screenheight() # height of the screen
  11. ww = 300
  12. hh = 300
  13. # calculate x and y coordinates for the Tk root window
  14. xx = (ws/2) - (ww/2)
  15. yy = (hs/2) - (hh/2)
  16. root.geometry("%dx%d%+d%+d" % (ww, hh, xx, yy))
  17.  
  18. b64='''\
  19. eJztWPdTE86+TUICCaEE6T0QWui9S0IHpYMU6UgXDU2aUhKKFJHQi1QBqUJEpINUqUpVlCJViiKg
  20. WBCBXL533nv/wv3hvjPzmT17dmZ3Zmc/O5/zSTIz0ael5qAGAAC0hgY6FgAAEPtPQCkulBOTa3kX
  21. AzDIQl8L0PCGa/diAvbSNNYEAJ6lwU9dIQAABcDMyEr3QidQUcVCoXHU1PE0NAl0dElMTMksLMms
  22. rClsbCns7A84OB5wcqZycaVycz/k4UlDItP4+Ij8/EQUKl1AIF1QMENIKENYOFNEJBONzhIVzRIT
  23. yxYXz5aQyJGUzJGSypWWzpOVzZOTy5eXz1dQKFBULFBSeqSs/EhFpVBVtUhdvVhDoxiDKcFiSzQ1
  24. S7W0SrW1y3R0ynR1H+vplRsYlBsaVly5UnH1aqWRUaWx8RMTkyemplVmZtUWFtWWljVWVjXXrtVa
  25. W9fa2NTZ2tbZ2dXb2z91cHjq6Njg5NTg7Nzo4tLo6kpyc3vm4dHk6dnk5fXc2/u5j0+zr28LDtca
  26. ENAWGNgREtIRGtoZHt4dGdkTFQX4f/w34ToWvX6RJ4BgCxNL8n83AJa/qQQvrgTmb2AXBADQDfwT
  27. wGFctfuFyB6saxtsifMMDnUN9AD4u/rcDpa87RGM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lkMBWPBY1e1p3z5VJJW70GzO9EKpRj95eqQtj5NFlrWZRT
  68. 1GkLH63yuWurxxKalszwptX2SBUG7QlM7lpmY8A9Ft8rnvkRB9ZHkIwv+VZSUz6I3HiOqfTLmr53
  69. jpwFpFujrIon/QniSlgktVxYTHWTGWi0BrtzCBCWLhWCoPH0iA6gRTCSiPVVvEM17VVjuIqKyxKc
  70. kmzFghiy+n7+ZZQzA3FvuzaZBclm9SrEucdM0RIfUzBA+MMoahl+7Q1WmHGzB3qA+diRwRQVO9gh
  71. UHpFnws5gdd5nYlSZAzwvzoFWCuBVTkemCyLNaCELgPQqqWgfzYSnfo/upDIhtSlEmWpXs4DZ3nE
  72. 5NE0o17HNNEEi2wDxmSnRsGsZrYsRW8CgZUe07BrrGbXc64/+YHLLppx7si3POmoHsTPNGWxVI8X
  73. wEQkUiTrO2NmaIJv3qcQeY1VYm2PWW+S97jL+XPEYaOlVSEsVvBPl82Gou1nlhWaMrB192hhTwwZ
  74. E7L75QbdSakxJS/endlvv9zJSqnjo363AT418j2xPob23ntSx7Dj7i8U3oG2bEXzXe+w40/8TSoD
  75. /ASKPDIw+H0siSADsz9rE2pLTztV6wp2e3VWyej8TaqodJs+Yaki12TqCMbnfBd0g5L8eL54MPrb
  76. m/lyqY9lWwdySyTWAyIIN1FXAnpaQw9Fk97kpsk04sqdNfSiWCmS2tfRJJ/Gl+WdMT8iPIOco6uk
  77. XIIBlQVtPSCmDyntSY0AQdrPV725se2U+42aCSCG05vEUbE2NMgwdiKlzSt9WsTcedA4OiQviiH0
  78. J2j02+HrSGb1psDhl5QZlGJkNuB89NoT14IjGdAT6o5ouo1o/zUIWeb+3wnWk4IjFZoz5DggGm/k
  79. bIaBWfFk3Z4hpllMO/Y+OjQXjYiUcx3zld7zmmbZDff2VGlTxDdr7g/cM63Z1pONO5VeWXh9LM1r
  80. CTnD0KpY788S36WEAqi51vakBr0JwV0ImDvNFQ2T6+8L3z96mMX5I8BCD9munGGxddQ2ixhy+dJO
  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Akq5nhHXOsGo0uOXMRnNBsZhKAdr8II0TkW/d4UZLyt/tr8bpb1+
  95. nCIgDXRd3OHmkU37pDxlOElgXMDa9iUJkR705UqZ/op3/NJ9lpkLccwFQkXVv4FzZPL4P19nDX7U
  96. qGnAkCh8GNhZ32/c2t5/s+NHZm5518rDIPLVJFuKp0FzKk6NRr+55EPuzopt/h0wAL3BpF2ydNmt
  97. C+s9EHv+/LlXvMTRtN13L7G5v4x2CCOudepgyzz/P12zuyT7U9MMZ41W42wLJNF2kCXP4bJBhTor
  98. zpLHXU+smyqW127b0JASqCPYgl3syT8GL5DdUD29v4dbPnVU+w03RMGYoUZYLebY/g2L1F5Vpser
  99. 760ESYHxIgqqb2H1c8jk+hhqyO1cnkWY4dXaB9QQ67c8NtO/oi5MNWya2dr2D+WFzwbm6wF1rQxo
  100. QRvowV2xShY8ekSFTfnWpznBwzC1nWX/zoUclPYtBWngKBVAuD5WFSKEjYEW1gr5sigXNsbCvPzD
  101. ns5JSrtOm66t9i74EHqDIVKVza/c63MQuMrX8Z5QLtkpW5eA1Qb2zwnyhqWwB8BTUri8zSqt5n+2
  102. rfE/NDmMfM4rS4apTQu6cv7TDzPUNdFp0HIh/AtH+5Nu'''
  103.  
  104. b64 = base64.decodestring(b64)
  105. zzz = StringIO(zlib.decompress(b64))
  106. img_data = Image.open(zzz)
  107. img = ImageTk.PhotoImage(img_data)
  108.  
  109. cv = Canvas(root, width=300, height=300)
  110. cv.grid(row=0, column=0)
  111. cv.create_rectangle(0, 0, 180, 180, fill='blue')
  112. cv.create_rectangle(120, 120, 300, 300, fill='yellow')
  113. top = Toplevel()
  114. top.geometry("%dx%d%+d%+d" % (ww, hh, xx+8, yy+32))
  115. top2 = Toplevel()
  116. top2.geometry("%dx%d%+d%+d" % (ww, hh, xx+8, yy+32))
  117. cv2 = Canvas(top, width=300, height=300, bg='white')
  118. cv2.grid(row=0, column=0)
  119. cv2.master.overrideredirect(True)
  120. cv2.master.wm_attributes("-topmost", True)
  121. cv2.master.wm_attributes('-transparentcolor', 'white')
  122. cv2.master.wm_attributes('-alpha', 0.85)
  123. cv2.create_image(150, 150, image=img)
  124. cv3 = Canvas(top2, width=300, height=300, bg='darkgreen')
  125. cv3.grid(row=0, column=0)
  126. cv3.master.overrideredirect(True)
  127. cv3.master.wm_attributes("-topmost", True)
  128. cv3.master.wm_attributes('-transparentcolor', 'darkgreen')
  129. cv3.create_text(150, 150, text='TKINTER', font=('', 32, 'bold', 'italic'), fill='green')
  130. top.lift() #???
  131.  
  132. def configure(event):
  133.     xx, yy = event.x, event.y
  134.     if xx and yy:
  135.         top.geometry("%dx%d%+d%+d" % (ww, hh, xx+8, yy+32))
  136.         top2.geometry("%dx%d%+d%+d" % (ww, hh, xx+8, yy+32))
  137.  
  138. root.bind("<Configure>", configure)
  139.  
  140. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement