当我只想要一个时,为什么会有两个屏幕?

问题描述

基本上,我在turtle中创建了一个应用程序,我想将turtle应用程序的内容放置到Tkinter画布上。但是,当我运行代码时,当我只想要一个时就会创建两个屏幕。

这是示例代码

from tkinter import *
import turtle                   
import time                     


 
# Screen
screen = Tk()
screen.geometry("{0}x{1}+0+0".format(screen.winfo_screenwidth(),screen.winfo_screenheight())) 
screen.title("Example Code")
screen.configure(bg="Gray")
# Canvas
canvas = Canvas(screen,width="666",height="666")
canvas.place(relx=0.5,rely=0.5,anchor=CENTER)




# Making The User
user = turtle.RawTurtle(canvas)
user.shape("triangle")
user.setheading(90)
user.speed(0)
user.color("black")
user.down()
user.goto(0,0)
userspeed = 15

 

 
 
# Moving Functions
def move_up():
    y = user.ycor()
    y += userspeed
    if y > 280:
        y = 280
    user.sety(y)
    user.setheading(90)  # Changes Direction of the Head
 
 
def move_down():
    y = user.ycor()
    y -= userspeed
    if y < -280:
        y = -280
    user.sety(y)
    user.setheading(-90)  # Changes Direction of the Head
 
 
def move_left():
    x = user.xcor()
    x -= userspeed
    if x < -280:
        x = - 280
    user.setx(x)
    user.setheading(60)  # Changes Direction of the Head
 
 
def move_right():
    x = user.xcor()
    x += userspeed
    if x > 280:
        x = 280
    user.setx(x)
    user.setheading(0)  # Changes Direction of the Head
 
 
# Keyboard Bindings For Moving Functions
turtle.listen()
turtle.onkey(move_up,"Up")
turtle.onkey(move_down,"Down")
turtle.onkey(move_left,"Left")
turtle.onkey(move_right,"Right")

screen.mainloop()

运行此命令时,将创建两个屏幕(Tkinter屏幕和Turtle屏幕),我只需要Tkinter屏幕。但是,当我关闭乌龟屏幕时,键盘绑定在Tkinter屏幕上不起作用。我该如何解决

解决方法

删除乌龟事件。只需将其添加到tkinter.canvas中即可。您可以在下面尝试以下代码:

from tkinter import *
import turtle
import time

# Screen
screen = Tk()
screen.geometry("{0}x{1}+0+0".format(screen.winfo_screenwidth(),screen.winfo_screenheight()))
screen.title("Example Code")
screen.configure(bg="Gray")
# Canvas
canvas = Canvas(master=screen,width="666",height="666")
canvas.place(relx=0.5,rely=0.5,anchor=CENTER)

# Making The User
user = turtle.RawTurtle(canvas)
user.shape("triangle")
user.setheading(90)
user.speed(0)
user.color("black")
user.down()
user.goto(0,0)
userspeed = 15


# Moving Functions
def move_up(event=None):
    y = user.ycor()
    y += userspeed
    if y > 280:
        y = 280
    user.sety(y)
    user.setheading(90)  # Changes Direction of the Head


def move_down(event=None):
    y = user.ycor()
    y -= userspeed
    if y < -280:
        y = -280
    user.sety(y)
    user.setheading(-90)  # Changes Direction of the Head


def move_left(event=None):
    x = user.xcor()
    x -= userspeed
    if x < -280:
        x = - 280
    user.setx(x)
    user.setheading(60)  # Changes Direction of the Head


def move_right(event=None):
    x = user.xcor()
    x += userspeed
    if x > 280:
        x = 280
    user.setx(x)
    user.setheading(0)  # Changes Direction of the Head


# Keyboard Bindings For Moving Functions
# turtle.listen()
# turtle.onkey(move_up,"Up")
# turtle.onkey(move_down,"Down")
# turtle.onkey(move_left,"Left")
# turtle.onkey(move_right,"Right")
canvas.focus_set()
canvas.bind("<Up>",move_up)
canvas.bind("<Down>",move_down)
canvas.bind("<Left>",move_left)
canvas.bind("<Right>",move_right)
screen.mainloop()