python tkinter窗口组件Canva

import time
import tkinter
from threading import Thread

# 主窗口的类,使用root接收一下
root = tkinter.Tk()
# 标题
root.title("My table1")
# 像素
width, height = 300, 600
# 获得屏幕的最大尺寸
width_max, height_max = root.maxsize()
# 使页面显示在屏幕正中间
s_center = '%dx%d+%d+%d' % (width, height, (width_max - width) / 2, (height_max - height) / 2)
print(s_center)
# 设置屏幕大小
root.geometry(s_center)
# 如果是true代表可以拉伸 false 不可拉伸
root.resizable(width=False, height=False)

# 画布对象
c = tkinter.Canvas(root, width=width, height=400, bg='yellow')
c.place(x=0, y=0)


# 绘制线条 create_line()
# 绘制线条 x, y,x1,y1 (x, y)代表起始点,(x1,y1)代表结束点,然后相连变成直线
# fill 是填充颜色
# 简单的说绘制一条线条需要知道2头端点即可连成一条线,绘制也是同理需要知道2端的坐标值
# 相信大家也学过坐标轴坐标原点即是(0,0)也即是画布的左上角
# create_line()画布对象绘制线条的方法需要知道2端的坐标


def draw_line():
    # create_line(x,y,x1,y1)
    # fill='red’代表线条的填充颜色是红色 width=2代表粗2个像素的单位
    c.create_line(10, 0, 10, 400, fill='red', width=2)
    # dash=(2,1)代表2个像素的短线和1个像素的间隔
    c.create_line(20, 0, 20, 400, fill='red', dash=(2, 1))


# 绘制线条的按钮
tkinter.Button(root, text='绘制线条', command=draw_line).place(x=10, y=420)


def draw_lines_blue():
    for y in range(20, 400, 20):
        c.create_line(0, y, width, y, fill='blue')


def draw_lines_red():
    for x in range(20, 300, 20):
        c.create_line(x, 0, x, 400, fill='red')


tkinter.Button(root, text="绘制蓝色线条", command=draw_lines_blue).place(x=95, y=420)
tkinter.Button(root, text="绘制红色线条", command=draw_lines_red).place(x=200, y=420)

# 绘制矩形框create_rectangle()
# 需要知道矩形的左上角和右下角的坐标然后绘制成一个矩形通过矩形绘制一个内切圆
# outline='red’代表矩形的外框颜色,width=8代表矩形的外框线条的粗细程度为8像素
# fill='white ’代表填充的背景色是白色 outline='Tan'代表外框线条是茶色
# 绘制一个长方形 160, 160 代表一个坐标  250, 350 代表另一个坐标,两个坐标绘制正方形
c.create_rectangle(160, 160, 250, 350, fill="white", outline='Tan', width=1)

# 绘制圆形 c.create_oval()
# 圆形也是在矩形框的基础上绘制的一个圆形,如果是正方形的矩形则是正圆,其他则是椭圆
c.create_oval(10, 10, 150, 150, fill='SlateGray', outline='red', width=1)

# 绘制一个椭圆
c.create_oval(160, 160, 250, 350, fill='SlateGray', outline='red', width=1)


# 以圆形相切的4条直线


def draw_oval_line():
    c.create_line(10, 0, 10, 400, fill='DarkTurquoise')
    c.create_line(0, 10, 300, 10, fill='DarkTurquoise')
    c.create_line(150, 0, 150, 400, fill='DarkTurquoise')
    c.create_line(0, 150, 300, 150, fill='DarkTurquoise')


tkinter.Button(root, text='绘制4条切线', command=draw_oval_line).place(x=10, y=470)


# 清空所有画布对象


def del_all():
    c.delete(tkinter.ALL)


tkinter.Button(root, text='删除画布对象', command=del_all).place(x=95, y=470)
# 绘制多边形 create_polygon()等腰三角形(150,150,100,260,280,2.0)+矩形的形状(100,200,10,258,290,250,20,20)
# c.create_polygon(150, 150, 100, 200, 200, 200, fill='Tan', outline='black')
# c.create_rectangle(100, 200, 200, 250, fill='MintCream')
c.create_polygon(150, 150, 100, 200, 100, 250, 200, 250, 200, 200
                 , fill='red')

# 绘制文字 create_text()
# (180,40)坐标 text='文本内容’要显示的文字 font=('宋体',16)字体
# 的相关设置anchor=tkinter.W文字的对其方式:左对齐 fill填充颜色
c.create_text(180, 40, text='文本内容', font=('宋体', 16)
              , anchor=tkinter.W, fill='red')

# 绘制图片对象
# image = tkinter.PhotoImage(file="../src/xiaolan.gif")
# c.create_image(10, 330, image=image, anchor=tkinter.W)

# 绘制弧形 start可以改变角度 360 度是一条线   start=-90开始顺时针旋转90度不改变原大小extent
# c.create_rectangle(10, 250, 120, 400, fill='red')

c.create_arc(10, 250, 120, 400)
c.create_arc(10, 250, 120, 400, extent=359)
c.create_arc(10, 250, 120, 400, start=-90)


# 通过 线程+时间模块 搭配看演示效果
def fun():
    Thread(target=run_arc).start()


# 弧形start效果
def run_arc():
    for i in range(-1, -360, -1):
        t = c.create_arc(10, 250, 120, 400, start=i, fill='white')
        time.sleep(0.05)
        c.delete(t)


tkinter.Button(root, text='弧形走动效果', command=fun).place(x=200, y=470)

root.mainloop()






import time
import tkinter
from threading import Thread

# 主窗口的类,使用root接收一下
root = tkinter.Tk()
# 标题
root.title("My table1")
# 像素
width, height = 300, 600
# 获得屏幕的最大尺寸
width_max, height_max = root.maxsize()
# 使页面显示在屏幕正中间
s_center = '%dx%d+%d+%d' % (width, height, (width_max - width) / 2, (height_max - height) / 2)
print(s_center)
# 设置屏幕大小
root.geometry(s_center)
# 如果是true代表可以拉伸 false 不可拉伸
root.resizable(width=False, height=False)

# 生成画布对象
c = tkinter.Canvas(root, width=width, height=400, bg='yellow')
c.place(x=0, y=0)

# 画布对象的对于对象的编辑方法
# move(绘制的对象,X, y)移动画布上绘制的对象×代表便宜的大小和方向,6,-9代表向右偏移6像荼,像上偏移9像素#coords(绘制的对象,x, y ,x1,y1)可以改变原有对象的位置和大小
# delete(绘制对象)删除1个对象的方法
# delete(绘制对象)删除1个对象的方法
# c.delete(tkinter.ALL)删除所有对象方法
# c.itemconfig()修改内部一些参数值比如颜色啊线宽等

rect = c.create_rectangle(50, 50, 160, 160, fill='white', outline='blue',
                          width=2)
print('画布上的新建对象', rect)

# 绘制了方形边上4个线条便于观察
c.create_line(0, 50, 300, 50, fill='red', width=2)
c.create_line(0, 160, 300, 160, fill='red', width=2)
c.create_line(50, 0, 50, 400, fill='red', width=2)
c.create_line(160, 0, 160, 400, fill='red', width=2)


def fun():
    Thread(target=move_rect).start()


# 移动方法 move 接收对象,和坐标
def move_rect():
    for i in range(0, 25):
        c.move(rect, 10, 0)
        time.sleep(0.2)


# 可以改变原有对象的位置大小
# 可以改变选定图像的大小和位置
def coords_rect():
    c.coords(rect, 10, 30, 150, 250)


# delete(绘制对象)删除1个对象的方法
# c.delete(tkinter.ALL)删除所有对象方法
def del_rect():
    # c.delete(rect)
    c.delete(tkinter.ALL)


# 修改内部一些参数 比如颜色 线宽等
def config_rect():
    c.itemconfig(rect, fill='Tan', outline='red', width=4)


var = tkinter.StringVar()
var.set('移动方法')
# var.set('改变形态和位置方法')
# var.set('删除的方法')
# var.set('修改设置')
tkinter.Button(root, textvariable=var, command=fun).place(x=10, y=420)
root.mainloop()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lanlnan抱抱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值