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中mainloop添加背景_Python实例讲解 - tkinter canvas (设置背景图片及文字)相关推荐

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

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

  2. python设置背景图片大全_Python实例讲解 - tkinter canvas (设置背景图片及文字)

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

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

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

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

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

  5. python中doc=parased.getroot()_python实例手册.py

    python实例手册 #encoding:utf8 # 设定编码-支持中文 0 说明 手册制作: 雪松 littlepy www.51reboot.com 更新日期: 2016-01-21 欢迎系统运 ...

  6. python中mainloop什么意思_Python Turtle mainloop()用法

    我有一个online tutorial的以下代码来学习基于事件的编程,方法是在点击鼠标时使停止灯改变状态.这里是我的代码的全部:Python Turtle mainloop()用法 import tu ...

  7. python为text添加滚动条_Python GUI编程(Tkinter)7、带滚动条的Text

    import tkinter #创建主窗口 win = tkinter.Tk() #设置标题 win.title("sunck") #设置大小和位置 #win.geometry(& ...

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

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

  9. python中如何添加名片_Python的名片

    开头:本文是送给还在python门口前犹豫是否进来的小白们的,已经知道python是干什么的同学请回啦~ 目录python的历史 python的优缺点 python在当今的适用领域 python的发展 ...

最新文章

  1. python输入文字字符串、如何提取某个汉字_python提取字符串中的汉字数字字母
  2. Java连接Memcached进行CRUD
  3. hdfs web_ui深入讲解、服务启动日志分析、NN SNN关系
  4. python3 规则引擎_几个常见规则引擎的简单介绍和演示
  5. [导入]心平气和,千佳骈集;意粗性躁,一事无成
  6. 海豚php表格,表格选取(1.4.3+) · DolphinPHP1.5.0完全开发手册-基于ThinkPHP5.1.41LTS的快速开发框架 · 看云...
  7. jsp form提交到后台中文乱码_JSP与servlet之间的数据传递
  8. 解决file.seek()读取文件报错:AttributeError: ‘str‘ object has no attribute ‘seek‘
  9. linux svn启动失败,linux svn authorization failed错误
  10. C++socket编程(八):8.2简单的基于UDP的客户端和服务端
  11. 何时、何地应用何种窗函数?
  12. git 查看和修改用户名及邮箱
  13. 手写Vue个人组件库——fl-Badge
  14. 中南大学计算机学院楠,中南比湖大更湖大,不对,应该说湖大没中南大学中南。...
  15. 【Rust日报】2022-10-12 国内物联网芯片厂商发布世界上第一款 rust 芯片支持库
  16. 使用python打印九九乘法表
  17. ubuntu自动重连l2tp并转发
  18. 大牛讲解信号与系统以及数字信号处理
  19. 目标检测——day66 Scaled-YOLOv4: Scaling Cross Stage Partial Network
  20. *用jQuery实现星星好评wink*~

热门文章

  1. python漏洞检测脚本_URL重定向漏洞,python打造URL重定向漏洞检测脚本
  2. 8k分辨率需要多大带宽_又一支持8K分辨率的接口标准发布
  3. 抓娃娃机爪不动怎么办_黄子韬吃娃娃菜能把临时牙咬断?种植牙到底结实不结实?...
  4. v-for中用elementUI实现分页
  5. Python应用实战-如何用Pyecharts绘制可视化地图?
  6. body curl 设置post_curl 命令详解
  7. rhel 8.2不识别unicode_基于tensorflow 实现端到端的OCR:二代身份证号识别
  8. getresource 路径转义_java中的相对路径和绝对路径
  9. java中大数开方_大数开方(Java版)
  10. 前端 PDFObject.embed预览PDF,另类方式隐藏工具条样例