Python小實(shí)例混合使用turtle和tkinter讓小海龜互動(dòng)起來(lái)
Turtle 窗口
請(qǐng)看下圖,turtle窗口圖標(biāo)是一片小葉子,估計(jì)它就是繼承自 tkinter 庫(kù)。

tkinter 窗口
參閱了一些資料,發(fā)現(xiàn) turtle 方法可直接在 tkinter 的畫(huà)布Canvas上操作:

源代碼
from tkinter import *
from turtle import RawTurtle
def circ():
tu.penup()
tu.home()
tu.clear()
tu.speed(0)
[x,y,R] = et1.get().split(',')
try:
x = int(x.replace('(',''))
y = int(y.replace(')',''))
R = int(R.strip())
except:
x,y,R = 0,0,50 #輸入錯(cuò)誤則賦予默認(rèn)值
tu.goto(x,y-R)
tu.pendown()
tu.circle(R)
def rect():
tu.penup()
tu.home()
tu.clear()
tu.speed(0)
tu.color('red', 'yellow')
tu.begin_fill()
[d,rad] = et2.get().split(',')
try:
d = int(d)
rad = int(rad)
except:
d,rad = 200,216 #輸入錯(cuò)誤則賦予默認(rèn)值
tu.pendown()
tu.back(d//5)
while True:
tu.forward(d)
tu.left(rad)
if abs(tu.pos()[0]+d//5)<0.1 and abs(tu.pos()[1])<0.1:
break
tu.end_fill()
def taiji():
tu.penup()
tu.home()
tu.clear()
tu.speed(0)
d = et3.get()
try:
d = int(d)
except:
d = 120 #輸入錯(cuò)誤則賦予默認(rèn)值
tu.hideturtle()
tu.goto(d//2,-d)
tu.pendown()
tu.begin_fill()
tu.color('black','black')
tu.circle(d,extent=180)
tu.circle(d//2,extent=180)
tu.circle(-d//2,extent=180)
tu.end_fill()
tu.circle(-d,extent=180)
tu.penup()
tu.goto(d//2,-d//6*4)
tu.pendown()
tu.begin_fill()
tu.fillcolor("black")
tu.circle(d//5,extent=360)
tu.end_fill()
tu.penup()
tu.goto(d//2,d//3)
tu.pendown()
tu.begin_fill()
tu.fillcolor("white")
tu.circle(d//5,extent=360)
tu.end_fill()
tu.penup()
def main():
global tu,et1,et2,et3
root = Tk()
root.geometry('520x520+150+300')
root.title('turtle在tkinter.Canvas上的操作')
root.resizable(False, False)
canvas = Canvas(root, width=640, height=400)
canvas.pack()
tu = RawTurtle(canvas)
tu.hideturtle()
et1 = Entry(root, width=12)
et1.place(x = 30, y = 480)
et1.insert(0,'(50,-20), 100')
bt1 = Button(root,text=' 畫(huà)圓 ',command=circ)
bt1.place(x = 60, y = 425)
et2 = Entry(root, width=12)
et2.place(x = 190, y = 480)
et2.insert(0,'200, 216')
bt2 = Button(root,text=' 多角星(或多邊形) ',command=rect)
bt2.place(x = 180, y = 425)
et3 = Entry(root, width=12)
et3.place(x = 360, y = 480)
et3.insert(0,'120')
bt3 = Button(root,text=' 太極 ',command=taiji)
bt3.place(x = 380, y = 425)
root.mainloop()
if __name__ == '__main__':
main()
turtle和tkinter混合使用
turtle和tkinter兩者搭配使用,使得 turtle 的畫(huà)圖參數(shù)可以由 tkinter 的控件當(dāng)場(chǎng)設(shè)置,這樣就實(shí)現(xiàn)了turtle小海龜?shù)默F(xiàn)場(chǎng)互動(dòng),運(yùn)行效果見(jiàn)圖二。
三個(gè)Entry控件,分別設(shè)置:
1. 圓心坐標(biāo)和圓的直徑;
2. 多邊形的邊長(zhǎng)和邊的轉(zhuǎn)動(dòng)角度;
3. 太極圖的外圓直徑。
(本篇完)
到此這篇關(guān)于Python小實(shí)例混合使用turtle和tkinter讓小海龜互動(dòng)起來(lái)的文章就介紹到這了,更多相關(guān)Python turtle 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python列表對(duì)象實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Python列表對(duì)象實(shí)現(xiàn)原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
pywinauto自動(dòng)化測(cè)試使用經(jīng)驗(yàn)
本文主要介紹了pywinauto自動(dòng)化測(cè)試使用經(jīng)驗(yàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Python一行代碼實(shí)現(xiàn)自動(dòng)發(fā)郵件功能
最近在自己學(xué)習(xí)Python爬蟲(chóng),學(xué)到了用Python發(fā)送郵件,覺(jué)得這個(gè)可能以后比較實(shí)用。所以這篇文章主要給大家介紹了如何通過(guò)Python一行代碼實(shí)現(xiàn)自動(dòng)發(fā)郵件功能的相關(guān)資料,需要的朋友可以參考下2021-05-05
Python爬蟲(chóng) urllib2的使用方法詳解
這篇文章主要介紹了Python爬蟲(chóng) urllib2的使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
python使用pyshark庫(kù)捕獲數(shù)據(jù)包的示例詳解
PyShark是一個(gè)基于Python的網(wǎng)絡(luò)數(shù)據(jù)包分析工具庫(kù),它允許用戶捕獲、解碼和分析實(shí)時(shí)網(wǎng)絡(luò)流量,特別是Wi-Fi和TCP/IP協(xié)議的數(shù)據(jù),所以本文給大家介紹了python使用pyshark庫(kù)捕獲數(shù)據(jù)包的示例,需要的朋友可以參考下2024-08-08
Python datetime包函數(shù)簡(jiǎn)單介紹
這篇文章主要介紹了Python datetime包函數(shù)簡(jiǎn)單介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08

