Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我只需要一个屏幕,却有两个屏幕?_Python_Python 3.x_Tkinter_Turtle Graphics_Python Turtle - Fatal编程技术网

Python 为什么我只需要一个屏幕,却有两个屏幕?

Python 为什么我只需要一个屏幕,却有两个屏幕?,python,python-3.x,tkinter,turtle-graphics,python-turtle,Python,Python 3.x,Tkinter,Turtle Graphics,Python Turtle,基本上,我用turtle制作了一个应用程序,我想把turtle应用程序的内容放到Tkinter画布上。但是,当我运行代码时,当我只需要一个屏幕时,会创建两个屏幕 下面是示例代码: from tkinter import * import turtle import time # Screen screen = Tk() screen.geometry("{0}x{1}+0+0".forma

基本上,我用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导入*
进口海龟
导入时间
#屏风
screen=Tk()
几何体(“{0}x{1}+0+0.”格式(screen.winfo_screenwidth(),screen.winfo_screenheight())
屏幕标题(“示例代码”)
屏幕配置(bg=“灰色”)
#帆布
画布=画布(屏幕,宽度=“666”,高度=“666”)
canvas.place(relx=0.5,rely=0.5,anchor=CENTER)
#使用户
user=turtle.RawTurtle(画布)
user.shape(“三角形”)
用户设置标题(90)
用户速度(0)
user.color(“黑色”)
user.down()
user.goto(0,0)
用户速度=15
#移动函数
def上移()
y=user.ycor()
y+=用户速度
如果y>280:
y=280
user.sety(y)
用户设置标题(90)#改变头部方向
def move_down():
y=user.ycor()
y-=用户速度
如果y<-280:
y=-280
user.sety(y)
user.setheading(-90)#更改头部的方向
def向左移动()
x=user.xcor()
x-=用户速度
如果x<-280:
x=-280
user.setx(x)
用户设置标题(60)#改变头部方向
def向右移动()
x=user.xcor()
x+=用户速度
如果x>280:
x=280
user.setx(x)
user.setheading(0)#更改头部的方向
#移动函数的键盘绑定
乌龟,听着
乌龟。上键(向上移动,“向上”)
乌龟。上键(向下移动,“向下”)
乌龟。上键(向左移动,“左”)
乌龟.安基(向右移动,“向右”)
screen.mainloop()

当我运行这个程序时,会创建两个屏幕(Tkinter屏幕和turtle屏幕),我只需要Tkinter屏幕。但是,当我关闭turtle屏幕时,键盘绑定在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()
从tkinter导入*
进口海龟
导入时间
#屏风
screen=Tk()
几何体(“{0}x{1}+0+0.”格式(screen.winfo_screenwidth(),screen.winfo_screenheight())
屏幕标题(“示例代码”)
屏幕配置(bg=“灰色”)
#帆布
画布=画布(主屏幕=屏幕,宽度=“666”,高度=“666”)
canvas.place(relx=0.5,rely=0.5,anchor=CENTER)
#使用户
user=turtle.RawTurtle(画布)
user.shape(“三角形”)
用户设置标题(90)
用户速度(0)
user.color(“黑色”)
user.down()
user.goto(0,0)
用户速度=15
#移动函数
def上移(事件=无):
y=user.ycor()
y+=用户速度
如果y>280:
y=280
user.sety(y)
用户设置标题(90)#改变头部方向
def下移(事件=无):
y=user.ycor()
y-=用户速度
如果y<-280:
y=-280
user.sety(y)
user.setheading(-90)#更改头部的方向
def向左移动(事件=无):
x=user.xcor()
x-=用户速度
如果x<-280:
x=-280
user.setx(x)
用户设置标题(60)#改变头部方向
def向右移动(事件=无):
x=user.xcor()
x+=用户速度
如果x>280:
x=280
user.setx(x)
user.setheading(0)#更改头部的方向
#移动函数的键盘绑定
#乌龟,听着
#乌龟。上键(向上移动,“向上”)
#乌龟。上键(向下移动,“向下”)
#乌龟。上键(向左移动,“左”)
#乌龟.安基(向右移动,“向右”)
canvas.focus_set()
canvas.bind(“,向上移动)
canvas.bind(“,下移)
canvas.bind(“,向左移动)
canvas.bind(“,向右移动”)
screen.mainloop()