python-菜鸟教程-tkinter画布的坐标

#!/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循环来添加的

以及坐标轴的箭头啥的都能加

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值