【问题标题】:Python 3.0 using turtle.onclickPython 3.0 使用 turtle.onclick
【发布时间】:2013-03-31 08:51:51
【问题描述】:

所以这是我的问题,我必须为我的 CS 课制作一张照片,在海龟中进行估计真的很令人沮丧。我打算使用 .onclick() 来显示我的位置。

import turtle as t
def getPos(x,y):
    print("(", x, "," ,y,")")
    return

def main():
    t.onclick(getPos)
    t.mainloop()
main()

turtle 文档似乎说 onclick 将在一个接受两个变量的函数中传递坐标。

http://docs.python.org/3.1/library/turtle.html#turtle.onclick

注意:当我单击箭头时它会起作用,但这不是我想要的。我想点击屏幕上的其他位置来找出我应该将箭头发送到哪个坐标!

任何帮助将不胜感激。

【问题讨论】:

  • 这里有什么问题?
  • 这对我有用。您是否正确与海龟交互?你希望print 语句写到哪里?
  • 好的,我测试了它并在上面添加了一条注释。我不想点击箭头。我想点击没有箭头的空白区域,以便找出我想将箭头发送到的坐标。

标签: python onclick coordinates turtle-graphics


【解决方案1】:

您需要使用 Screen 类。但是,如果你想远离OOP,你可以使用内置方法turtle.onscreenclick(func)

替换

def main():
    t.onclick(getPos)
    t.mainloop()
main()

def main():
    t.onscreenclick(getPos)
    t.mainloop()
main()

【讨论】:

    【解决方案2】:

    自己找到解决方案真是太棒了。

    您是否曾经浏览过 turtle 的文档?

    http://docs.python.org/2/library/turtle.html

    看起来您可以从模块中导入screenturtlescreen 有一个自己的 onclick 事件,它可以满足您的期望。

    请注意以下有关如何访问 screen 对象的行:

    The function Screen() returns a singleton object of a TurtleScreen subclass.
    This function should be used when turtle is used as a standalone tool for
    doing graphics. As a singleton object, inheriting from its class is not
    possible.
    

    免责声明:我以前从未使用过海龟。

    【讨论】:

    • 我看了看。我不知道这是否有效。它似乎没有按照它所说的去做。不过感谢您的尝试。
    【解决方案3】:

    好的,我想出了一个解决方法。它不是一个完美的解决方案,但效果很好。因为只有当你点击箭头时 onclick 才会响应,所以我让箭头包含了整个屏幕。然后我把它藏了起来。您需要做的是将鼠标悬停在您想去的位置上,按“a”,当它变黑时单击屏幕。然后外壳将显示您需要的坐标。确保你总是回到 (1000,0)。

    import turtle as t
    
    def showTurtle():
        t.st()
        return
    
    def getPos(x,y):
        print("(", x, "," ,y,")")
        return
    
    def hideTurtle(x,y):
        t.ht()
        return
    
    def main():
        t.speed(20)
        t.shapesize(1000,1000)
        t.up()
        t.goto(1000,0)
        t.ht()
        t.onkey(showTurtle,"a")
        t.listen()
        t.onclick(getPos)
        t.onrelease(hideTurtle)
        t.mainloop()
    main()
    

    另外,如果我班上的任何人发现这个,我是宾厄姆顿的一名 CS 学生,如果你使用这个,我建议不要留下任何痕迹。教授已经看到了这一点,并会认出它。

    【讨论】:

      【解决方案4】:

      您必须首先获取海龟正在绘制的屏幕对象,然后调用屏幕对象的 onclick()。这是一个例子:

      import turtle as t
      
      def getPos(x,y):
          print("(", x, "," ,y,")")
          return
      
      def main():
          s = t.getscreen()
          s.onclick(getPos)
          t.mainloop()
      
      main()
      

      【讨论】:

        【解决方案5】:

        Python 3.7 版本。

        import turtle as t
        
        def click(x,y):
          print('Clicked:', x, y)
        
        def main():
          s = t.Screen()
          s.onclick(click)
        
          s.mainloop()
        
        main()
        

        请注意,打印是在控制台上进行的,而不是在 Turtle 上。

        【讨论】:

          【解决方案6】:

          我有一个小的儿童程序员,但我学到了很多东西。

          我喜欢只为有人说你创造了好东西而编写代码,但我也喜欢分享我学到的东西。

          您来这里是为了代码点击代码这里

          您需要执行以下操作:

          # import package 
          import turtle 
             
          # screen object 
          wn = turtle.Screen() 
            
          # method to perform action 
          def fxn(x, y): 
            turtle.goto(x, y) 
            turtle.write(str(x)+","+str(y)) 
            
          # onclick action  
          wn.onclick(fxn) 
          wn.mainloop()`
          

          现在,只需点击屏幕,就可以看到魔法了

          【讨论】:

          • 请在您的答案中添加一些解释,以便其他人可以从中学习
          猜你喜欢
          • 2019-11-08
          • 1970-01-01
          • 1970-01-01
          • 2011-01-14
          • 2021-06-19
          • 2023-03-21
          • 2010-10-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多