如何在没有收到响应消息的情况下保持python龟窗口打开

时间:2018-01-16 02:20:04

标签: python turtle-graphics

你可以保持一个python龟窗口打开而不会得到没有响应的消息。我制作了一个用户输入f,b,l或r的程序,并输入一个数字来表示该距离。但是,乌龟窗口总是说没有响应。"任何人都知道如何解决这个问题。我不是在寻找done()exitonclick()bye()。它们都不能在这种情况下工作,因为我想在不关闭的情况下使用同一个窗口。

这是我的代码:

import turtle as t
t.showturtle()


def main():
     ask = input("F, B, L, R, or EXIT: ")
     ask = ask.upper()
     if ask == "F":
         x = int(input("How much: "))
         t.forward(x)
         main()
     if ask == "B":
         x = int(input("How much: "))
         t.left(180)
         t.forward(x)
         main()
     if ask == "L":
         x = int(input("How much: "))
         t.left(x)
         main()
     if ask == "R":
         x = int(input("How much: "))
         t.right(x)
         main()
     if ask == "EXIT":
         quit()
     else:
         main()

main()

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

而不是在main()内执行main(),而是更好地使用while True循环

import turtle as t

def main():
    while True:
        ask = input("F, B, L, R, or EXIT: ")
        ask = ask.upper()
        if ask == "F":
            x = int(input("How much: "))
            t.forward(x)
        elif ask == "B":
            x = int(input("How much: "))
            t.backward(x)
        elif ask == "L":
            x = int(input("How much: "))
            t.left(x)
        elif ask == "R":
            x = int(input("How much: "))
            t.right(x)
        elif ask == "EXIT":
            return

t.showturtle()
main()

但是当GUI程序使用命令行获取值时,它看起来很奇怪。

但是turtleinput methods来获取值,您可以为按键分配功能。

import turtle as t

def on_forward():
    val = t.textinput('Forward', "How much: ")
    if val:
        t.forward(int(val))
    t.listen()

def on_backward():
    val = t.textinput('Backward', "How much: ")
    if val:
        t.backward(int(val))
    t.listen()

def on_left():
    val = t.textinput('Left', "How much: ")
    if val:
        t.left(int(val))
    t.listen()

def on_right():
    val = t.textinput('Right', "How much: ")
    if val:
        t.right(int(val))
    t.listen()

def on_exit():
    t.bye()  

t.showturtle()

# assign functions to keys
t.onkeypress(on_forward, 'f')
t.onkeypress(on_backward, 'b')
t.onkeypress(on_left, 'l')
t.onkeypress(on_right, 'r')
t.onkeypress(on_exit, 'e')

# listen keypresses on main window
t.listen()

# start mainloop which will run functions after key press
t.done()

编辑:使用tkinter消息框显示错误消息

import turtle as t
from tkinter import messagebox

def on_forward():
    val = t.textinput('Forward', "How much: ")
    if val:
        try:
            val = int(val)
            t.forward(val)
        except ValueError:
            messagebox.showinfo('Error', 'Wrong value')
    t.listen()

def on_backward():
    val = t.textinput('Backward', "How much: ")
    if val:
        try:
            val = int(val)
            t.backward(val)
        except ValueError:
            messagebox.showinfo('Error', 'Wrong value')
    t.listen()

def on_left():
    val = t.textinput('Left', "How much: ")
    if val:
        try:
            val = int(val)
            t.left(val)
        except ValueError:
            messagebox.showinfo('Error', 'Wrong value')
    t.listen()

def on_right():
    val = t.textinput('Right', "How much: ")
    if val:
        try:
            val = int(val)
            t.right(val)
        except ValueError:
            messagebox.showinfo('Error', 'Wrong value')
    t.listen()

def on_exit():
    t.bye()  

t.showturtle()

# assign functions to keys
t.onkeypress(on_forward, 'f')
t.onkeypress(on_backward, 'b')
t.onkeypress(on_left, 'l')
t.onkeypress(on_right, 'r')
t.onkeypress(on_exit, 'e')

# catch key presses in main window
t.listen()

# run loop which will execute functions 
t.mainloop()