#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
from tkinter import *
canvas = Canvas(width = 400,height = 400,bg = 'white')
# 画红色的坐标轴线
canvas.create_line(0, 200, 400, 200, fill="yellow", arrow=LAST) # x轴的y画布高的一半
canvas.create_line(200,400, 200, 0, fill="red", arrow=LAST) # y轴的x=画布宽的一半
left = 20
right = 50
top = 50
num = 10
for i in range(num):
canvas.create_rectangle(20 - 2 * i,20 - 2 * i,10 * (i + 2),10 * ( i + 2))
# right += 5
# left += 5
# top += 10
canvas.pack()
mainloop()
画个带坐标的图
from tkinter import *
import math
root = Tk()
w = Canvas(root, width=320, height=240,bg='white')
w.pack()
w0 = 160 # 半宽
h0 = 120 # 半高
# 画红色的坐标轴线
w.create_line(0, 120, 320, 120, fill="yellow", arrow=LAST) # x轴的y画布高的一半
w.create_line(160, 240, 160, 0, fill="red", arrow=LAST) # y轴的x=画布宽的一半
# 标题文字
w.create_text(w0 + 50, 10, text='y=sin(x)')
# x轴刻度
for i in range(-3, 4):
j = i * 40
w.create_line(j + w0, h0, j + w0, h0 - 5, fill="red")
w.create_text(j + w0, h0 + 5, text=str(i))
# y轴刻度
for i in range(-2, 3):
j = i * 40
w.create_line(w0, j + h0, w0 + 5, j + h0, fill="red")
w.create_text(w0 - 10, j + h0, text=str(-i))
# 计算x
def x(t):
x = t * 40 # x轴放大40倍
x += w0 # 平移x轴
return x
# 计算y
def y(t):
y = math.sin(t) * 40 # y轴放大40倍
y -= h0 # 平移y轴
y = -y # y轴值反向
return y
# 连续绘制微直线
t = -math.pi
while (t < math.pi):
w.create_line(x(t), y(t), x(t + 0.01), y(t + 0.01), fill="blue")
t += 0.01
root.mainloop()
坐标轴的刻度线、刻度都是通过for循环来添加的
以及坐标轴的箭头啥的都能加