《趣学Python编程》笔记---第一部分:学习编程(3)

本文介绍了使用Python的Turtle和Tkinter库进行图形绘制的方法,包括基本图形的绘制、填充颜色、创建动画等,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#第十一章节 高级海龟作图
11.1 从基本的正方形开始
直接贴代码

import turtle
t=turtle.Pen()
for x in range(1,5):
	t.forward(50)
	t.left(90)

11.2 画星星

>>> t.reset()
>>> for x in range(1,9):
	t.forward(100)
	t.left(225)

>>> t.reset()
>>> for x in range(1,38):
	t.forward(100)
	t.left(175)

11.3 画汽车

import turtle
t=turtle.Pen()
t.color(1,0,0)
t.begin_fill()
t.forward(100)
t.left(90)
t.forward(20)
t.left(90)
t.forward(20)
t.right(90)
t.forward(20)
t.left(90)
t.forward(60)
t.left(90)
t.forward(20)
t.right(90)
t.forward(20)
t.left(90)
t.forward(20)
t.end_fill()
t.color(0,0,0)
t.up()
t.forward(10)
t.down()
t.begin_fill()
t.circle(10)
t.end_fill()
t.setheading(0)
t.up()
t.forward(90)
t.right(90)
t.forward(10)
t.setheading(0)
t.begin_fill()
t.down()
t.circle(10)
t.end_fill()

11.4 填色
后面的懒得写了,自己看书吧


#第十二章 用Thinter画高级图形
12.1 创造一个可以点的按钮
用from +模块名+import+* 就可以在不用模块名的情况下使用模块的内容

def hello():
	print("hello there")
from tkinter import *
tk=Tk()
bth=Button(tk,text="click me",command=hello)
bth.pack()

12.2 使用具名参数

def person(width,height):
	print("i am %s feet wide,%s feet high"%(width,height))
person(width=3,height=4)
i am 3 feet wide,4 feet high

12.3 创建一个画图用的画布

>>>from tkinter import *
>>> tk=Tk()
>>> canvas=Canvas(tk,width=500,height=500)
>>> canvas.pack()

12.4 画线

from tkinter import *
tk=Tk()
canvas=Canvas(tk,width=500,height=500)
canvas.pack()
canvas.create_line(0,0,500,500)

12.5 画盒子

from tkinter import *
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
canvas.create_rectangle(10,10,50,50)

from tkinter import *
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
canvas.create_rectangle(10,10,300,50)

12.5.1 画许多矩形

from tkinter import *
import random
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
def random_rectangle(width,height):
    x1=random.randrange(width)
    y1=random.randrange(height)
    x2=x1+random.randrange(width)
    y2=y1+random.randrange(height)
    canvas.create_rectangle(x1,y1,x2,y2)
random_rectangle(400,400)
for x in range(0,100):
    random_rectangle(400,400)

12.5.2 设置颜色

from tkinter import *
import random
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
def random_rectangle(width,height,fill_color):
    x1=random.randrange(width)
    y1=random.randrange(height)
    x2=x1+random.randrange(width)
    y2=y1+random.randrange(height)
    canvas.create_rectangle(x1,y1,x2,y2,fill=fill_color)
random_rectangle(100,100,'green')

12.6 画圆弧

from tkinter import *
import random
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
canvas.create_arc(10,10,200,100,extent=180,style=ARC)

360°==0° 什么也画不出来 如果画整圆弧就写359°

12.7画多变形
12.8显示文字

from tkinter import *
import random
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
canvas.create_text(150,100,text="there one was a man")

显示颜色

from tkinter import *
import random
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
canvas.create_text(130,120,text="who are you ",fill='red')

字体大小/格式

from tkinter import *
import random
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
canvas.create_text(130,120,text="who are you ",fill='red',font=('Times',20))


from tkinter import *
import random
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
canvas.create_text(130,120,text="who are you ",fill='red',font=('Times',20))
canvas.create_text(200,170,text="who are you ",fill='red',font=('Helvetica',20))


12.9 显示图片
12.10 创建基本的动画

import time
from tkinter import *
tk=Tk()
canvas=Canvas(tk,width=400,height=200)
canvas.pack()
canvas.create_polygon(10,10,10,60,50,35)
for x in range(0,60):
    canvas.move(1,5,0)
    tk.update()
    time.sleep(0.05)

12.11 让对象对操作有反应

Chapter 2 Magic coins example. magic_coins1.py Chapter 3 Favourite sports. favourite_sports.py Furniture placeholder. furniture_placeholder.py A list of lists. list_of_lists.py A letter from Malcolm Dithering dithering_letter.py Escaping quotes quote_escaping.py The Wizard List wizard_list.py Chapter 4 The turtle draws a square. turtle1.py The turtle draws two parallel lines. turtle2.py Chapter 5 If statements if_statements.py Conditions in if-statements. conditions.py Else-if (elif) statements. elif_statements.py Strings and numbers. strings_and_numbers.py Chapter 6 Five Hellos. five_hellos.py Huge hairy pants (example 1). huge_hairy_pants1.py Huge hairy pants (example 2). huge_hairy_pants2.py Magic coins (example 2). magic_coins2.py A while-loop with multiple conditions. while_loop_multiple_conditions.py Looping through the wizard list. wizard_list_loop.py Chapter 7 A function to calculate your savings. savings.py Building a spaceship. spaceship_building.py Test function (example 1). test_function1.py Test function (example 2). test_function2.py Your age function. your_age.py Chapter 8 Giraffes (example 1). giraffes1.py Giraffes (example 2). giraffes2.py Three turtles. three_turtles.py Chapter 9 Using the abs (absolute) function. abs_function.py Using the exec (execute) function. exec_function.py Using the len (length) function. len_function.py Using the max and min functions. max_and_min.py Using the range function. range_function.py Using the sum function. range_function.py Opening a file. opening_a_file.py Writing to a file. writing_to_a_file.py Chapter 10 Copying objects (example 1). copying_objects1.py Copying objects (example 2). copying_objects1.py Guess a random number. guess_a_number.py Random desserts. random_desserts.py Using the time module. timing_lots_of_numbers.py Using pickle to save information. pickle_saving.py Using pickle to load information. pickle_loading.py Chapter 11 Drawing an eight point star. eight_point_star.py Drawing a many point star. many_point_star.py Drawing a spiral star. spiral_star.py Drawing a nine point star. nine_point_star.py Drawing a car. car.py Drawing a yellow circle. yellow_circle.py Drawing a filled green circle. green_circle.py Drawing a dark-green circle. dark_green_circle.py Drawing squares using a function. square_function.py Drawing filled and unfilled squares. filled_squares.py Drawing stars using a function. star_function.py Chapter 12 Clickable button (example 1). clickable_button1.py Clickable button (example 2). clickable_button2.py Drawing a diagonal line. diagonal_line.py Drawing a square. square.py Drawing a horizontal rectangle. horizonal_rectangle.py Drawing a vertical rectangle. horizonal_rectangle.py Drawing random rectangles. random_rectangles.py Drawing coloured rectangles. coloured_rectangles.py Using the color chooser. rectangle_colorchooser.py Drawing arcs. drawing_arcs.py Drawing polygons. drawing_polygons.py Drawing text. drawing_text.py Drawing images. drawing_images.py Basic animation (example 1). basic_animation1.py Basic animation (example 2). basic_animation2.py Using events (example 1). using_events1.py Using events (example 2). using_events1.py Using the move function. using_move.py Using the itemconfig function. using_itemconfig.py Chapter 13 Bounce (example 1) - this one doesn't do anything when it's run. bounce1.py Bounce (example 2) - stationary ball. bounce2.py Bounce (example 3) - ball moving upwards. bounce3.py Bounce (example 4) - ball moving up and down. bounce4.py Bounce (example 5) - ball moving around the screen. bounce5.py Chapter 14 Bounce (example 6) - adding the paddle. bounce6.py Bounce (example 7) - moving paddle. bounce6.py Bounce (example 8) - bouncing the ball when it hits the paddle. bounce6.py Bounce (example 9) - ending the game when the ball hits the floor. bounce6.py Chapter 15 Transparent Image - 27 by 30 pixels. transparent-image.gif 6 Stick Figure Images - left and right. stickfigure.zip 3 Platform images. platform.zip 2 Door images. door.zip Background image. background.gif Chapter 16 Stickman Game, version 1 - displaying the background. stickmangame1.py Stickman Game, version 2 - adding the within functions. stickmangame2.py Stickman Game, version 3 - adding the platform. stickmangame3.py Stickman Game, version 4 - adding lots of platforms. stickmangame4.py Chapter 17 Stick figure sprite class. stickfiguresprite.py Stickman Game, version 5 - adding the stick figure. stickmangame5.py Chapter 18 Stickman Game, version 6 - animating the stick figure. stickmangame6.py Stickman Game, version 6 - adding the door sprite. stickmangame7.py Afterword Pygame2 example. pygame-example.py Hello World Java example. HelloWorld.java Hello World C example. helloworld.c Hello World C++ example. helloworld.cpp Hello World C# example. helloworld.cs Hello World PHP example. helloworld.php Hello World Objective-C example. helloworld.m Hello World Perl example. helloworld.pl Hello World Ruby example. helloworld.rb Hello World Javascript example. helloworld.js Hello World Javascript Browser example. helloworld-js.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值