The first code is a simple example:
We must create a canvas using the "Canvas" method, also its dimensions are declared here.from Tkinter import * root = Tk() c1 = Canvas(root, height = 180, width = 200) c1.create_line(20, 20, 160, 160, fill = "orange" , width = 3) c1.create_line(40, 50, 100, 110, fill = "yellow" , width = 3) c1.pack() root.title("Lines") root.mainloop()
c1 = Canvas(root, height = 180, width = 200)
Then we create a line using:
c1.create_line(20, 20, 160, 160, fill = "orange" , width = 3)
The coordinates of the first point are (20, 20), and of the last point are (160, 160). The attributes of fill_color and line_width go with "fill" and "width", respectively.
Then we could show the results in the screen by using the "pack" or "grid" methods.
The next examples are a little more complex but show something else than just a couple of lines.
Second code sample is:
import Tkinter def line(a, b, color, w): x0, y0, x1, y1 = a[0], a[1], b[0], b[1] c1.create_line(x0, y0, x1, y1, fill = color, width = w) root = Tkinter.Tk() ch, cw = 180, 200 A, B, C = [30, 16], [160, 16], [30, 165] D, E, F = [160, 165], [ 20, 164], [90, 94] G, H, I = [100, 85], [168, 16], [20, 16] J = [168, 165] P = [A, B, C, D, E, F, G, H, I, J] c1 = Tkinter.Canvas(root, height = ch, width = cw) for i in range(len(P) / 2): if i < 2: line(P[2 * i], P[2 * i + 1], "red" , 3) else: line(P[2 * i], P[2 * i + 1], "blue", 3) c1.pack() root.title("Lines") root.mainloop()
Third code sample is:
from Tkinter import * root = Tk() ch, cw = 180, 200 r , s = 5, 5 xs, xr = 5, 3 loop_b = 12 loop_c = 40 loop_d = 7 ox, oy = -20, -10 dx, dy = 5, 2.5 n = 1.0 a = [ 80 * n + ox, 160 * n + oy] b = [160 * n + ox, 160 * n + oy] c = [ 64 * n + ox, 135 * n + oy] d = [120 * n + ox, 135 * n + oy] e = [123 * n + ox, 135 * n + oy] f = [160 * n + ox, 135 * n + oy] g = [100 * n + ox, 20 * n + oy] h = [120 * n + ox, 20 * n + oy] c1 = Canvas(root, height = ch, width = cw) for i in range(31): y = 0 + 5 * i c1.create_line(0, y, 200, y, fill = "cyan") for i in range(5): x0, y0 = a[0] - 2 * r * i, a[1] - r * i x1, y1 = b[0] + 2 * r * i, b[1] - r * i c1.create_line(x0, y0, x1, y1, fill = "orange") print x0, y0, x1, y1 for i in range(loop_b): x0, y0 = c[0] + xs * i, c[1] - s * i x1, y1 = d[0] + 00 * i, d[1] - s * i c1.create_line(x0, y0, x1, y1, fill = "white") for i in range(loop_b): x0, y0 = e[0] + 00 * i, e[1] - s * i x1, y1 = f[0] - xr * i, f[1] - s * i c1.create_line(x0, y0, x1, y1, fill = "white") for j in range(loop_d): for i in range(loop_c): x0, y0 = 0.0 + 5 * i, 152.5 + 5 * j x1, y1 = 2.5 + 5 * i, 150.0 + 5 * j x2, y2 = 5.0 + 5 * i, 155.0 + 5 * j c1.create_line(x0, y0, x1, y1, fill = "blue") c1.create_line(x1, y1, x2, y2, fill = "blue") c1.pack() root.title("Lines") root.mainloop()
No comments:
Post a Comment