仅在触发另一个关键事件之后,如何才能触发关键事件?

发布于 2025-01-24 12:44:42 字数 848 浏览 2 评论 0原文

我是一个学习Python的学生,如果你们能帮助我,那就太好了。

这是一个临时最小代码段,Window1是一个预制变量,可在功能之外创建矩形。

def key_pressed(event):
    if event.key == "a":
        add(window1)
    else:    
        if event.key == "n":
            name = str(input("Task Name: "))
            tname = Text(name)
            tname.set_position(get_width(), get_height())
            add(tname)              
        elif event.key == "t":
            time = int(input("Due Time: "))
            ttime = Text(str(time))
            add(tname)
    if event.key == "Escape":
        remove(window1)
        remove(tname)
        remove(ttime)

add_key_down_handler(key_pressed)

等。

我在网站Codehs上使用Python 3图形(Brython),我希望我的程序仅在按下另一个事件后才允许关键事件。我已经尝试嵌套IF语句和其他内容,但我似乎无法使其工作。另外,event.key ==“逃脱”不会删除tname和ttime图形。

我该如何做到,以便只能在“ a”后按下“ n”和“ t”。

谢谢

I am a student learning Python, it would be great if you guys can help me out with this.

This is a temporary minimum code segment, window1 is a premade variable that creates a rectangle outside the function.

def key_pressed(event):
    if event.key == "a":
        add(window1)
    else:    
        if event.key == "n":
            name = str(input("Task Name: "))
            tname = Text(name)
            tname.set_position(get_width(), get_height())
            add(tname)              
        elif event.key == "t":
            time = int(input("Due Time: "))
            ttime = Text(str(time))
            add(tname)
    if event.key == "Escape":
        remove(window1)
        remove(tname)
        remove(ttime)

add_key_down_handler(key_pressed)

etc.etc.

I'm using Python 3 Graphics (Brython) on the website CodeHS, and I want my program to allow a key event only after another one has been pressed. I've tried nesting the if statements and other stuff but I can't seem to get it to work. Plus, the event.key == "Escape" does not remove the tname and ttime graphics.

How can I make it so that 'n' and 't' can only be pressed after 'a' is pressed and not be pressed after 'esc' when it removes the window?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亚希 2025-01-31 12:44:42

我也是一名学生(目前也在做Codehs),我试图找出一种解决这个问题的方法。这是您的代码的编辑版本(用您自己的代码替换“成功”消息,但是由于我没有窗口1,所以我决定使用简单的“打印”函数)。

has_a_yet = False

def callback(event):
    key = event.key
    print([key])
    
if key == 'a':
    print("A SUCCESSFUL")
    has_a_yet = True
    
if has_a_yet:
    if key == "n":
        name = str(input("Task Name: "))
        tname = Text(name)
        tname.set_position(get_width(), get_height())
        add(tname)              
    elif key == "t":
        time = int(input("Due Time: "))
        ttime = Text(str(time))
        add(tname)

if key == "Escape":
    print("remove all objects")

add_key_down_handler(callback)

I'm also a student (also doing CodeHS currently) and I tried to figure out a way to solve this. Here's an edited version of your code (replace the "A SUCCESSFUL" message with your own code, but since I didnt have a window1 I decided to use a simple "print" function instead).

has_a_yet = False

def callback(event):
    key = event.key
    print([key])
    
if key == 'a':
    print("A SUCCESSFUL")
    has_a_yet = True
    
if has_a_yet:
    if key == "n":
        name = str(input("Task Name: "))
        tname = Text(name)
        tname.set_position(get_width(), get_height())
        add(tname)              
    elif key == "t":
        time = int(input("Due Time: "))
        ttime = Text(str(time))
        add(tname)

if key == "Escape":
    print("remove all objects")

add_key_down_handler(callback)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文