设为首页 收藏本站
查看: 1800|回复: 0

[经验分享] Python实例讲解 -- tkinter canvas (设置背景图片及文字)

[复制链接]

尚未签到

发表于 2017-5-5 12:47:12 | 显示全部楼层 |阅读模式
先来一个绘图:

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)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373499-1-1.html 上篇帖子: Python中调用父类的同名方法 下篇帖子: 编程语言排行榜 Python 或成黑马
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表