在python 2中绘制基本形状

时间:2014-12-14 00:37:53

标签: python python-2.7 graphics geometry

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

2 个答案:

答案 0 :(得分:3)

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

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

产生类似

的东西

enter image description here

答案 1 :(得分:0)

结帐

svgfig或svgwrite for svg

reportlab for pdf

用于绘制到窗口的pyglet