graphics.py退出方法干扰Enter键检测

时间:2018-06-13 16:21:50

标签: python graphics

我正在开发一个Python graphics.py银行项目以获得乐趣,我遇到了问题。以下是我为单击退出按钮时出现的退出窗口编写的代码:

def exit():
exit_pop_up_page = GraphWin("Exit Page",400,200)
exit_pop_up_page.setBackground(color_rgb(255,255,0))
rect = Rectangle(Point(75,75),Point(325,125))
rect.setFill(color_rgb(224,102,255))
rect.draw(exit_pop_up_page)
exit_pop_up_page_text = Text(Point(200,37.5),"Are you sure you want to exit?")
exit_pop_up_page_text.setSize(20)
exit_pop_up_page_text.setFace("courier")
exit_pop_up_page_text.setTextColor(color_rgb(253,208,35))
exit_pop_up_page_text.draw(exit_pop_up_page)
yes_button = Rectangle(Point(125,85), Point(175,115))
yes_button.setFill("white")
yes_button.setOutline("black")
yes_button.setWidth(2)
yes_button.draw(exit_pop_up_page)
yes_button_text = Text(Point(150,100), "Yes")
yes_button_text.setSize(20)
yes_button_text.setFace("courier")
yes_button_text.draw(exit_pop_up_page)
no_button = Rectangle(Point(225,85), Point(275,115))
no_button.setFill("white")
no_button.setOutline("black")
no_button.setWidth(2)
no_button.draw(exit_pop_up_page)
no_button_text = Text(Point(250,100), "No")
no_button_text.setSize(20)
no_button_text.setFace("courier")
no_button_text.draw(exit_pop_up_page)
yes_or_no_clicked = False
while yes_or_no_clicked == False:
    exit_pop_up_page_click = exit_pop_up_page.getMouse()
    if exit_pop_up_page_click.isIn(yes_button):
        yes_or_no_clicked = True
        return True
    if exit_pop_up_page_click.isIn(no_button):
        yes_or_no_clicked = True
        exit_pop_up_page.close()
        return False

此代码运行正常,但出现以下情况时会出现问题:

while leave == False:
    get_balance_by_click = login_page.checkMouse()
    get_balance_by_enter_press = login_page.checkKey()
    if get_balance_by_click  != None:
        if get_balance_by_click.isIn(get_balance_enter_button):
            leave = True
        elif get_balance_by_click.isIn(exit_button):
            exit_bank = exit()
            if exit_bank == True:
                return True
    elif get_balance_by_enter_press == "Return":
        leave = True

在上面的代码片段中,我编写了代码,用于在用户单击按钮或按Enter键时进行注册。起初它似乎完美无缺。当用户按下或单击进入时程序继续,如果按下退出按钮则退出并且用户在退出窗口中按“是”以确保他们想要退出。然而,经过仔细检查,我意识到在用户触发退出屏幕但按“否”返回当前窗口的情况下,程序仅在用户单击“输入”按钮时继续,而不是在用户按下Enter时键盘上的键。窗户的图像在下面。关于为什么会发生这种情况的任何想法?

example window

0 个答案:

没有答案