Python实例讲解 -- tkinter canvas (设置背景图片及文字)

先来一个绘图:

from Tkinter import *

master = Tk()

w = Canvas(master, width=200, height=100)

w.pack()

w.create_line(0, 0, 200, 100)

w.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))

w.create_rectangle(50, 25, 150, 75, fill="blue")

mainloop()

tk 默认处理图片格式为gif 处理其他格式的图片,否则需要下载image的mod,见附件

# -*- coding:utf-8 -*-

# file: TkinterCanvas.py

#

import Tkinter # 导入Tkinter模块

from PIL import Image, ImageTk

root = Tkinter.Tk()

canvas = Tkinter.Canvas(root,

width = 500, # 指定Canvas组件的宽度

height = 600, # 指定Canvas组件的高度

bg = 'white') # 指定Canvas组件的背景色

#im = Tkinter.PhotoImage(file='img.gif') # 使用PhotoImage打开图片

image = Image.open("img.jpg")

im = ImageTk.PhotoImage(image)

canvas.create_image(300,50,image = im) # 使用create_image将图片添加到Canvas组件中

canvas.create_text(302,77, # 使用create_text方法在坐标(302,77)处绘制文字

text = 'Use Canvas' # 所绘制文字的内容

,fill = 'gray') # 所绘制文字的颜色为灰色

canvas.create_text(300,75,

text = 'Use Canvas',

fill = 'blue')

canvas.pack() # 将Canvas添加到主窗口

root.mainloop()

参看: http://effbot.org/tkinterbook/canvas.htm#when-to-use

wxpython 的canvas

# create a canvas on top of a blank bitmap

# any canvas drawings can now be saved to a standard image file

# tested with Python27 and wxPython28 by vegaseat 05jan2011

import wx

class MyFrame(wx.Frame):

def __init__(self, parent=None, id=-1, title=None):

wx.Frame.__init__(self, parent, id, title, size=(380,400))

self.statbmp = wx.StaticBitmap(self)

self.draw_image()

self.save_image()

def draw_image(self):

# select the width and height of the blank bitmap

# should fit the client frame

w, h = 340, 340

# create the blank bitmap as a draw background

draw_bmp = wx.EmptyBitmap(w, h)

# create the canvas on top of the draw_bmp

canvas_dc = wx.MemoryDC(draw_bmp)

# fill the canvas white

canvas_dc.SetBrush(wx.Brush('white'))

canvas_dc.Clear()

# draw a bunch of circles ...

# pen colour

canvas_dc.SetPen(wx.Pen('red', 1))

# fill colour

canvas_dc.SetBrush(wx.Brush('yellow'))

for x in range(10, 180, 10):

y = x

r = x

canvas_dc.DrawCircle(x, y, r)

# now put the canvas drawing into a bitmap to display it

# remember the canvas is on top of the draw_bmp

self.statbmp.SetBitmap(draw_bmp)

def save_image(self):

"""save the drawing"""

finished_image = self.statbmp.GetBitmap()

#finished_image.SaveFile("mydrawing.png", wx.BITMAP_TYPE_PNG)

finished_image.SaveFile("mydrawing.jpg", wx.BITMAP_TYPE_JPEG)

app = wx.App(0)

MyFrame(title='canvas draw and save').Show()

app.MainLoop()

# help(wx.PaintDC)

python设置背景图片大全_Python实例讲解 - tkinter canvas (设置背景图片及文字)相关推荐

  1. python中mainloop添加背景_Python实例讲解 - tkinter canvas (设置背景图片及文字)

    Python实例讲解 -- tkinter canvas (设置背景图片及文字) 先来一个绘图: from Tkinter import * master = Tk() w = Canvas(mast ...

  2. python中mainloop添加背景_Python实例讲解 tkinter canvas (设置背景图片及文字)

    Python实例讲解 tkinter canvas (设置背景图片及文字) 2018-09-14 Python实例讲解 -- tkinter canvas (设置背景图片及文字) 博客分类:Pytho ...

  3. Python实例讲解 -- tkinter canvas (设置背景图片及文字)

    先来一个绘图: from Tkinter import *master = Tk()w = Canvas(master, width=200, height=100) w.pack()w.create ...

  4. python定时播放音乐程序_Python实例讲解 -- 定时播放 (闹钟+音乐)

    自己写的闹钟, 只可以播放wav格式的音频. import time import sys soundFile = 'sound.wav' not_executed = 1 def soundStar ...

  5. python分割数字_对python数据切割归并算法的实例讲解

    当一个 .txt 文件的数据过于庞大,此时想要对数据进行排序就需要先将数据进行切割,然后通过归并排序,最终实现对整体数据的排序.要实现这个过程我们需要进行以下几步:获取总数据行数:根据行数按照自己的需 ...

  6. python实现简单的api接口-对Python实现简单的API接口实例讲解

    get方法 代码实现 # coding:utf-8 import json from urlparse import parse_qs from wsgiref.simple_server impor ...

  7. python对abaqus本构二次开发_基于Python的Abaqus二次开发实例讲解

    第 1 页 共 11 页 基于 Python 的 Abaqus 二次开发实例讲解 ( asian58 2013.6.26 ) 基于 Python 的 Abaqus 的二次开发便捷之处在于: 1 .所有 ...

  8. 简单python脚本实例-对Python实现简单的API接口实例讲解

    get方法 代码实现 # coding:utf-8 import json from urlparse import parse_qs from wsgiref.simple_server impor ...

  9. python中numpy数组的合并_基于Python中numpy数组的合并实例讲解

    基于Python中numpy数组的合并实例讲解 Python中numpy数组的合并有很多方法,如 - np.append() - np.concatenate() - np.stack() - np. ...

最新文章

  1. Android开发:Kotlin下配置DataBinding
  2. TypeError: only integer scalar arrays can be converted to a scalar index
  3. 罗森伯格荣获2015年度中国数据中心优秀供应商与中国十大布线品牌两项大奖
  4. 如何去除计算机的访问限制,访问限制达成,教你如何禁止别人访问你电脑的C盘...
  5. 用c语言实现键盘画图,用C实现键盘画图.doc
  6. jQuery Mobile事件,开发全解+完美注释
  7. Atlassian JIRA 插件开发之三 创建
  8. linux运行natapp,Centos使用natapp教程
  9. 操作系统基础知识复习总结
  10. 【微信小程序】(一)创建项目与前端编写
  11. echarts 关系图 力引导布局
  12. 百度翻译API教程(完整Android代码)
  13. 2011年IT行业薪资调查报告
  14. 语言-汉语-官话:官话
  15. 网卡驱动卸载后一直安装失败的问题成功解决
  16. android拷贝设备断连接,android – Firebase Messaging Inactivity,断开与AppMeasurementService的连接[复制]...
  17. invalidate()和postInvalidate() 的区别及使用
  18. 银联电子支付 php chinapay
  19. python的str()字符串类型的方法详解
  20. amlogic调试系列(一)-芯片型号列表

热门文章

  1. LORA智能巡检手持机|无线数据采集终端
  2. 【Machine Learning, Coursera】机器学习Week6 偏斜数据集的处理
  3. mac pro M1(ARM)安装:python开发环境
  4. 建个网站需要多少钱?你真的知道吗?
  5. 搜搜问问做外链是一个误区
  6. 云服务器 重做系统软件,云服务器 重做系统软件
  7. 虚拟机ping通主机,但是ping不通百度
  8. Android开发——Android中的二维码生成与扫描
  9. 关于联想昭阳k20-80触摸屏关闭
  10. centos7使用救援模式备份数据