海龟命令学习器

0 Comments

李兴球Python海龟命令学习器

李兴球Python海龟命令学习器

from sprites import *

def exit_program():
   screen.bye()

screen = Screen()
screen.setup(580,460)
root = screen._root
canvas = screen.getcanvas()
screen.bgcolor('light cyan')

t = Sprite('res/turtle2.png')
t.pendown()

frame = TK.Frame(canvas)
frame.grid(row=0,column=1)          # 这里一定要用grid布局

# 标签
label = TK.Label(frame, text= "请选择", font= ('Helvetica 14 bold'))
label.grid(row=0,column=0)

# 选择画笔颜色
def select_pencolor():
    s = askcolor()
    if s[-1]: t.pencolor(s[-1])
    
b1  = TK.Button(frame, text= "画笔颜色", command= select_pencolor)
b1.grid(row=1,column=0)

# 选择填充颜色
def select_fillcolor():
    s = askcolor()    
    if s[-1]: t.fillcolor(s[-1])
    
b2  = TK.Button(frame, text= "填充颜色", command= select_fillcolor)
b2.grid(row=2,column=0)

# 前进
b3  = TK.Button(frame, text= "前进forward", command=lambda :t.fd(10))
b3.grid(row=3,column=0)

# 右转
b4  = TK.Button(frame, text= "右转", command=lambda :t.rt(10))
b4.grid(row=4,column=0)

# 线宽
def input_pensize():
    w = screen.inputbox('输入','画笔线宽')
    if w.isdigit():
        print(w)
        t.pensize(int(w))
    
b5  = TK.Button(frame, text= "画笔线宽", command=input_pensize)
b5.grid(row=5,column=0)

# 手动输入命令
def run(event):
    cmd = b6.get()
    exec(cmd)
    
b6  = TK.Entry(frame, width=20)
b6.bind("",run)
b6.grid(row=6,column=0)


root.mainloop()

标签:

发表评论