【python之tkinter画布】

本文代码来源于机械工业出版社的一本python书籍.

要画布就要使用tkinter的小构件,canvas(结构化的图形,用于绘制图形,创建图形编辑器以及实现自定制的小构件类)

我们先使用create_rectangle, create_oval, create_arc, create_polygon, create_line分别绘制矩形,椭圆,圆弧,多边形,线段。

创建CanvasDemo.py的文件,代码如下:

# -*- coding: utf-8 -*-
###################
#画布,使用Canvas小构件
###################
from Tkinter import * class CanvasDemo:
def __init__(self):
window = Tk() #创建窗口
window.title("Canvas Demo") #给窗口命名 #在窗口画布
self.canvas = Canvas(window, width = 200, height = 100, bg = "white")
self.canvas.pack() #创建frame的框架,窗口window为这个框架的父容器
frame = Frame(window)
frame.pack()
#frame框架作为Button的父容器
btRectangle = Button(frame, text="rectangle", command = self.displayRect)
btOval = Button(frame, text = "Oval", command = self.displayOval)
btArc = Button(frame, text = "Arc", command = self.displayArc)
btPolygon = Button(frame, text = "Polygon", command = self.displayPolygon)
btLine = Button(frame, text = "Line", command = self.displayLine)
btString = Button(frame, text = "String", command = self.displayString)
btClear = Button(frame, text = "Clear", command = self.displayClear) #Button在画布上布局
btRectangle.grid(row = 1, column = 1)
btOval.grid(row = 1, column = 2)
btArc.grid(row = 1, column = 3)
btPolygon.grid(row = 1, column = 4)
btLine.grid(row = 1, column = 5)
btString.grid(row = 1, column = 6)
btClear.grid(row = 1, column = 7) #创建事件循环直到关闭主窗口
window.mainloop() def displayRect(self):
self.canvas.create_rectangle(10,10,190,90,tags = "rect") #fill填充oval的颜色
def displayOval(self):
self.canvas.create_oval(10,10,190,90, fill = "red", tags = "oval") # start为开始的度数,extent为要转的度数.全部以逆时针为正方向,0为x轴正方向
def displayArc(self):
self.canvas.create_arc(10,10,190,90, start = 0, extent = 90, width = 8, fill = "red",tags = "arc") def displayPolygon(self):
self.canvas.create_polygon(10,10,190,90,10,90,tags = "polygon") #arrow表示line指向,activefill:当鼠标在line上时出现的特定风格,本例中鼠标移动到第二个line上时line变蓝
def displayLine(self):
self.canvas.create_line(10,10,190,90,fill = "red",tags = "line")
self.canvas.create_line(10,90,190,10,width = 9,arrow = "first",activefill = "blue", tags = "line") #font定义字体(字体名,大小,风格)
def displayString(self):
self.canvas.create_text(60,40,text= "hi, i am string", font = "time 10 bold underline", tags = "string") #delete方法通过tags参数从画布上删除图形
def displayClear(self):
self.canvas.delete("rect","oval","arc","polygon","line","string") CanvasDemo()

运行程序即可。

可能你不知道create_xx()里的数字意思,其实是坐标(x1,y1),(x2,y2),(x3,y3)create_xx(x1,y1,x2,y2,x3,y3)

Tkinter的坐标系是这样的:

2018.4.25更新: 经过Pangolin2 的提醒上图错了, 在python3.6中x为横向(向右为正方向), y为纵向(向下为正方向), 偷个懒, 图就不改了哈.

【python基础】 Tkinter小构件之canvas 画布的更多相关文章

  1. 微信小程序 在canvas画布上划动,页面禁止滑动

    要实现微信小程序 在canvas画布上划动,页面禁止滑动,不仅要设置disable-scroll="true",还要要给canvas绑定一个触摸事件才能生效. <canvas ...

  2. 小程序开发-Canvas画布组件

    Canvas画布 基本使用方法: 在wxml中添加canvas组件 <canvas canvas-id='canvasDemo' class='demo'></canvas> ...

  3. python基础之小数据池、代码块、编码和字节之间换算

    一.代码块.if True: print(333) print(666) while 1: a = 1 b = 2 print(a+b) for i in '12324354': print(i) 虽 ...

  4. python基础之小数据池、代码块、编码

    一.代码块.if True: print(333) print(666) while 1: a = 1 b = 2 print(a+b) for i in '12324354': print(i) 虽 ...

  5. Python基础--01小项目体现的基础知识

    part1:猜拳游戏 #coding=utf-8 #当有汉语时可能编译器不认识,需要定义代码 ''' 多行注释 写这个程序是为了熟悉python的基本语法 这是第一个小例子包含简单的if判断,循环和输 ...

  6. python基础语法小笔记

    这几天看着python,然后就记下一些自己觉得需要注意以下的基础语法吧! 如下: for i in range(0,100)表示从0到99,不包括后边界 单引号(')和双引号("" ...

  7. python基础之小数据池

    一,id,is,== 在Python中,id是什么?id是内存地址,比如你利用id()内置函数去查询一个数据的内存地址: name = '太白' print(id(name)) # 158583128 ...

  8. python基础之小数据池,is和==区别 编码问题

    主要内容 小数据池,is和==区别 编码问题 小数据池 一种缓存机制,也称为驻留机制,是为了能更快提高一些字符串和整数的处理速度is 和 == 的区别 == 主要指对变量值是否相等的判断,只要数值相同 ...

  9. Python基础=== Tkinter Grid布局管理器详解

    本文转自:https://www.cnblogs.com/ruo-li-suo-yi/p/7425307.html          @ 箬笠蓑衣 Grid(网格)布局管理器会将控件放置到一个二维的表 ...

随机推荐

  1. PAT (Advanced Level) 1024. Palindromic Number (25)

    手动模拟加法高精度. 注意:如果输入数字的就是回文,这个时候输出0步. #include<iostream> #include<cstring> #include<cma ...

  2. BNU OJ 50998 BQG's Messy Code

    #include <cstdio> #define _(l) int l #define ___(l,L) for (_(o)=0,x=l o*2;o<x;o++)O= L o; # ...

  3. Python tab 命令补全,以及 vim 补全

    在python 命令行中,使用补全 python 查看 packages 的目录 可用 sys.path 查看. /usr/lib/python2.7/site-packages vim tab.py ...

  4. 关于val(),text(),html()的用法

    直接上demo: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://ww ...

  5. BigDecimal 高精度计算 熟悉扩展,java除法保留小数问题

    java保留两位小数问题: 方式一: 四舍五入  double   f   =   111231.5585;  BigDecimal   b   =   new   BigDecimal(f);  d ...

  6. LNMP的安装

    一.安装Linux 安装某个linux桌面版系统,基本是ubuntu即可. 安装必要的库,如:pcre.xml.openssl.zlib等,sudo apt-get install libpcre3 ...

  7. JS表单原生验证器

    一.前言 最近在开发一个新项目,需要做登陆等一系列的表单提交页面.在经过“缜密”的讨论后,我们决定 不用外部流行的框架,如bootstrap,由于我负责的模块 仅仅是其中的一部分,因此少数服从多数,无 ...

  8. JS中Exception处理

    程序开发中,编程人员经常要面对的是如何编写代码来响应错误事件的发生,即例外处理(exception handlers).如果例外处理代码设计得周全,那么最终呈现给用户的就将是一个友好的界面.否则,就会 ...

  9. 比较实用的webpack配置代码

    var path = require('path');var webpack = require('webpack');var ExtractTextPlugin = require('extract ...

  10. VS2010环境下用ANSI C创建DLL和使用方法(转)

    源:VS2010环境下用ANSI C创建DLL和使用方法 . 创建DLL工程 1.2 创建一个dll工程. 操作:a.文件->新建->项目->Win32控制台应用程序. b.输入工程 ...