タートル描画
タートル描画Pythonは非常に人気の描画ツールです。- 1966年、博士サイモンパパートは、プログラミング言語を学習するための特別な子供たちを発明LOGO言語を。これは、画面上の小さなカメの描画の命令をプログラムすることによって区別されます。
ウミガメとPythonは、モジュールを描く建て亀の原画のすべての機能をコピーします。
次の手順では、turtle.mainloop()、ドローの最後に閉じた描画ウィンドウを許可していない最後の亀の描画に使用されるこの方法を見つけるだろう
Tkinterのモジュール(「Tkのインタフェース」)であるPythonの .Tk標準のTk GUIツールキットへのインタフェースとTkinterのがほとんどで働くのUnixプラットフォーム下での使用、同じでは、WindowsおよびMacintoshのシステムに適用することができ、その後のバージョンを達成することができ.Tk8.0ローカルスタイルのウィンドウ、およびプラットフォームの大半でも実行
そこ少し良く理解この記事内のリンクの説明されていますhttps://blog.csdn.net/San__Qi/article/details/100568682
PythonのTkinterの中の物質のメインループ機能
テキストは
ここでは最も一般的なTkinterのプログラマーは共通している段階です、それは次のことを行うためのコードです:
コンポーネントクラスの間のモジュールから1ロード苦しみ。
コンポーネントクラスラベルクラスのインスタンスを作成2.
親コンポーネント内3.パッケージの新しいタブを。
4.メイン・ループ、表示ウィンドウを呼び出し、Tkinterのイベントループを開始しました。
最後の実行方法をメインループ、ラベルが画面上に表示され、待機状態に入る(注:コンポーネントがパッケージ化されていない場合は、ウィンドウに表示されません)、ユーザによって開始GUIイベントに応答する準備ができて。メインループ機能では、内部のTkinterはように、キーボード活動、マウスクリックなどのこれらのイベントを監視し、あろう。実際には、以下の擬似コードでメインループPython関数のTkinterには本質的に同じです。def mainloop(): while the main window has not been closed: if an event has occurred: run the associated event handler function
このイベントモデルのためには、限りGUIが画面上に残っているとして、メインループの呼び出しがコードを実行するためには戻りません。我々は大規模なコードを書くときに、唯一のメインループの練習を呼び出した後にイベントに応答してコールマネージャを登録することです。終了条件が満たされるまで、行っ割り込みメインループの一例を終了するTkの()を呼び出します。実際にはsys.exit機能も、それは例外で、その後、例外が(os._exit方式を採用していない、それはまた、全体のプログラムを終了することができますキャプチャすることができ、投げることでプログラムを終了し、GUIを終了するために使用することができますが、それはしていません)これは、クリーンアップ操作を実行し、それがキャッチすることはできません。ウィンドウ(例えば、Tkのルートウィンドウ、トップレベルのインスタンス)機能を破壊もGUIを閉じるために使用することができるが、我々は、一般的に、この方法を使用していないが、それはプログラムTkのルートウィンドウの複数のみ破壊する場合、関数を終了異なりますルートGUIを離れる際に、最後のウィンドウが閉じられた後。もちろん、我々はまた、缶フィルタと、これらのイベントの他の操作をメインループが、これは、この記事に関連した大きなではありません。
「Pythonプログラミング」---- O'REILLY第四版からの要約。
この方法ではカメのモジュール
-
インポートモジュール
輸入輸入カメにカメの必要性を描画するとき、例えば、Pythonでインポートするモジュールのニーズを使用している場合
-
「召喚」カメ
カメは小さいカメ(ペン)を描画すると、矢印の元の形状である、我々は亀の変更その形状の内部を形成する方法を使用することができます。
import turtle turtle.shape('turtle')
これは、小さな亀の形にそれを置きます。
他の形状も:「円」、「矢印」 、「四角」、「三角形」、「クラシック」。 -
前方に
の方向に前方に向い前方に孵化の使用を制御する方法
import turtle turtle.shape('turtle') turtle.forward(100)
-
円
サークルカメを描くように小さな円を指令するために使用される方法
import turtle turtle.shape('turtle') turtle.circle(50)
前記括弧内の数字の後ろの円の半径は、円のサイズはピクセル単位であります
-
ターン
この方法は、左操舵孵化に左を使用して制御することができます
import turtle turtle.shape('turtle') turtle.left(45) turtle.forward(100)
このコード孵化が左に45度に調整し、次に直線を描画するために進むことができます。
-
四角を描きます
import turtle turtle.shape('turtle') turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.mainloop()
-
描画雪だるま
import turtle turtle.shape('turtle') turtle.circle(50) turtle.left(180) turtle.circle(100) turtle.mainloop()
-
前方前方 - 後方後方
import turtle turtle.shape('turtle') turtle.backward(100)
-
右折右 - 左を左に
import turtle turtle.right(45) turtle.forward(100)
-
赤の広場
import turtle turtle.pensize(4) turtle.pencolor('red') turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.mainloop()
-
旗を描きます
""" 用Python的turtle模块绘制国旗 """ import turtle def draw_rectangle(x, y, width, height): """绘制矩形""" turtle.goto(x, y) turtle.pencolor('red') turtle.fillcolor('red') turtle.begin_fill() for i in range(2): turtle.forward(width) turtle.left(90) turtle.forward(height) turtle.left(90) turtle.end_fill() def draw_star(x, y, radius): """绘制五角星""" turtle.setpos(x, y) pos1 = turtle.pos() turtle.circle(-radius, 72) pos2 = turtle.pos() turtle.circle(-radius, 72) pos3 = turtle.pos() turtle.circle(-radius, 72) pos4 = turtle.pos() turtle.circle(-radius, 72) pos5 = turtle.pos() turtle.color('yellow', 'yellow') turtle.begin_fill() turtle.goto(pos3) turtle.goto(pos1) turtle.goto(pos4) turtle.goto(pos2) turtle.goto(pos5) turtle.end_fill() def main(): """主程序""" turtle.speed(12) turtle.penup() x, y = -270, -180 # 画国旗主体 width, height = 540, 360 draw_rectangle(x, y, width, height) # 画大星星 pice = 22 center_x, center_y = x + 5 * pice, y + height - pice * 5 turtle.goto(center_x, center_y) turtle.left(90) turtle.forward(pice * 3) turtle.right(90) draw_star(turtle.xcor(), turtle.ycor(), pice * 3) x_poses, y_poses = [10, 12, 12, 10], [2, 4, 7, 9] # 画小星星 for x_pos, y_pos in zip(x_poses, y_poses): turtle.goto(x + x_pos * pice, y + height - y_pos * pice) turtle.left(turtle.towards(center_x, center_y) - turtle.heading()) turtle.forward(pice) turtle.right(90) draw_star(turtle.xcor(), turtle.ycor(), pice) # 隐藏海龟 turtle.ht() # 显示绘图窗口 turtle.mainloop() if __name__ == '__main__': main()
-
豚ページを描画
""" 绘制小猪佩奇 """ from turtle import * def nose(x,y): """画鼻子""" penup() # 将海龟移动到指定的坐标 goto(x,y) pendown() # 设置海龟的方向(0-东、90-北、180-西、270-南) setheading(-30) begin_fill() a = 0.4 for i in range(120): if 0 <= i < 30 or 60 <= i <90: a = a + 0.08 # 向左转3度 left(3) # 向前走 forward(a) else: a = a - 0.08 left(3) forward(a) end_fill() penup() setheading(90) forward(25) setheading(0) forward(10) pendown() # 设置画笔的颜色(红, 绿, 蓝) pencolor(255, 155, 192) setheading(10) begin_fill() circle(5) color(160, 82, 45) end_fill() penup() setheading(0) forward(20) pendown() pencolor(255, 155, 192) setheading(10) begin_fill() circle(5) color(160, 82, 45) end_fill() def head(x, y): """画头""" color((255, 155, 192), "pink") penup() goto(x,y) setheading(0) pendown() begin_fill() setheading(180) circle(300, -30) circle(100, -60) circle(80, -100) circle(150, -20) circle(60, -95) setheading(161) circle(-300, 15) penup() goto(-100, 100) pendown() setheading(-30) a = 0.4 for i in range(60): if 0<= i < 30 or 60 <= i < 90: a = a + 0.08 lt(3) #向左转3度 fd(a) #向前走a的步长 else: a = a - 0.08 lt(3) fd(a) end_fill() def ears(x,y): """画耳朵""" color((255, 155, 192), "pink") penup() goto(x, y) pendown() begin_fill() setheading(100) circle(-50, 50) circle(-10, 120) circle(-50, 54) end_fill() penup() setheading(90) forward(-12) setheading(0) forward(30) pendown() begin_fill() setheading(100) circle(-50, 50) circle(-10, 120) circle(-50, 56) end_fill() def eyes(x,y): """画眼睛""" color((255, 155, 192), "white") penup() setheading(90) forward(-20) setheading(0) forward(-95) pendown() begin_fill() circle(15) end_fill() color("black") penup() setheading(90) forward(12) setheading(0) forward(-3) pendown() begin_fill() circle(3) end_fill() color((255, 155, 192), "white") penup() seth(90) forward(-25) seth(0) forward(40) pendown() begin_fill() circle(15) end_fill() color("black") penup() setheading(90) forward(12) setheading(0) forward(-3) pendown() begin_fill() circle(3) end_fill() def cheek(x,y): """画脸颊""" color((255, 155, 192)) penup() goto(x,y) pendown() setheading(0) begin_fill() circle(30) end_fill() def mouth(x,y): """画嘴巴""" color(239, 69, 19) penup() goto(x, y) pendown() setheading(-80) circle(30, 40) circle(40, 80) def setting(): """设置参数""" pensize(4) # 隐藏海龟 hideturtle() colormode(255) color((255, 155, 192), "pink") setup(840, 500) speed(10) def main(): """主函数""" setting() nose(-100, 100) head(-69, 167) ears(0, 160) eyes(0, 140) cheek(80, 10) mouth(-20, 30) done() if __name__ == '__main__': main()
-