In this Python article iam going to show you writing Merry Christmas Code With Turtle Graphic, so for this purpose we are going to use Python Turtle Graphic.
Also you can check the complete tutorial for python GUI Frameworks
1: PyQt5 GUI Development Complete Tutorials
2: TKinter GUI Development For Beginners
3: Pyside2 GUI Development For Beginners
4: wxPython GUI Full Course For Beginners
What is Python Turtle ?
Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzig and Seymour Papert in 1966. Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle, give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25), and it rotates in-place 25 degrees clockwise.By combining together these and similar commands, intricate shapes and pictures can easily be drawn.The turtle module is an extended re-implementation of the same-named module from the Python standard distribution up to version Python 2.5. It tries to keep the merits of the old turtle module and to be (nearly) 100% compatible with it. This means in the first place to enable the learning programmer to use all the commands, classes and methods interactively when using the module from within IDLE run with the -n switch.The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses tkinter for the underlying graphics, it needs a version of Python installed with Tk support.
The object-oriented interface uses essentially two+two classes:
-
The TurtleScreen class defines graphics windows as a playground for the drawing turtles. Its constructor needs a tkinter.Canvas or a ScrolledCanvas as argument. It should be used when turtle is used as part of some application.
The function Screen() returns a singleton object of a TurtleScreen subclass. This function should be used when turtle is used as a standalone tool for doing graphics. As a singleton object, inheriting from its class is not possible.
All methods of TurtleScreen/Screen also exist as functions, i.e. as part of the procedure-oriented interface.
-
RawTurtle (alias: RawPen) defines Turtle objects which draw on a TurtleScreen. Its constructor needs a Canvas, ScrolledCanvas or TurtleScreen as argument, so the RawTurtle objects know where to draw.
Derived from RawTurtle is the subclass Turtle (alias: Pen), which draws on “the” Screen instance which is automatically created, if not already present.
So now this is the complete code for Python Merry Christmas Code With Turtle Graphic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
import turtle def main(): window = turtle.Screen() my_turtle = turtle.Turtle() screen = my_turtle.getscreen() screen.title("Merry Christmas") screen.bgcolor("#E731CE ") # drawing the tree my_turtle.color("green") my_turtle.pensize(5) my_turtle.begin_fill() # this is the right half of the tree my_turtle.forward(100) my_turtle.left(150) my_turtle.forward(90) my_turtle.right(150) my_turtle.forward(60) my_turtle.left(150) my_turtle.forward(60) my_turtle.right(150) my_turtle.forward(40) my_turtle.left(150) my_turtle.forward(100) # this is the left half of the tree my_turtle.left(60) my_turtle.forward(100) my_turtle.left(150) my_turtle.forward(40) my_turtle.right(150) my_turtle.forward(60) my_turtle.left(150) my_turtle.forward(60) my_turtle.right(150) my_turtle.forward(90) my_turtle.left(150) my_turtle.forward(133) my_turtle.end_fill() # create the trunk of the tree my_turtle.color("red") my_turtle.pensize(1) my_turtle.begin_fill() my_turtle.right(90) my_turtle.forward(70) my_turtle.right(90) my_turtle.forward(33) my_turtle.right(90) my_turtle.forward(70) my_turtle.end_fill() # create star at the top of tree my_turtle.speed(1) my_turtle.penup() my_turtle.color('yellow') my_turtle.goto(-28, 110) my_turtle.begin_fill() my_turtle.pendown() for i in range(5): my_turtle.forward(40) my_turtle.right(144) my_turtle.end_fill() # create different color balls def ball(trt, x, y, size=10, colour="red"): trt.penup() trt.setpos(x, y) trt.color(colour) trt.begin_fill() trt.pendown() trt.circle(size) trt.end_fill() ball(my_turtle, 95, -5) ball(my_turtle, -110, -5) ball(my_turtle, 80, 40, size=7, colour="yellow") ball(my_turtle, -98, 40, size=7, colour="yellow") ball(my_turtle, 70, 70, size=5) ball(my_turtle, -93, 70, size=5) def create_circle(turtle, x, y, radius, color): my_turtle.penup() my_turtle.color(color) my_turtle.fillcolor(color) my_turtle.goto(x, y) my_turtle.pendown() my_turtle.begin_fill() my_turtle.circle(radius) my_turtle.end_fill() # create moon in sky create_circle(my_turtle, 230, 180, 60, "white") # overlap with full circle of BG color create_circle(my_turtle, 210, 180, 60, "#E731CE") # print greeting message my_turtle.speed(1) my_turtle.penup() msg = "Merry Christmas, Subscribe My Channel" my_turtle.goto(0, -200) # y is in minus because tree trunk was below x axis my_turtle.color("white") my_turtle.pendown() my_turtle.write(msg, move=False, align="center", font=("Arial", 20, "bold")) my_turtle.hideturtle() window.mainloop() if __name__ == "__main__": main() |
There is no need of explanation, because i have already commented the code, so if you run the code you will see
a graphic drawing of the tree.
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email