Python tkinter库之Canvas 以圆模拟画圆环

tkinter.Canvas.create_xxx()没有直接定义画圆的函数,可以用等轴的椭圆或360度的弧形代替。本例中用画椭圆函数创建一个以圆心坐标x,y和半径r为自变量的画圆函数 Circle(x,y,r,color) ;用它模仿画圆环,叠加很多圆,它们的圆心都在另一圆周上,定圆和动圆的半径以及动圆的稀密程序随机变化:

import tkinter as tk
import pyautogui as ag
from random import *
from time import sleep as Delay
from math import sin
from math import cos
from math import pi
from numpy import arange as np

def Window_Open(W, H):
    X, Y = ag.size()
    winSize = str(W)+"x"+str(H)
    winPos = winSize + "+" + str((X - W) // 2)
    winPos += "+" + str((Y - H) // 2)
    win.geometry(winPos)
    win.resizable(False, False)
    title = u'桌面分辨率:' + str(X) + "x" + str(Y)
    title += ' ' * 5 + u'窗体大小:' + winSize
    win.title(title)
    win.update()

def Circle(x,y,r,c='black'):
    coord=x-r,y-r,x+r,y+r
    tCanvas.create_oval(coord,outline=c)

rand = lambda a,b:(random()*(a+1))+b

if __name__ == '__main__':
    
    win = tk.Tk()
    Window_Open(480,480)
    tCanvas = tk.Canvas(win, width=win.winfo_width(), height=480, bg='white')
    tCanvas.pack(side="top")
    c_txt=tCanvas.create_text((225, 240),text='',anchor=tk.W, font=("宋体",20))
    Color = ['red','blue','green','magenta','navy','lawngreen','orange']
    I=20
    for i in range(1,I+1):
        R=rand(60,80)
        r=rand(30,50)
        step = rand(1,15)*pi/1080
        for t in np(-pi,pi,step):
            x = 240+R*cos(t)
            y = 220+R*sin(t)
            c = choice(Color)
            Circle(x,y,r,c)
        tCanvas.update()
        Delay(0.5)
        if i!=I:tCanvas.delete("all")

    tCanvas.create_text((225, 450),text='End!',anchor=tk.W, font=("宋体",20))
    win.mainloop()

效果图: 

猜你喜欢

转载自blog.csdn.net/boysoft2002/article/details/115386613
今日推荐