Python tkinter库之Canvas 直线等分圆弧(割圆术)

直线等分圆周,分隔得越多越接近于圆:

import tkinter as tk
import pyautogui as ag
import random
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()

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")

    Color = ['red','blue','green','magenta','navy','lawngreen','orange']
    Rad = [60,45,30,15,10,5]
    
    for i in range(len(Rad)):
        step=Rad[i]*pi/180
        for t in np(-pi,pi,step):
            x = 240+200*cos(t)
            y = 240+200*sin(t)
            for r in np(-pi,pi,step):
                x0 = 240+200*cos(r)
                y0 = 240+200*sin(r)
                c = random.choice(Color)
                coord=x,y,x0,y0
                tCanvas.create_line(coord,fill=c)
            tCanvas.update()
        Delay(2)
        if i+1!=len(Rad):
            tCanvas.create_rectangle(0,0,480,480,outline='white',fill='white')
    win.mainloop()

效果图:

 改进一下代码:只连接相邻分隔点,加快程序速度;分割圆周2^15=32768等份,按照“割圆术”来算pi的话可以精确到3.1415926了。

import tkinter as tk
import pyautogui as ag
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()

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))
    step = pi
    for i in range(1,15):
        step/=2
        for t in np(-pi+pi/4,pi+pi/4,step):
            x = 240+200*cos(t)
            y = 240+200*sin(t)
            x0 = 240+200*cos(t+step)
            y0 = 240+200*sin(t+step)
            coord=x,y,x0,y0
            tCanvas.create_line(coord)
        tCanvas.itemconfig(c_txt, text=str(2**(i+1)))
        tCanvas.update()
        Delay(0.5)
    
    tCanvas.create_text((225, 280),text='End!',anchor=tk.W, font=("宋体",20))
    tCanvas.update()
    win.mainloop()

其中:函数 itemconfig( id, text='变更的内容') 可以在后期修改文本内容

c_txt = tCanvas.create_text((225, 240),text='变更前的文字',anchor=tk.W, font=("宋体",20))

tCanvas.itemconfig(c_txt, text='变更后的文字')

 效果图: (分到256份后已看不出变化了)

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hann Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值