结束Python乌龟绘图,以便用户返回主菜单 [英] End Python turtle drawing so user goes back to main menu

查看:135
本文介绍了结束Python乌龟绘图,以便用户返回主菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写代码,以便当用户单击窗口时,它将根据他们选择的输入来绘制形状.我在正确分配窗口和乌龟的位置以及如何为函数分配退出当前窗口的方式方面遇到麻烦.我有什么办法可以结束mainloop(),以便用户在单击并制作尽可能多的形状后返回主菜单(假设每次选择一个选项时,窗口都会重置为空白状态) )?如果无法无限点击,那么只有1次点击(然后返回主菜单),有什么方法可以做到?为简单起见,我仅包含一个选项,因为其他代码将相同.

I'm making a code so that when a user clicks on the window, it will draw the shape based on their input of choice. I'm having trouble on where I should assign the window and turtle correctly and how to assign to a function a way to quit the window its currently on. Is there any way for me to end a mainloop() so that the user goes back to the main menu after clicking and making as many of their chosen shapes (given that each time they do choose an option, the window resets to a blank state)? If limitless clicking is not possible, then is there any way to do it but with only 1 click (then goes back to the main menu)? For simplicity, I only included one option because the other codes will be the same.

import turtle
window = turtle.Screen()
turt = turtle.Turtle()

def U(x,y):
    turt.penup()
    turt.setposition(x,y)
    turt.pendown()
    for x in range(4):
        turt.forward(100)
        turt.right(90)
    return

def main():
    global choice
    if choice.upper() == "U":
        window.onscreenclick(U)
        window.mainloop()

run = True
print("Please choose a drawing option: 'U' or 'q' to quit.")
while run == True:
    choice = str(input())
    if choice.upper() != "U" and choice.lower() != "q":
        print("Invalid input. Please choose either: 'U' or 'q'.")
    elif choice.upper() == "U":
        main()
    elif choice.lower() == 'q':
        run = False

推荐答案

您的控制逻辑有缺陷. Turtle图形是一个事件驱动的世界,因此我们需要放置事件逻辑,然后将控制权移至mainloop(),以监视键盘和鼠标事件.像while Trueinput()这样的代码与该模型合谋.这是我对您的代码进行的修改,以便在Python 3中以事件驱动的方式进行操作.

Your control logic is flawed. Turtle graphics is an event driven world so we need to put the event logic in place and then turn control over to mainloop() to watch for keyboard and mouse events. Code like while True and input() conspire against this model. Here's my rework of your code to operate in an event driven way in Python 3:

from turtle import Turtle, Screen

TITLE = 'Drawing Options'
PROMPT = "Please choose a drawing option: 'U' or 'q' to quit."

def U(x, y):
    window.onscreenclick(None)

    turtle.penup()
    turtle.setposition(x, y)
    turtle.pendown()

    for _ in range(4):
        turtle.forward(100)
        turtle.right(90)

def menu():
    choice = ''
    while choice not in ['U', 'u', 'Q', 'q']:
         choice = window.textinput(TITLE, PROMPT)  # may return None

    if choice.upper() == 'U':
        window.onscreenclick(U)
    elif choice.lower() == 'q':
        window.bye()

    window.listen()  # textinput() unsets this so redo

turtle = Turtle()
window = Screen()

window.onkey(menu, key='M')
window.listen()

window.mainloop()

运行它,选择一个窗口使其成为侦听器,然后按大写的"M"以打开一个提示窗口,您可以在其中输入"U"或"q"命令.您可以在"U"命令后用鼠标单击任意位置以设置正方形位置.完成此操作后,您可以再次输入大写字母"M"以调出菜单并放置另一个正方形或退出.

Run it, select the window to make it the listener, then hit capital 'M' to bring up a prompt window where you can input your 'U' or 'q' commands. You can click anywhere with the mouse after the 'U' command to set a position with your square. Once this completes, you can again type capital 'M' to bring up the menu and place another square or quit.

为简单起见,我没有将窗口重置为空白状态.

For simplicity, I left out resetting the window to a blank state.

这篇关于结束Python乌龟绘图,以便用户返回主菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆