Python等效的HTML5 Canvas [英] Python Equivalent of HTML5 Canvas
本文介绍了Python等效的HTML5 Canvas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用Python完成以下HTML5画布笔画?我会使用 Tkinter 吗? Qt的? wxWidgets的?其他一些图书馆?
How would I accomplish the following HTML5 canvas stroke using Python? Would I use Tkinter? Qt? WxWidgets? Some other library?
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.moveTo(20,20);
ctx.lineTo(20,100);
ctx.lineTo(70,100);
ctx.strokeStyle="red";
ctx.stroke();
</script>
</body>
</html>
推荐答案
使用tkinter可以使用 Canvas
小部件类型。你不能直接改变边框的颜色;我不确定这是否是问题的一部分。您可以轻松地绘制彩色边框,或将其放在彩色边框内以使其具有边框。
With tkinter use can use the Canvas
widget type. You can't directly change the color of the border; I'm not sure if that's part of the question or not. You can easily draw a colored border, or put it inside a colored frame to give it a border.
要创建线条,请使用 create_line
方法。
To create the line, use the create_line
method.
以下是一个例子:
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
self.canvas = tk.Canvas(width=300, height=150,borderwidth=1)
self.canvas.pack(side="top", fill="both", expand=True)
points = (20,20,20,100,70,100)
self.canvas.create_line(points, fill="red")
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
这篇关于Python等效的HTML5 Canvas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文