Advertisement
here2share

# Tk_shape_demo.py

Oct 17th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1. # Tk_shape_demo.py
  2.  
  3. from Tkinter import *
  4. import random
  5. import time
  6.  
  7. def click():
  8.     radius = 0
  9.     size = sizeVar.get()
  10.     shape = shapeVar.get()
  11.     if size == "small":
  12.         ran1 = 30
  13.         ran2 = 60
  14.     elif size == "medium":
  15.         ran1 = 60
  16.         ran2 = 90
  17.     else:
  18.         ran1 = 90
  19.         ran2 = 120
  20.  
  21.     circleNum = random.randint(10,25)
  22.  
  23.     for i in range(0, circleNum):
  24.         d = random.randint(ran1,ran2)
  25.  
  26.         colour = colourVar.get()
  27.         if colour == rColour:
  28.             colour = randColour()
  29.         x = random.randint(0, 300)
  30.         y = random.randint(0, 400)
  31.         if shape == "circle":
  32.             circle = canvas.create_oval(x, y, x+d, y+d, fill = colour)
  33.         elif shape == "square":
  34.             square = canvas.create_rectangle(x, y, x+d, y+d, fill = colour)
  35.         else:
  36.             triangle = canvas.create_polygon(x, y, x+d, y, x+(d/2), y-d, fill = colour, outline = "black")
  37.  
  38.         time.sleep(0.05)
  39.         canvas.update()
  40.  
  41. def randColour():
  42.     colour = "#"
  43.     for i in range(0, 6):
  44.         colour = colour + random.choice("ABCDEF0123456789")
  45.     return colour
  46.  
  47. def clickDraw(event):
  48.     x = event.x
  49.     y = event.y
  50.     size = sizeVar.get()
  51.     if size == "small":
  52.         d = 60
  53.     elif size == "medium":
  54.         d = 90
  55.     else:
  56.         d = 120
  57.     shape = shapeVar.get()
  58.     colour = colourVar.get()
  59.     if colour == rColour:
  60.         colour = randColour()
  61.     if shape == "circle":
  62.         circle = canvas.create_oval(x, y, x+d, y+d, fill = colour)
  63.     elif shape == "square":
  64.         square = canvas.create_rectangle(x, y, x+d, y+d, fill = colour)
  65.     else:
  66.         triangle = canvas.create_polygon(x, y, x+d, y, x+(d/2), y-d, fill = colour, outline = "black")
  67.     canvas.update()
  68.  
  69. def clear():
  70.     canvas.delete("all")
  71.  
  72. #generate the holding structures
  73. root = Tk()
  74.  
  75. mainframe = Frame(root, bg = "white")
  76.  
  77. colourFrame = LabelFrame(mainframe, text = "Choose a colour for shapes", font = ("Calibri", 14))
  78. sizeFrame = LabelFrame(mainframe, text = "Choose a size for shapes", font = ("Calibri", 14))
  79. shapeFrame = LabelFrame(mainframe, text = "Choose a shape to draw", font = ("Calibri", 14))
  80.  
  81. #create the widgets
  82. canvas = Canvas(mainframe, width = 300, height = 400, bg = "white")
  83. title = Label(mainframe, text = "Shape Generator Demo", font = ("Calibri", 20), bg = "white", fg = "purple")
  84.  
  85. colourVar = StringVar()
  86.  
  87. blackRadio = Radiobutton(colourFrame, text = "Black", variable = colourVar, value = "black")
  88. yellowRadio = Radiobutton(colourFrame, text = "Yellow", variable = colourVar, value = "yellow")
  89. blueRadio = Radiobutton(colourFrame, text = "Blue", variable = colourVar, value = "blue")
  90. greenRadio= Radiobutton(colourFrame, text = "Green", variable = colourVar, value = "green")
  91. redRadio = Radiobutton(colourFrame, text = "Red", variable = colourVar, value = "red")
  92. purpleRadio = Radiobutton(colourFrame, text = "Purple", variable = colourVar, value = "purple")
  93. rColour = randColour()
  94. randomRadio = Radiobutton(colourFrame, text = "Random", variable = colourVar, value = rColour)
  95. colourVar.set("black")
  96.  
  97. sizeVar = StringVar()
  98.  
  99. smallRadio = Radiobutton(sizeFrame, text = "Small", variable = sizeVar, value = "small")
  100. mediumRadio = Radiobutton(sizeFrame, text = "Medium", variable = sizeVar, value = "medium")
  101. bigRadio = Radiobutton(sizeFrame, text = "Large", variable = sizeVar, value = "big")
  102. sizeVar.set("small")
  103.  
  104. shapeVar = StringVar()
  105.  
  106. cirRadio = Radiobutton(shapeFrame, text = "Circle", variable = shapeVar, value = "circle")
  107. squRadio = Radiobutton(shapeFrame, text = "Square", variable = shapeVar, value = "square")
  108. triRadio = Radiobutton(shapeFrame, text = "Triangle", variable = shapeVar, value = "triangle")
  109. shapeVar.set("circle")
  110.  
  111. drawButton = Button(mainframe, text = "Draw Shapes", command = click, font = ("Calibri", 14), bg = "lightblue")
  112. clearButton = Button(mainframe, text = "Clear", command = clear, font = ("Calibri", 14), bg = "lightblue")
  113.  
  114. canvas.bind("<Button-1>", clickDraw)
  115.  
  116. #gridding
  117. mainframe.grid()
  118. title.grid(row=0, column=0, columnspan=3)
  119. colourFrame.grid(row=1, column=0, pady=15, padx=10)
  120. sizeFrame.grid(row=2, column=0, pady=15, padx=10)
  121. shapeFrame.grid(row=3, column=0, pady=15)
  122.  
  123. blackRadio.grid(row=0, sticky=W)
  124. yellowRadio.grid(sticky=W)
  125. blueRadio.grid(sticky=W)
  126. greenRadio.grid(sticky=W)
  127. redRadio.grid(sticky=W)
  128. purpleRadio.grid(sticky=W)
  129. randomRadio.grid(sticky=W)
  130.  
  131. smallRadio.grid(row=0, sticky=W)
  132. mediumRadio.grid(sticky=W)
  133. bigRadio.grid(sticky=W)
  134.  
  135. cirRadio.grid(row=0, sticky=W)
  136. squRadio.grid(sticky=W)
  137. triRadio.grid(sticky=W)
  138.  
  139. drawButton.grid(row=3, column=1, pady = 15)
  140. clearButton.grid(row=3, column=2, pady=15)
  141.  
  142. canvas.grid(row=1, column=1, rowspan=2, columnspan=2, padx=10, pady=15)
  143.  
  144. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement