先来一个绘图:
- 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,见附件
-
-
-
- import Tkinter
- from PIL import Image, ImageTk
-
- root = Tkinter.Tk()
- canvas = Tkinter.Canvas(root,
- width = 500,
- height = 600,
- bg = 'white')
-
- image = Image.open("img.jpg")
- im = ImageTk.PhotoImage(image)
-
- canvas.create_image(300,50,image = im)
- canvas.create_text(302,77,
- text = 'Use Canvas'
- ,fill = 'gray')
- canvas.create_text(300,75,
- text = 'Use Canvas',
- fill = 'blue')
- canvas.pack()
- root.mainloop()
参看: http:///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)