在Python 2中绘制基本形状 [英] Drawing basic shapes in python 2

查看:217
本文介绍了在Python 2中绘制基本形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在python中绘制一个简单的部分透明的矩形。我想不从互联网上下载任何东西,并纯粹使用python 2.7.3。我也想控制矩形开始和结束的位置,并控制它的宽度和高度。最终目标是建立一张密歇根湖及其大湖泊的地图,并在海岸线上出现颜色编码矩形,以便根据NDBV的浮标数据直观地显示预期的天气情况。所以简而言之,我可以在上面放置彩色编码矩形的地图,这些地图将沿着西密歇根州的海岸线定向。 解决方案

这是一些Tkinter示例代码。请注意,据我所知,Tkinter不支持透明度;但它可以通过点画来伪造,如下所示:

 #从http://www.kosbie.net/cmu复制/fall-10/15-110/koz/misc-demos/src/semi-transparent-stipple-demo.py 

#semi-transparent-stipple-demo.py
#note :点画仅适用于某些对象(如矩形)
#而不是其他(如椭圆形)。但它总比没有好...... Tkinter导入的

*

def redrawAll(canvas):
canvas.delete(ALL)
#draw
canvas.create_rectangle(0,0,250,600,fill =red)
#在中间绘制半透明矩形
canvas.create_rectangle( 200,75,300,125,fill =blue,stipple =)
canvas.create_rectangle(200,175,300,225,fill =blue,stipple =gray75)
canvas.create_rectangle(200,275,300,325,fill =blue,stipple =gray50)
canvas.create_rectangle(200,375,300,425,fill =blue,stipple = grey25)
canvas.create_rectangle(200,475,300,525,fill =blue,stipple =gray12)

def init(画布):
redrawAll (canvas)

###########复制粘贴在这里下面###########

def run() :
#创建根和画布
root = Tk()
canvas = Canvas(root,width = 500,height = 600)
canvas.pack()
#Sto在根和画布本身中重新画布以获得回调
root.canvas = canvas.canvas = canvas
#设置画布数据并调用init
canvas.data = {}
init (canvas)
#设置事件
#root.bind(< Button-1>,mousePressed)
#root.bind(< Key>,keyPressed)
#timerFired(canvas)
#并启动应用程序
root.mainloop()#这个调用BLOCKS(所以你的程序等待,直到你关闭窗口!)

run()

产生类似于


How would I draw a simple partially transparent rectangle in python. I would like to not download anything from the internet and purely use python 2.7.3. I would like to also control where the rectangle starts and ends and control its width and height. The end goal of this is to have a map (of michigan and its great lakes) and have color coded rectangles pop up along the coast to visually show what the expected weather will be like based of buoys data from the NDBV. So in short, a map that i can place color coded rectangles on, that will be oriented along the coast of western michigan.

解决方案

Here is some Tkinter sample code. Note that, so far as I am aware, Tkinter does not support transparency; but it can fake it by stippling, as demonstrated here:

# copied from http://www.kosbie.net/cmu/fall-10/15-110/koz/misc-demos/src/semi-transparent-stipple-demo.py

# semi-transparent-stipple-demo.py
# note: stipple only works for some objects (like rectangles)
# and not others (like ovals).  But it's better than nothing...

from Tkinter import *

def redrawAll(canvas):
    canvas.delete(ALL)
    # draw a red rectangle on the left half
    canvas.create_rectangle(0, 0, 250, 600, fill="red")
    # draw semi-transparent rectangles in the middle
    canvas.create_rectangle(200,  75, 300, 125, fill="blue", stipple="")
    canvas.create_rectangle(200, 175, 300, 225, fill="blue", stipple="gray75")
    canvas.create_rectangle(200, 275, 300, 325, fill="blue", stipple="gray50")
    canvas.create_rectangle(200, 375, 300, 425, fill="blue", stipple="gray25")
    canvas.create_rectangle(200, 475, 300, 525, fill="blue", stipple="gray12")

def init(canvas):
    redrawAll(canvas)

########### copy-paste below here ###########

def run():
    # create the root and the canvas
    root = Tk()
    canvas = Canvas(root, width=500, height=600)
    canvas.pack()
    # Store canvas in root and in canvas itself for callbacks
    root.canvas = canvas.canvas = canvas
    # Set up canvas data and call init
    canvas.data = { }
    init(canvas)
    # set up events
    # root.bind("<Button-1>", mousePressed)
    # root.bind("<Key>", keyPressed)
    # timerFired(canvas)
    # and launch the app
    root.mainloop()  # This call BLOCKS (so your program waits until you close the window!)

run()

which produces something like

这篇关于在Python 2中绘制基本形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆