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

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

2018-09-14

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

博客分类:Python 2 / Wxpython

PythonwxPython

先来一个绘图:

Python代码

30398554_1.gif

30398554_2.png

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,见附件

Python代码

30398554_1.gif

30398554_2.png

# -*- 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

Java代码

30398554_1.gif

30398554_2.png

# 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)

Tkinter编程代码实例.pdf (639 KB)

下载次数: 47

image_PIL-1.1.7.win32-py2.7.tar (820 KB)

下载次数: 19

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。

http://www.pinlue.com/style/images/nopic.gif

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值