码迷,mamicode.com
首页 > 其他好文 > 详细

Tkinter canvas-01

时间:2021-06-03 17:57:11      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:import   line   边框颜色   技术   http   xxx   宽度   let   画圆   

  • 常用参数和函数
fill = xxx  # 填充xxx颜色
width = xxx  # 设置宽度或边框宽度
outline = orange  # 设置边框的颜色
canvas.itemcget(name, xxx)  # 获取组件name的xxx属性
canvas.coords(name)  # 获取组件的横坐标和纵坐标
canvas.coords(name, x1, y1, x2, y2)  # 重新设置组件的横坐标和纵坐标
  • 绘制实线和虚线
from tkinter import *

win = Tk()
win.geometry(500x500+500+100)
canvas = Canvas(win)
canvas.pack(fill=BOTH, expand=True)
# 画实线,填充橙色
canvas.create_line(100, 100, 100, 200, fill=orange)
# 画实线,填充橙色,设置宽度为10
canvas.create_line(150, 100, 150, 200, fill=orange, width=10)
# 画虚线,填充橙色
canvas.create_line(200, 100, 200, 200, fill=orange, dash=(2, 2))
win.mainloop()

技术图片

  • 绘制矩形
from tkinter import *

win = Tk()
win.geometry(500x500+500+100)
canvas = Canvas(win)
canvas.pack(fill=BOTH, expand=True)
# 画矩形,填充橙色
canvas.create_rectangle(100, 100, 200, 200, fill=orange)
# 画矩形,填充橙色,设置边框宽度为10
canvas.create_rectangle(250, 100, 350, 200, fill=orange, width=10)
# 画矩形,填充橙色,设置边框是红色
canvas.create_rectangle(400, 100, 450, 200, fill=orange, outline=red, width=2)
win.mainloop()

技术图片

  • 绘制文字
from tkinter import *

win = Tk()
win.geometry(500x500+500+100)
canvas = Canvas(win)
canvas.pack(fill=BOTH, expand=True)
# 画文字
canvas.create_text(100, 100, text=文字)
# 画文字,填充橙色
canvas.create_text(200, 100, text=文字, fill=orange)
win.mainloop()

技术图片

  •  绘制圆和椭圆
from tkinter import *

win = Tk()
win.geometry(500x500+500+100)
canvas = Canvas(win)
canvas.pack(fill=BOTH, expand=True)
# 画圆,填充橙色
canvas.create_oval(100, 100, 200, 200, fill=orange)
# 画椭圆,填充橙色,设置边框宽度5,边框颜色pink
canvas.create_oval(200, 200, 300, 350, fill=orange, width=5, outline=pink)

win.mainloop()

技术图片

  • 绘制多边形
from tkinter import *

win = Tk()
win.geometry(500x500+500+100)
canvas = Canvas(win)
canvas.pack(fill=BOTH, expand=True)
# 绘制多边形,填充粉色
point = [(50, 50), (100, 50), (100, 100), (75, 150), (50, 100)]
canvas.create_polygon(point, fill=pink, width=5, outline=orange)
win.mainloop()

技术图片

  • 获取组件的属性
from tkinter import *

win = Tk()
win.geometry(500x500+500+100)
canvas = Canvas(win)
canvas.pack(fill=BOTH, expand=True)
# 画实线,填充橙色,宽度为10
line = canvas.create_line(150, 100, 150, 200, fill=orange, width=10)


def event():
    # 使用 itemcget 来获取组件的属性
    fill = canvas.itemcget(line, fill)
    print(fill)
    width = canvas.itemcget(line, width)
    print(width)


btn = Button(win, text=点击获取实线属性, command=event)

canvas.create_window((100, 50), window=btn)
win.mainloop()

技术图片

  • 重新设置组件位置和组件属性
from tkinter import *

win = Tk()
win.geometry(500x500+500+100)
canvas = Canvas(win)
canvas.pack(fill=BOTH, expand=True)
# 画实线,填充橙色,宽度为10
line = canvas.create_line(150, 100, 150, 200, fill=orange, width=10)


def move_line():
    # 使用 coords 来重新设置组件的位置
    canvas.coords(line, 200, 100, 200, 200)


btn = Button(win, text=点击移动实线, command=move_line)
# 使用 itemconfig 来重新设置组件的属性
btn1 = Button(win, text=点击切换实线填充颜色, command=lambda: canvas.itemconfig(line, fill=pink))
canvas.create_window((100, 50), window=btn)
canvas.create_window((300, 50), window=btn1)
win.mainloop()

技术图片

  •  移动组件
from tkinter import *

win = Tk()
win.geometry(500x500+500+100)
canvas = Canvas(win)
canvas.pack(fill=BOTH, expand=True)
# 画实线,填充橙色,宽度为10
line = canvas.create_line(150, 100, 150, 200, fill=orange, width=10)


def move_line():
    # 使用 coords 来重新设置组件的位置
    canvas.coords(line, 200, 100, 200, 200)


btn = Button(win, text=点击移动实线, command=move_line)
# 使用 move 移动组件,参数表示沿XY轴移动的距离
btn1 = Button(win, text=点击移动实线2, command=lambda: canvas.move(line, 10, 0))
canvas.create_window((100, 50), window=btn)
canvas.create_window((300, 50), window=btn1)
win.mainloop()

技术图片

  •  删除组件
from tkinter import *

win = Tk()
win.geometry(500x500+500+100)
canvas = Canvas(win)
canvas.pack(fill=BOTH, expand=True)
# 画实线,填充橙色
line = canvas.create_line(100, 100, 100, 200, fill=orange)
# 画实线,填充橙色,宽度为10
canvas.create_line(150, 100, 150, 200, fill=orange, width=10)
# 画虚线,填充橙色
canvas.create_line(200, 100, 200, 200, fill=orange, dash=(2, 2))
# 使用delete来删除组件
canvas.create_window((250, 50), window=Button(text=删除橙色实线,
                                              command=lambda x=canvas: x.delete(line)))
# all 代表画布的所有组件
canvas.create_window((150, 50), window=Button(text=删除画布所有组件,
                                              command=lambda x=canvas: x.delete(all)))
win.mainloop()

 

Tkinter canvas-01

标签:import   line   边框颜色   技术   http   xxx   宽度   let   画圆   

原文地址:https://www.cnblogs.com/rainbow-tan/p/14844097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!