Python RawTurtle不遵循goto()命令

时间:2018-02-11 10:04:46

标签: python python-3.x tkinter turtle-graphics tkinter-canvas

我正在尝试创建一个程序,您单击Tkinter画布并且RawTurtle移动到鼠标,但我的代码不起作用。画布上有一个绑定的Button-1事件,用于告诉程序鼠标的坐标。 但是,当你点击画布而不是乌龟去鼠标时,它会反映出你期望它做的事情(远离mosue离开,好像另一个mosue被镜像一样)。打印出来时,事件和龟位置坐标都是相同的。

代码:

import turtle
from tkinter import *


def move(event):
    global t
    x = event.x
    y = event.y
    t.setpos(x,y)
    print(t.pos())
    print(event)
def penState(event):
    global penDown,t
    if penDown == True:
        t.penup()
        penDown = False
else:
    t.pendown()
    penDown = True

def changeWidth(w):
    t.pensize(w)
def changeColour(e=None):
    global colourBox
    t.color(colourBox.get())
    colourBox.configure(fg=colourBox.get())
def doCircle():
    global checkFillIsTrue,circleSizeBox
if checkFillIsTrue.get() == 1:
    begin_fill()
    t.circle(int(circleSizeBox.get()))
    end_fill()
else:
    circle(int(circleSizeBox.get()))
window = Tk('Paint')
window.title('onionPaint')
root = Frame(window)
root.pack(side=LEFT)
cv = Canvas(window,width=500,height=500)
t = turtle.RawTurtle(cv)
t.resizemode('user')
cv.bind('<Button-1>',move)
cv.bind('<Button-2>',penState)
cv.pack(side=RIGHT)
checkFillIsTrue=IntVar()
penDown = True
#Pen width box
sizeLabel = Label(root, text="Pen Width")
sizeLabel.grid()
sizeScale = Scale( root, variable = \
'var',orient=HORIZONTAL,command=changeWidth )
sizeScale.grid()
#Colour box
colourLabel = Label(root, text="Color(HEX or name):")
colourLabel.grid()
colourFrame = Frame(root)
colourFrame.grid()
colourBox = Entry(colourFrame, bd=1)
colourBox.pack(side=LEFT)
colourSubmit = Button(colourFrame,text="OK",command=changeColour)
colourSubmit.pack(side=RIGHT)
#Fill
fillLabel = Label(root,text='Fill')
fillLabel.grid()
fillFrame = Frame(root)
fillFrame.grid()
beginFill = Button(fillFrame,text='Begin Fill',command=t.begin_fill)
endFill = Button(fillFrame,text='End Fill',command=t.end_fill)
beginFill.pack(side=LEFT)
endFill.pack(side=RIGHT)
#Mmore shapes
Label(root,text='Shapes').grid()
#Circle form
Label(root,text='Circle',font=('Heveltica',8)).grid()
circleSize = Frame(root)
circleSize.grid()
circleSizeBox = Entry(circleSize,bd=1)
circleSizeBox.insert(0,'Radius')
circleSizeBox.pack(side=LEFT)
fillCheck = 
Checkbutton(circleSize,text='Fill',variable=checkFillIsTrue).pack(side=LEFT)
circleSizeSubmit = 
Button(circleSize,text='Draw',command=doCircle).pack(side=RIGHT)
#Text form
Label(root,text='Text',font=('Heveltica',8)).grid()
textFrame = Frame(root)
textFrame.grid()

window.mainloop()

对此问题的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

你的代码是一场灾难。您应该专门阅读Python中的'global'关键字以及何时必须使用它。和Python代码风格一般。我相信你的程序的关键修复是在画布上引入TurtleScreen覆盖,然后切换到turtle事件处理而不是tkinter事件处理。

我在下面修改了你的代码,解决了尽可能多的问题:

import turtle
from tkinter import *

FONT = ('Helvetica', 8)

def move(x, y):
    terrapin.setpos(x, y)
    print(terrapin.pos())

def penState(x, y):
    global penDown

    if penDown:
        terrapin.penup()
        penDown = False
    else:
        terrapin.pendown()
        penDown = True

def changeWidth(w):
    terrapin.pensize(w)

def changeColour():
    color = colourBox.get()
    terrapin.color(color)
    colourBox.configure(fg=color)

def doCircle():
    radius = int(circleSizeBox.get())

    if checkFillIsTrue.get():
        terrapin.begin_fill()
        terrapin.circle(radius)
        terrapin.end_fill()
    else:
        terrapin.circle(radius)

window = Tk('Paint')
window.title('onionPaint')

root = Frame(window)
root.pack(side=LEFT)

canvas = Canvas(window, width=500, height=500)
screen = turtle.TurtleScreen(canvas)

terrapin = turtle.RawTurtle(screen)

screen.onclick(move, btn=1)
screen.onclick(penState, btn=2)

canvas.pack(side=RIGHT)

checkFillIsTrue = BooleanVar()
penDown = True

# Pen width box
sizeLabel = Label(root, text="Pen Width")
sizeLabel.grid()
sizeScale = Scale(root, variable='var', orient=HORIZONTAL, command=changeWidth)
sizeScale.grid()

# Colour box
colourLabel = Label(root, text="Color(HEX or name):")
colourLabel.grid()
colourFrame = Frame(root)
colourFrame.grid()
colourBox = Entry(colourFrame, bd=1)
colourBox.pack(side=LEFT)
colourSubmit = Button(colourFrame, text="OK", command=changeColour)
colourSubmit.pack(side=RIGHT)

# Fill
fillLabel = Label(root, text='Fill')
fillLabel.grid()
fillFrame = Frame(root)
fillFrame.grid()
beginFill = Button(fillFrame, text='Begin Fill', command=terrapin.begin_fill)
endFill = Button(fillFrame, text='End Fill', command=terrapin.end_fill)
beginFill.pack(side=LEFT)
endFill.pack(side=RIGHT)

# More shapes
Label(root, text='Shapes').grid()

# Circle form
Label(root, text='Circle', font=FONT).grid()
circleSize = Frame(root)
circleSize.grid()
circleSizeBox = Entry(circleSize, bd=1)
circleSizeBox.insert(0, 'Radius')
circleSizeBox.pack(side=LEFT)
fillCheck = Checkbutton(circleSize, text='Fill', variable=checkFillIsTrue).pack(side=LEFT)
circleSizeSubmit = Button(circleSize, text='Draw', command=doCircle).pack(side=RIGHT)

# Text form
Label(root, text='Text', font=FONT).grid()
textFrame = Frame(root)
textFrame.grid()

window.mainloop()