创建画布以及函数,绘制坐标

创建宽度和高度均为600像素的窗体,并在其上创建400×400的画布,画布原点设置在画布中心,通过单选按钮选定公式,点击绘图按钮,绘制相应图形X,Y轴,放大倍数均为25,以红色实线绘制带箭头的坐标,当x在【-2π,2π】区间内以步长0.01变化时,用蓝色绘制函数

img

import tkinter as tk
import numpy as np

def drawCoord():
    global canvas
    canvas = tk.Canvas(win, width = 400, height = 400, bg = 'white')
    canvas.place(x = 100, y = 100)
    coords = (20,200,380,200), (200,20,200,380)
    points = [370,195, 370,205, 380,200], [195,30, 205,30, 200,20]
    for i in range(2):
        canvas.create_line(coords[i], fill = 'red', width = 2)
        canvas.create_polygon(points[i], outline="red", fill='red')

    labels = '-2π','-π','0','π','2π'
    for i in range(5):
        canvas.create_text(3.14 * 25 * i + 35, 210, text = labels[i], fill = 'red', font = ('System', 12))
        if i!=2:
            canvas.create_text(185, 3.14 * 25 * i + 40,  text = labels[i], fill = 'red', font = ('System', 12))

def Sin():
    canvas.destroy()
    drawCoord()
    x = np.linspace(0, 2*np.pi, int(2*np.pi/0.01), endpoint=True)
    y = np.sin(x)
    for i,j in zip(x,y):
        canvas.create_oval(i*25*2+42,j*3.14*25+200, i*25*2+43,j*3.14*25+201, fill = 'blue')

def Cos():
    canvas.destroy()
    drawCoord()
    x = np.linspace(0, 2*np.pi, int(2*np.pi/0.01), endpoint=True)
    y = np.cos(x)
    for i,j in zip(x,y):
        canvas.create_oval(i*25*2+42,j*3.14*25+200, i*25*2+43,j*3.14*25+201, fill = 'blue')

if __name__ == '__main__':
 
    win = tk.Tk()
    win.geometry('600x600')
    win.title('Draw Function Image')

    drawCoord()
    Sin()

    radio1 = tk.Radiobutton(win, text = 'y = cos(x)', command = Cos, value = 0)
    radio2 = tk.Radiobutton(win, text = 'y = sin(x)', command = Sin, value = 1)
    radio1.place(x = 400, y = 550)
    radio2.place(x = 400, y = 520)

    win.mainloop()