tkinter canvas

from tkinter import *
import random
import tkinter.colorchooser as cc
c = cc.askcolor()
tk = Tk()
canvas = Canvas(tk, width=400, height=400,bg=c[1])
canvas.pack()
def random_rectangle(width, height,fill_color):
    x1 = random.randrange(width)
    y1 = random.randrange(height)
    x2 = x1 + random.randrange(width)
    y2 = y1 + random.randrange(height)
    canvas.create_rectangle(x1, y1, x2, y2,fill=fill_color)
_color = [ 'green', 'red', 'blue', 'orange', 'yellow', 'pink', 'purple', 'violet','magenta','cyan']
#Draw rectangle with filled color
for x in range(0, 500):
    random_rectangle(400, 400,_color[random.randrange(len(_color))])
#write text to canvas
canvas.create_text(130, 120, text='Who rode around on a moose.', fill='red')
canvas.create_text(150, 100, text='There once was a man from Toulouse',fill=c[1])
canvas.create_text(150, 150, text='He said, "It\'s my curse,', font=('Times', 15))
canvas.create_text(200, 200, text='But it could be worse,', font=('Helvetica', 20))
canvas.create_text(220, 250, text='My cousin rides round', font=('Courier', 22))
canvas.create_text(220, 300, text='on a goose."', font=('Courier', 30),fill="blue")
#Canvas background picture
my_image = PhotoImage(file='c:\\test.gif')
canvas.create_image(0, 0, anchor=NW, image=myimage)
##random_rectangle(400, 400, 'green')
##random_rectangle(400, 400, 'red')
##random_rectangle(400, 400, 'blue')
##random_rectangle(400, 400, 'orange')
##random_rectangle(400, 400, 'yellow')
##random_rectangle(400, 400, 'pink')
##random_rectangle(400, 400, 'purple')
##random_rectangle(400, 400, 'violet')
##random_rectangle(400, 400, 'magenta')
##random_rectangle(400, 400, 'cyan')

cnliutz

计算机基础教师

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注