1 # tkinter高级图形课程 2 #!/usr/bin/python 3 # -*- coding: UTF-8 -*- 4 # python 36 5 6 from tkinter import * 7 import turtle 8 import random 9 10 tk = Tk() 11 canvas = Canvas(tk, width=640, height=480) 12 canvas.pack() 13 fill_color = ['green', 'red', 'blue', 'orange', 'yellow', 'pink', 'purple', 'violet', 'magenta', 'cyan'] 14 coloe_mun = int(len(fill_color)) 15 16 def random_rectangle(width, height, colors): 17 x1 = random.randrange(width) 18 y1 = random.randrange(height) 19 x2 = x1 + random.randrange(width) 20 y2 = y1 + random.randrange(height) 21 canvas.create_rectangle(x1, y1, x2,y2, fill=colors) 22 23 for x in range(0, 100): 24 c = random.randint(0, coloe_mun-1) 25 colors = str(fill_color[c]) 26 random_rectangle(400, 400, colors) 27 tk.mainloop()
调用
tkinter,显示随机矩形,随机颜色