Python 如何从GUI调用Turtle窗口

Python 如何从GUI调用Turtle窗口,python,tkinter,python-turtle,Python,Tkinter,Python Turtle,我试图从GUI中打开一个Turtle窗口,用鼠标单击在图像上选择一个位置。然后将x和y坐标作为输入返回到GUI 下面是一个简单的工作示例: from tkinter import * from tkinter import font as tkFont import turtle xclick = 0 yclick = 0 def callback3(): getcoordinates() def getcoordinates(): screen = turtle.Scr

我试图从GUI中打开一个Turtle窗口,用鼠标单击在图像上选择一个位置。然后将x和y坐标作为输入返回到GUI

下面是一个简单的工作示例:

from tkinter import *
from tkinter import font as tkFont
import turtle

xclick = 0
yclick = 0

def callback3():
    getcoordinates()


def getcoordinates():
    screen = turtle.Screen()
    screen.setup(400, 400)
    screen.bgpic("stunden.gif")
    screen.onscreenclick(modifyglobalvariables)

    
def modifyglobalvariables(rawx,rawy):
    global xclick
    global yclick
    xclick = int(rawx//1)
    yclick = int(rawy//1)
    print(xclick)
    print(yclick)
    turtle.bye()


root = Tk()

helv30 = tkFont.Font(family='Helvetica', size=30)

button1 = Button(root, text = "1", width=3, font=helv30, borderwidth=0, command=callback3)
button1.grid(row=0, column=0, padx=5, pady=0)

root.mainloop()

然后显示错误图像pyimage2不存在。我发现,它与Tk的两个实例有关,因为有根和turtle窗口,我应该用Toplevel解决它。然而,经过数小时的研究和反复尝试,我仍然无法找到正确的解决方案来让我的代码正常工作。非常感谢您提供的任何帮助。

以下是如何在tkinter中直接实现它-因此不需要turtle模块-正如我之前在现已删除的评论中所建议的:

from tkinter import *
from tkinter import font as tkFont

CLOCK_IMAGE_PATH = 'clock.png'
xclick, yclick = 0, 0

def callback3():
    window = Toplevel()
    window.title("Stunden")
    img = PhotoImage(file=CLOCK_IMAGE_PATH)
    img_width, img_height = img.width(), img.height()
    window.minsize(width=img_width, height=img_height)
    canvas = Canvas(window, width=img_width, height=img_height)
    canvas.pack()
    canvas.image = img  # Keep reference to avoid image being garbage collected.
    canvas.create_image(0, 0, anchor='nw', image=img)
    canvas.bind("<Button-1>", get_coordinates)  # Set up event-handler callback.

def get_coordinates(event):
    global xclick, yclick
    xclick = int(event.x // 1)
    yclick = int(event.y // 1)
    print(f'xclick={xclick}, yclick={yclick}')

root = Tk()
helv30 = tkFont.Font(family='Helvetica', size=30)
button1 = Button(root, text="1", width=3, font=helv30, borderwidth=0, command=callback3)
button1.grid(row=0, column=0, padx=5, pady=0)
root.mainloop()

请注意,您的问题标记错误,应该是tkinter而不是tinker。无论如何,我的建议是不要用海龟来做这件事。充其量这会很尴尬,因为该模块使用tkinter实现——尤其是在这种情况下,因为您可以通过tkinter本身直接轻松地完成同样的事情。谢谢您的建议martineau。你能告诉我怎么直接用tkinter吗?