事件内部功能无法正常工作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了事件内部功能无法正常工作相关的知识,希望对你有一定的参考价值。

我有这个代码定义了一个drawScene0函数,该函数应该与用户交互,响应鼠标点击和键盘,但它似乎没有它只是绘制,当我尝试点击或键入时,它不起作用。我尝试在drawcene函数以及主while循环中调试事件循环,我发现主while循环中的事件循环似乎更快更好并且响应完美,另一方面他在drawcene函数内部的事件循环比较慢得多,甚至有点甚至没有响应点击十分之一点击正在打印,甚至一次点击没有做他应该做的事我真的很困惑。请帮忙。谢谢!我发布了完整的代码以获得最大的理解,请不要介意搞砸的代码。这是代码:

import pygame as pg

pg.init()

#       MAIN DISPLAY

displayHeight = 400
displayWidth = 400
programDisplay = pg.display.set_mode((displayHeight, displayWidth))
pg.display.set_caption('Scene0Prep')
#       TRANSPARENT SCREEN

s = pg.Surface((displayHeight, displayWidth), pg.SRCALPHA)

#       VARIABLES

currentScene = None

clock = pg.time.Clock()

font = pg.font.SysFont("PierSansLight", 20)

#       VARIABLES SECTION ENDS

#       DEFINE FUNCTIONS HERE

def drawScene_0 () :
    currentScene = 0
    #   Variables
    input_box = pg.Rect(150, 150, 100, 100)
    inactiveColor = pg.Color(105, 105, 105)
    activeColor = pg.Color(185, 185, 185)
    color = inactiveColor
    active = False
    inputTextN = ''
    #   Variables end

    def display_message (msg, xPos, yPos, color) :
        textS = font.render(msg, True, color)
        programDisplay.blit(textS, [xPos, yPos])

    def draw_bg () :
        rectNo = 0
        pg.draw.rect(s, (0, 0, 0, 100), [0, 0, 400, 400])
        while rectNo < 30 :
            boxWHi = (rectNo + 1) * 10
            boxHf = displayHeight - boxWHi
            boxWf = displayWidth - boxWHi
            boxXY = (rectNo + 1) * 5
            pg.draw.rect(s, (0, 0, 0, 100 +(rectNo*3.5)), [boxXY, boxXY, boxWf, boxHf])
            rectNo += 1

    # EVENTS
    for event in pg.event.get():
        '''click on box'''
        if event.type == pg.MOUSEBUTTONDOWN :
            print('mouseDOWN')
            '''active or not'''
            if input_box.collidepoint(event.pos) :
                active = not active
                print(active)
            else :
                active = False
            '''changes color'''
            color = activeColor if active else inactiveColor
        '''done clicking'''
        '''now for typing action'''
        if event.type == pg.KEYDOWN :
            if active :
                '''when user hits enter'''
                if event.key == pg.K_RETURN :
                    # basically save the input text, in this case printing it up
                    print (inputTextN)
                    # empty the text
                    inputTextN = ''
                elif event.key == pg.K_BACKSPACE:
                    '''when user hits backspace delete one'''
                    inputTextN = inputTextN[:-1]
                elif len(inputTextN) < 4 :
                    '''typing'''
                    inputTextN += events.unicode
    # DONE EVENTS

    draw_bg()
    txt = font.render(inputTextN, True, (0, 0, 0))
    programDisplay.blit(txt, (input_box.x + 33, input_box.y + 35))
    pg.draw.rect (programDisplay, color, input_box, 2)
    display_message("Please Enter Your Name Below", 60, 40, (0, 0, 0))
    return inputTextN

#   drawScene_0 ENDS

#       FUNCTION SECTION ENDS

done = False

while not done:
    #       EVENT HANDLING HERE

    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    #       FILL THE SCREENS
    programDisplay.fill ((255, 255, 255))
    s.fill((255, 255, 255, 0))

    #       EVENTS END

    #       CODE GOES HERE!
    drawScene_0 ()


    #       BLIT AND UPDATE SCREEN + FPS

    programDisplay.blit(s, (0, 0))
    pg.display.update()
    clock.tick(30)

pg.quit()
quit()
答案

我已重新安排您的代码以处理一个位置的事件。我做了很少的更改,但您应该考虑查看Python Style Guide以提高代码的可读性。代码似乎正常工作,如果您有任何问题,请告诉我们。

import pygame as pg

pg.init()
#       MAIN DISPLAY
displayHeight = 400
displayWidth = 400
programDisplay = pg.display.set_mode((displayHeight, displayWidth))
pg.display.set_caption('Scene0Prep')
#       TRANSPARENT SCREEN
s = pg.Surface((displayHeight, displayWidth), pg.SRCALPHA)

#       VARIABLES
currentScene = None
clock = pg.time.Clock()
font = pg.font.SysFont("PierSansLight", 20)
#       VARIABLES SECTION ENDS

#       DEFINE FUNCTIONS HERE
def draw_bg () :
    rectNo = 0
    pg.draw.rect(s, (0, 0, 0, 100), [0, 0, 400, 400])
    while rectNo < 30 :
        boxWHi = (rectNo + 1) * 10
        boxHf = displayHeight - boxWHi
        boxWf = displayWidth - boxWHi
        boxXY = (rectNo + 1) * 5
        pg.draw.rect(s, (0, 0, 0, 100 +(rectNo*3.5)), [boxXY, boxXY, boxWf, boxHf])
        rectNo += 1

def display_message (msg, xPos, yPos, color) :
    textS = font.render(msg, True, color)
    programDisplay.blit(textS, [xPos, yPos])

def drawScene_0 () :
    draw_bg()
    txt = font.render(inputTextN, True, (0, 0, 0))
    programDisplay.blit(txt, (input_box.x + 33, input_box.y + 35))
    pg.draw.rect (programDisplay, color, input_box, 2)
    display_message("Please Enter Your Name Below", 60, 40, (0, 0, 0))

#       FUNCTION SECTION ENDS
done = False

currentScene = 0
#   Variables
input_box = pg.Rect(150, 150, 100, 100)
inactiveColor = pg.Color(105, 105, 105)
activeColor = pg.Color(185, 185, 185)
color = inactiveColor
active = False
inputTextN = ''

while not done:
    #       EVENT HANDLING HERE
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.MOUSEBUTTONDOWN :
            print('mouseDOWN')
            '''active or not'''
            if input_box.collidepoint(event.pos) :
                active = not active
                print(active)
            else :
                active = False
            '''changes color'''
            color = activeColor if active else inactiveColor
        elif event.type == pg.KEYDOWN :
            if active :
                '''when user hits enter'''
                if event.key == pg.K_RETURN :
                    # basically save the input text, in this case printing it up
                    print (inputTextN)
                    # empty the text
                    inputTextN = ''
                elif event.key == pg.K_BACKSPACE:
                    '''when user hits backspace delete one'''
                    inputTextN = inputTextN[:-1]
                elif len(inputTextN) < 4 :
                    '''typing'''
                    inputTextN += event.unicode

    #       FILL THE SCREENS
    programDisplay.fill ((255, 255, 255))
    s.fill((255, 255, 255, 0))
    #       EVENTS END
    #       CODE GOES HERE!
    drawScene_0 ()
    #       BLIT AND UPDATE SCREEN + FPS
    programDisplay.blit(s, (0, 0))
    pg.display.update()
    clock.tick(30)

pg.quit()
quit()

以上是关于事件内部功能无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

使用GestureDetector后,单击或选择内部WebView无法正常工作

后堆栈在 Jetpack Navigation 中无法正常工作

带有复选框和自定义适配器的 ListView,片段无法正常工作

突出显示功能(mouseover / mouseout事件侦听器)无法正常工作

使用片段共享过渡时返回过渡无法正常工作

OnClick 在片段的回收器适配器中无法正常工作