仅在触发另一个关键事件之后,如何才能触发关键事件?
我是一个学习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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也是一名学生(目前也在做Codehs),我试图找出一种解决这个问题的方法。这是您的代码的编辑版本(用您自己的代码替换“成功”消息,但是由于我没有窗口1,所以我决定使用简单的“打印”函数)。
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).