python2的tkinter模块_[Python basics]基于Tkinter模块的GUI(2)-画布,入门,基础,tkinter,二,canvas...

canvas画图

方法

说明

参数

.create_line()

画线

起点坐标,终点坐标,width=线宽,fill=颜色

.create_arc()

画弧线

起点坐标,终点坐标,width=线宽,fill=颜色

.create_rectangle()

画矩形

起点坐标,终点坐标,fill=‘填充的颜色’,outline=边框颜色

.create_oval()

画椭圆

外界矩阵的左上角和右小角坐标,fill=‘填充的颜色’,outline=边框颜色

.create_polygon()

画多边形

多点坐标,fill=‘填充的颜色’,outline=边框颜色

.create_text()

显示文字

text=‘文字’

.delete()

删除对象

.move()

移动

(参数,0,2)横向不移动,纵向移动2

.update()

更新

当应用程序的状态发生变化和需要这些变化立刻显示时,不用等待脚本完成

.pack()

包装

eg.canvas画布

import tkinter as tk

win = tk.Tk()

win.geometry("600x400+200+100")

canvas = tk.Canvas(win, bg="white")

canvas.place(relx=0.05, rely=0.05, relwidth=0.9, relheight=0.9)

# 画一条实线, fill:填充的颜色

line1 = canvas.create_line((0, 10), (50, 50), width=5, fill="green")

# 画一条虚线 dash=(1, 1)

canvas.create_line((200, 200), (200, 300), width=5, fill="green", dash=(1, 1))

# 画一个圆弧

canvas.create_arc((100, 100), (200, 200), width=5)

# 显示文字

canvas.create_text((300, 100), text="canvas画布", font=("微软雅黑", 18))

# 绘制矩形, outline:线条颜色

canvas.create_rectangle(50, 25, 150, 75, fill='blue', outline='green', width=5)

# 绘制椭圆

canvas.create_oval(200, 25, 350, 75, fill='pink', outline='green', width=5)

# 绘制多边形

point = [(0, 0), (150, 200), (200, 300), (300, 450), (450, 550)]

canvas.create_polygon(point, outline='green', fill='yellow')

def dele_line():

canvas.delete(line1)

btn = tk.Button(canvas, text="删除", command=dele_line)

btn.place(relx=0.4, rely=0.8)

win.mainloop()

运行结果:

eg.五子棋盘

import tkinter

def mouse_evt_handler(evt=None):

print(evt.x,evt.y)

# margin=20

row = round((evt.y - 20) / 40)

col = round((evt.x - 20) / 40)

pos_x = 40 * col

pos_y = 40 * row

print(pos_x, pos_y, 40 + pos_x, 40 + pos_y)

# 圆外矩形左上角与右下角坐标

canvas.create_oval(pos_x, pos_y, 40 + pos_x, 40 + pos_y, fill='black')

top = tkinter.Tk()

# 设置窗口尺寸

top.geometry('620x620')

# 设置窗口标题

top.title('五子棋')

# 设置窗口大小不可改变

top.resizable(False, False)

# 设置窗口置顶

top.wm_attributes('-topmost', 1)

canvas = tkinter.Canvas(top, width=600, height=600, bd=0, highlightthickness=0)

# 鼠标左键绑定点击事件

canvas.bind('', mouse_evt_handler)

canvas.create_rectangle(0, 0, 600, 600, fill='b99e20', outline='white')

for index in range(15):

canvas.create_line(20, 20 + 40 * index, 580, 20 + 40 * index, fill='black')

canvas.create_line(20 + 40 * index, 20, 20 + 40 * index, 580, fill='black')

canvas.create_rectangle(15, 15, 585, 585, outline='black', width=4)

canvas.pack()

tkinter.mainloop()

运行结果:

说明:

五子棋对弈过程待实现。

eg.动态动画

import tkinter

import time

# 播放动画效果的函数

def play_animation():

canvas.move(oval, 2, 2)

canvas.update()

top.after(50, play_animation)

x = 10

y = 10

top = tkinter.Tk()

top.geometry('600x600')

top.title('动画效果')

top.resizable(False, False)

top.wm_attributes('-topmost', 1)

canvas = tkinter.Canvas(top, width=600, height=600, bd=0, highlightthickness=0)

canvas.create_rectangle(0, 0, 600, 600, fill='#beee99')

oval = canvas.create_oval(10, 10, 60, 60, fill='yellow')

canvas.pack()

top.update()

play_animation()

tkinter.mainloop()

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值