分享

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

 昵称QAb6ICvc 2013-02-19

先来一个绘图:

 

Python代码 复制代码 收藏代码
  1. from Tkinter import *   
  2.   
  3. master = Tk()   
  4.   
  5. w = Canvas(master, width=200, height=100)   
  6. w.pack()   
  7.   
  8. w.create_line(00200100)   
  9. w.create_line(01002000, fill="red", dash=(44))   
  10.   
  11. w.create_rectangle(502515075, fill="blue")   
  12.   
  13. mainloop()  
 

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

 

Python代码 复制代码 收藏代码
  1. # -*- coding:utf-8 -*-   
  2. # file: TkinterCanvas.py   
  3. #   
  4. import Tkinter         # 导入Tkinter模块   
  5. from PIL import Image, ImageTk   
  6.   
  7. root = Tkinter.Tk()   
  8. canvas = Tkinter.Canvas(root,   
  9.     width = 500,      # 指定Canvas组件的宽度   
  10.     height = 600,      # 指定Canvas组件的高度   
  11.     bg = 'white')      # 指定Canvas组件的背景色   
  12. #im = Tkinter.PhotoImage(file='img.gif')     # 使用PhotoImage打开图片   
  13. image = Image.open("img.jpg")   
  14. im = ImageTk.PhotoImage(image)   
  15.   
  16. canvas.create_image(300,50,image = im)      # 使用create_image将图片添加到Canvas组件中   
  17. canvas.create_text(302,77,       # 使用create_text方法在坐标(302,77)处绘制文字   
  18.    text = 'Use Canvas'      # 所绘制文字的内容   
  19.    ,fill = 'gray')       # 所绘制文字的颜色为灰色   
  20. canvas.create_text(300,75,   
  21.    text = 'Use Canvas',   
  22.    fill = 'blue')   
  23. canvas.pack()         # 将Canvas添加到主窗口   
  24. root.mainloop()  
 

 

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

 

wxpython 的canvas

 

Java代码 复制代码 收藏代码
  1. # create a canvas on top of a blank bitmap   
  2. # any canvas drawings can now be saved to a standard image file   
  3. # tested with Python27 and wxPython28  by vegaseat  05jan2011   
  4.   
  5. import wx   
  6.   
  7. class MyFrame(wx.Frame):   
  8.     def __init__(self, parent=None, id=-1, title=None):   
  9.         wx.Frame.__init__(self, parent, id, title, size=(380,400))   
  10.         self.statbmp = wx.StaticBitmap(self)   
  11.         self.draw_image()   
  12.         self.save_image()   
  13.   
  14.     def draw_image(self):   
  15.         # select the width and height of the blank bitmap   
  16.         # should fit the client frame   
  17.         w, h = 340340  
  18.         # create the blank bitmap as a draw background   
  19.         draw_bmp = wx.EmptyBitmap(w, h)   
  20.         # create the canvas on top of the draw_bmp   
  21.         canvas_dc = wx.MemoryDC(draw_bmp)   
  22.         # fill the canvas white   
  23.         canvas_dc.SetBrush(wx.Brush('white'))   
  24.         canvas_dc.Clear()   
  25.   
  26.         # draw a bunch of circles ...   
  27.         # pen colour   
  28.         canvas_dc.SetPen(wx.Pen('red'1))   
  29.         # fill colour   
  30.         canvas_dc.SetBrush(wx.Brush('yellow'))   
  31.         for x in range(1018010):   
  32.             y = x   
  33.             r = x   
  34.             canvas_dc.DrawCircle(x, y, r)   
  35.   
  36.         # now put the canvas drawing into a bitmap to display it   
  37.         # remember the canvas is on top of the draw_bmp   
  38.         self.statbmp.SetBitmap(draw_bmp)   
  39.   
  40.     def save_image(self):   
  41.         """save the drawing"""  
  42.         finished_image = self.statbmp.GetBitmap()   
  43.         #finished_image.SaveFile("mydrawing.png", wx.BITMAP_TYPE_PNG)   
  44.         finished_image.SaveFile("mydrawing.jpg", wx.BITMAP_TYPE_JPEG)   
  45.   
  46.   
  47. app = wx.App(0)   
  48. MyFrame(title='canvas draw and save').Show()   
  49. app.MainLoop()   
  50.   
  51. # help(wx.PaintDC)  
 

 

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多