为什么我只想要一个屏幕却有两个屏幕?

Why are there two screens when I only want one?

基本上我在 turtle 中创建了一个应用程序,我想将 turtle 应用程序的内容放到 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(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 屏幕。但是,当我关闭 turtle 屏幕时,键盘绑定在 Tkinter 屏幕上不起作用。我该如何解决这个问题?

删除 turtle.Just 的事件,将其添加到 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()