Advertisement
here2share

# Tk_rounded_buttons.py

Sep 8th, 2020 (edited)
2,993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. # Tk_rounded_buttons.py
  2.  
  3. from Tkinter import *
  4. from PIL import ImageDraw, ImageTk, Image, ImageGrab
  5. from math import sin, cos, pi
  6. import random
  7.  
  8. root = Tk()
  9. root.title("Tk Rounded Buttons")
  10. ### root.withdraw() # vs root.deiconify()')
  11. xm,ym = 600,600
  12.  
  13. canvas = Canvas(root, width=xm, height=ym)
  14. canvas.grid()
  15.  
  16. def click(event):
  17.     x,y = event.x,event.y
  18.     print x,y
  19.     go[1] = 1
  20.  
  21. def rounded_button():
  22.     canvas.delete('oBtn')
  23.     vertex_count = 100
  24.     t =  pi/vertex_count
  25.     a = 1.577
  26.     VERTEX = [(x1,y1+r)]
  27.     VERTEX += [(    x1+r*cos(i*2*t+a), \
  28.                     y1+r*sin(i*2*t+a)) for i in range(vertex_count/2)]
  29.     VERTEX += [(x2,y1-r)]
  30.     VERTEX += [[(   x2+r*cos(i*2*t+a), \
  31.                     y1+r*sin(i*2*t+a))] for i in range(vertex_count/2,vertex_count)]
  32.     canvas.create_polygon(VERTEX, fill=fillc, width=borderw, outline=borderc, tag='oBtn')
  33.     font_style = ''
  34.     if r > 21+borderw:
  35.         font_style = 'bold italic'
  36.     fff = ' '.join(['ariel', str(int(max(6,r*0.6))), font_style])
  37.     canvas.create_text(x1+(x2-x1)/2, y1, text='Hey!', font=fff, fill=borderc, tag='oBtn')
  38.     canvas.tag_bind('oBtn', '<Button-1>', click)
  39.    
  40.  
  41. def pop2(zzz):
  42.     L = len(zzz)
  43.     return zzz[ccc%L]
  44. 0
  45. zzz = '''
  46. red orange green blue purple gray black
  47. red orange yellow green blue purple
  48. white
  49. black
  50. '''.strip().splitlines()
  51. btn_colors = [[(z,zzz[i+2]),(zzz[i+2],z)] for i in range(2) for z in zzz[i].split()]
  52. btn_colors = [[a,b] for z in btn_colors for a,b in z]
  53. random.shuffle(btn_colors)
  54. border_width = list(range(10))
  55. xya = [(x,y) for x in range(100,xm-200,50) for y in range(100,ym-200,50)]
  56. random.shuffle(xya)
  57. xyb = [(x,y) for x in range(0,100,5) for y in range(24,16*5,4)]
  58. random.shuffle(xyb)
  59. go = 1
  60. ccc = 0
  61. while 1:
  62.     if go:
  63.         go = {}
  64.         fillc,borderc = pop2(btn_colors)
  65.         borderw = pop2(border_width)
  66.         x1,y1 = pop2(xya)
  67.         x2,r = pop2(xyb)
  68.         x2 = x1+x2
  69.         rounded_button()
  70.         ccc += 1
  71.     canvas.update()
  72. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement