Python Start Turtle Library application Example __python

Source: Internet
Author: User
Tags goto sendmsg

1, the use of Turtle Library functions to draw different geometries, and do fill processing:

# turtletest.py Import Turtle def main (): Turtle.speed (2) turtle.pensize (3) turtle.penup () turtle.go to ( -200,-50) Turtle.pendown () #表示开始做图形填充 Turtle.begin_fill () turtle.color (' Red ') turtle.circle (40,step s= 3) #填充结束 Turtle.end_fill () turtle.penup () Turtle.goto ( -100,-50) turtle.pendown () Turtle.begin_ Fill () Turtle.color ("Blue") turtle.circle (steps=4) Turtle.end_fill () Turtle.penup () ( 0,-50) Turtle.pendown () Turtle.begin_fill () Turtle.color ("green") turtle.circle (steps=5) turtle.en D_fill () Turtle.penup () Turtle.goto (100,-50) Turtle.pendown () Turtle.begin_fill () Turtle.color ("Yell ow ") turtle.circle (steps=6) Turtle.end_fill () Turtle.penup () Turtle.goto (200,-50) Turtle.pendown () Turtle.begin_fill () Turtle.color ("purple") turtle.circle #未设置步数则认为是绘制圆形 Turtle.end_fill () Turt Le.color ("green") tuRtle.penup () Turtle.goto ( -100,50) Turtle.pendown () turtle.write ("Cool Colorful Shapes"), #添加文字 font = ("Times", "bold")

 
 #设置文字格式 turtle.hideturtle () #隐藏画笔形状 turtle.done () if __name__ = = ' __main__ ': Main ()
Run Result:


2, using Turtle to draw graphical interactive interface, to chat software as an example;

# uiturtle.py from tkinter import * Import time Def main (): Def sendmsg (): #发送消息 strMsg = ' I: ' +time.strftime ( '%y-%m-%d%h:%m:%s ', time.localtime () + ' \ n ' txtmsglist.insert (end,strmsg, ' Greencolor ') Txtmsglist.insert (EN

    D,txtmsg.get (' 0.0 ', end)] txtmsg.delete (' 0.0 ', end) def cancelmsg (): #取消消息 txtmsg.delete (' 0.0 ', end) def sendmsgevent (event): #发送消息事件 if Event.keysym = = ' Up ': sendmsg () #创建窗口 t = Tk () t.titl

    E (' Chat with * * ") # T.  #创建frame容器 (actually dividing the window into different functional areas) FRMLT = frame (width = 500,height = 320,BG = ' White ') FRMLC = frame (width = 500,height
    = 150,BG = ' White ') frmlb = frame (width = 500,height =) FRMRT = FRAME (width = 200,height =) #创建控件 Txtmsglist = Text (FRMLT) #在父窗口frmLT中创建一个文本对象 txtmsglist.tag_config (' greencolor ', foreground = ' #008C00 ') #创建tag txt MSG = Text (FRMLC) txtmsg.bind (' <KeyPress-Up> ', sendmsgevent) btnsend = Button (frmlb,text = ' send ', width = 8,Command = sendmsg) Btncancel = Button (Frmlb,text = ' Cancel ', width = 8,command = cancelmsg) Imginfo = photoimage (file = ' Python.gif ') lblimage = Label (frmrt,image = imginfo) lblimage.image = Imginfo #窗口布局 frmlt.grid (row = 0 , column = 0,columnspan = 2,PADX =1,pady = 3) frmlc.grid (row = 1,column = 0,columnspan = 2,PADX =1,pady = 3) frmlb. Grid (row = 2,column = 0,columnspan = 2) frmrt.grid (row = 0,column = 2,rowspan = 3,PADX =2,pady = 3) #固定大小 frm Lt.grid_propagate (0) frmlc.grid_propagate (0) frmlb.grid_propagate (0) frmrt.grid_propagate (0) "pay special attention to the grid () function:
 This geometry manager organizes the parts in the parent part's table structure.
    Syntax: Widget.grid (grid_options) below is a list of possible options: column: Drop widget, defaults to 0 (leftmost column).
    ColumnSpan: The number of columns used by the part, defaults to 1.
    IPADX, Ipady: How many parts of the pixel, horizontal and vertical direction, part of the boundary.
    PADX, Pady: How many parts of the pixel, horizontal and vertical direction, V of the outer boundary.
    Row: The row places the widget; The default first row is still empty.
    RowSpan: How many rows of parts occupy; the default is 1. Sticky: What to do if the cell is larger than the widget. By default, the widget is in its cell center with sticky = '.
The sticky may be a string connected to 0 or more n,e,s,w, northeast, northwest, southeast, southwest, Compass direction indicating the parts adhere to the sides and corners of the cell.
   ''' Btnsend.grid (row = 3,column = 0) btncancel.grid (row = 3,column = 1) lblimage.grid () Txtmsglist.grid () txt
 Msg.grid () #主事件循环 t.mainloop () if __name__ = = ' __main__ ': Main ()
Execution results:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.