Python起步之turtle庫應用執行個體__Python

來源:互聯網
上載者:User

1、運用turtle庫函數實現繪製不同幾何圖形,並做填充處理:

# TurtleTest.pyimport turtledef main():    turtle.speed(2)        turtle.pensize(3)    turtle.penup()    turtle.goto(-200,-50)    turtle.pendown()    #表示開始做圖形填滿    turtle.begin_fill()    turtle.color('red')    turtle.circle(40,steps= 3)    #填充結束    turtle.end_fill()    turtle.penup()    turtle.goto(-100,-50)    turtle.pendown()    turtle.begin_fill()    turtle.color("blue")    turtle.circle(40, steps=4)    turtle.end_fill()     turtle.penup()    turtle.goto(0,-50)    turtle.pendown()    turtle.begin_fill()    turtle.color("green")    turtle.circle(40, steps=5)    turtle.end_fill()     turtle.penup()    turtle.goto(100,-50)    turtle.pendown()    turtle.begin_fill()    turtle.color("yellow")    turtle.circle(40, steps=6)    turtle.end_fill()     turtle.penup()    turtle.goto(200,-50)    turtle.pendown()    turtle.begin_fill()    turtle.color("purple")    turtle.circle(40)#未設定步數則認為是繪製圓形    turtle.end_fill()     turtle.color("green")    turtle.penup()    turtle.goto(-100,50)    turtle.pendown()    turtle.write(("Cool Colorful shapes"),#添加文字        font = ("Times", 18, "bold"))#設定文字格式    turtle.hideturtle()#隱藏畫筆形狀        turtle.done()    if __name__ == '__main__':    main() 
運行結果:


2、利用turtle繪製圖形互動介面,以聊天軟體為例;

# UITurtle.pyfrom tkinter import *import timedef main():    def sendMsg():#發送訊息        strMsg = '我:'+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())+'\n'        txtMsgList.insert(END,strMsg,'greencolor')        txtMsgList.insert(END,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.title('與***聊天中')   # t.    #建立frame容器(實際上是將視窗劃分成不同的功能區)    frmLT = Frame(width = 500,height = 320,bg = 'white')    frmLC = Frame(width = 500,height = 150,bg = 'white')    frmLB = Frame(width = 500,height = 30)    frmRT = Frame(width = 200,height = 500)    #建立控制項    txtMsgList = Text(frmLT)#在父視窗frmLT中建立一個文字物件    txtMsgList.tag_config('greencolor',foreground = '#008C00') #建立tag    txtMsg = Text(frmLC)    txtMsg.bind('<KeyPress-Up>',sendMsgEvent)    btnSend = Button(frmLB,text = '發 送',width = 8,command = sendMsg)    btnCancel = Button(frmLB,text = '取 消',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)    #固定大小    frmLT.grid_propagate(0)    frmLC.grid_propagate(0)    frmLB.grid_propagate(0)    frmRT.grid_propagate(0)'''特別注意grid()函數:這個的幾何管理器組織在父組件的表狀結構中的組件。 文法:widget.grid( grid_options )下面是可能的選項列表:    column : 列放組件,預設為0(最左邊的列).    columnspan: 組件佔用多少列,預設為1.    ipadx, ipady : 多少組件的像素,水平和垂直方向,組件的邊界內.    padx, pady : 多少組件的像素,水平和垂直方向,V的外邊界.    row: 該行放小組件;預設的第一行仍然是空的.    rowspan : 多少行的組件佔用;預設為1.    sticky : 做什麼,如果儲存格是比小組件大。預設情況下,用粘='',widget是在其細胞中心。粘可能是字串串連的零個或多個N,E,S,W,東北,西北,東南,西南,羅盤方向指示組件堅持儲存格的兩側和邊角.'''    btnSend.grid(row = 3,column = 0)    btnCancel.grid(row = 3,column = 1)    lblImage.grid()    txtMsgList.grid()    txtMsg.grid()    #主事件迴圈    t.mainloop()if __name__ == '__main__':    main()
執行結果:



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.