【问题标题】:How to clear screen after clicking button?点击按钮后如何清屏?
【发布时间】:2017-06-11 03:50:35
【问题描述】:

我正在尝试构建一个基于菜单的程序,如果我点击一个按钮,它对应一个功能;它会执行某些条件。但是,我无法让按钮本身工作。我希望拥有它,以便当我单击按钮时,屏幕会填充另一种颜色,这样我就可以拥有另一个屏幕,它具有单击的任何功能。我在下面添加了我的代码;非常感谢任何帮助!

这是我一直遇到问题的按钮:

def arrayIntro():
intro = True
while intro:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONUP:
            mousePosition = pygame.mouse.get_pos()                             

    screen.fill(MUTEDBLUE)

    textChoice = pygame.font.SysFont('americantypewriter', 20)

    mousePosition = pygame.mouse.get_pos()
    mouseClick = pygame.mouse.get_pressed()

#Top Row Draw Rects        

    if 50+250 > mousePosition[0] > 50 and 50 + 100 > mousePosition[1] > 50:
        pygame.draw.rect(screen, GRAY, (50, 50, 250, 100))
    else:
        pygame.draw.rect(screen, WHITE, (50, 50, 250, 100))

    enterInt = textChoice.render("enterInt", True, NAVYBLUE)
    screen.blit(enterInt, (135, 90))

    if mouseClick[0] ==1:
        screen.fill(MUTEDBLUE)

但是,如果有必要,这里是我所有的代码:

 #Import the necessary modules
import pygame
import sys
import os

#Initialize pygame
pygame.init()

# Set the size for the surface (screen)
screenSize = (900,750)
screen = pygame.display.set_mode((screenSize),0)

# Set the caption for the screen
pygame.display.set_caption("Array")

#Define Colours
WHITE = (255,255,255)
BLUE = (0,0,255)
BLACK = (0,0,0)
GRAY = (128, 128, 128)
MAROON = (128, 0, 0)
NAVYBLUE = (0, 0, 128)
OLIVE = (128, 128, 0)
PURPLE = (128, 0, 128)
TEAL = (0,128,128)
PINK = (226,132,164)
MUTEDBLUE = (155,182,203)
PLUM = (221,160,221)   


def selectedFirst():
    runFirst = True

    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def enterInt():
    runFirst = True

    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def display():
    runSecond = True

    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def countInt():
    runFirst = True

    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def displayReverse():

    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def sumInt():
    runFirst = True

    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def average():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def findMaxORfindMin():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def search():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def writeToText():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def fillFromText():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def searchReplace():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def shuffleArray():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def wordCount():
    runFirst = True
    while runFirst:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(MUTEDBLUE)
        pygame.display.update()

def arrayIntro():
    buttonenterInt = pygame.Rect(50, 50, 250, 100)
    buttondisplay =pygame.Rect(325, 50, 250, 100)
    buttoncountInt = pygame.Rect(600, 50, 250, 100)

    buttondisplayReverse = pygame.Rect(50, 250, 250, 100)
    buttonsumInt = pygame.Rect(325, 250, 250, 100)
    buttonaverage = pygame.Rect(600, 250, 250, 100)

    buttonfindMaxORfindMin = pygame.Rect(50, 450, 250, 100)
    buttonsearch = pygame.Rect(325, 450, 250, 100)
    buttonwriteToText= pygame.Rect(600, 450, 250, 100)

    buttonfillFromText =pygame.Rect(50, 640, 250, 100)
    buttonsearchReplace = pygame.Rect(325, 640, 250, 100)
    buttonwordCount = pygame.Rect(600, 640, 250, 100)

    intro = True
    while intro:
        mousePosition = pygame.mouse.get_pos()
        screen.fill(MUTEDBLUE)
        textChoice = pygame.font.SysFont('americantypewriter', 20)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
                clicked = pygame.mouse.get_pos()

                if buttonenterInt.collidepoint(clicked):
                    selectedFirst()

                elif buttondisplay.collidepoint(clicked):
                    display()

                elif buttoncountInt.collidepoint(clicked):
                    countInt()

                elif buttondisplayReverse.collidepoint(clicked):
                    displayReverse()

                elif buttonsumInt.collidepoint(clicked):
                    sumInt()

                elif buttonaverage.collidepoint(clicked):
                    average()

                elif buttonfindMaxORfindMin.collidepoint(clicked):
                    findMaxORfindMin()

                elif buttonsearch.collidepoint(clicked):
                    search()

                elif buttonwriteToText.collidepoint(clicked):
                    writeToText()

                elif buttonfillFromText.collidepoint(clicked):
                    fillFromText()

                elif buttonsearchReplace.collidepoint(clicked):
                    searchReplace()

                elif buttonshuffleArray.collidepoint(clicked):
                    shuffleArray()

                elif buttonwordCount.collidepoint(clicked):
                    wordCount() 
#########################################################################################
#Top Row Draw Rects
        if 50+250 > mousePosition[0] > 50 and 50 + 100 > mousePosition[1] > 50:
            pygame.draw.rect(screen, GRAY, (50, 50, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (50, 50, 250, 100))

        enterInt = textChoice.render("enterInt", True, NAVYBLUE)
        screen.blit(enterInt, (135, 90))

        if 325+250 > mousePosition[0] > 325 and 50 + 100 > mousePosition[1] > 50:
            pygame.draw.rect(screen, GRAY, (325, 50, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (325, 50, 250, 100))

        display = textChoice.render("display", True, NAVYBLUE)
        screen.blit(display, (414, 90))

        if 600+250 > mousePosition[0] > 600 and 50 + 100 > mousePosition[1] > 50:
            pygame.draw.rect(screen, GRAY, (600, 50, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (600, 50, 250, 100))

        countInt = textChoice.render("countInt", True, NAVYBLUE)
        screen.blit(countInt, (680, 90))

#########################################################################################
#Middle Row Draw Rects
        if 50+250 > mousePosition[0] > 50 and 250 + 100 > mousePosition[1] > 250:
            pygame.draw.rect(screen, GRAY, (50, 250, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (50, 250, 250, 100))

        displayReverse = textChoice.render("displayReverse", True, NAVYBLUE)
        screen.blit(displayReverse, (102, 287))

        if 325+250 > mousePosition[0] > 325 and 250 + 100 > mousePosition[1] > 250:
            pygame.draw.rect(screen, GRAY, (325, 250, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (325, 250, 250, 100))

        sumInt = textChoice.render("sumInt", True, NAVYBLUE)
        screen.blit(sumInt, (410, 287))

        if 600+250 > mousePosition[0] > 600 and 250 + 100 > mousePosition[1] > 250:
            pygame.draw.rect(screen, GRAY, (600, 250, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (600, 250, 250, 100))

        average = textChoice.render("average", True, NAVYBLUE)
        screen.blit(average, (685, 287))

#########################################################################################
#Middle Bottom Row Draw Rects
        if 50+250 > mousePosition[0] > 50 and 450 + 100 > mousePosition[1] > 450:
            pygame.draw.rect(screen, GRAY, (50, 450, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (50, 450, 250, 100))

        findMaxORfindMin = textChoice.render("findMax/findMin", True, NAVYBLUE)
        screen.blit(findMaxORfindMin, (95, 488))

        if 325+250 > mousePosition[0] > 325 and 450 + 100 > mousePosition[1] > 450:
            pygame.draw.rect(screen, GRAY, (325, 450, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (325, 450, 250, 100))

        search = textChoice.render("search", True, NAVYBLUE)
        screen.blit(search, (417, 488))

        if 600+250 > mousePosition[0] > 600 and 450 + 100 > mousePosition[1] > 450:
            pygame.draw.rect(screen, GRAY, (600, 450, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (600, 450, 250, 100))

        writeToText = textChoice.render("writeToText", True, NAVYBLUE)
        screen.blit(writeToText, (665, 488))

#########################################################################################
#Bottom Row Draw Rects

        if 50+250 > mousePosition[0] > 50 and 640 + 100 > mousePosition[1] > 640:
            pygame.draw.rect(screen, GRAY, (50, 640, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (50, 640, 250, 100))

        fillFromText = textChoice.render("fillFromText", True, NAVYBLUE)
        screen.blit(fillFromText, (115, 680))

        if 325+250 > mousePosition[0] > 325 and 640 + 100 > mousePosition[1] > 640:
            pygame.draw.rect(screen, GRAY, (325, 640, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (325, 640, 250, 100))

        searchReplace = textChoice.render("searchReplace", True, NAVYBLUE)
        screen.blit(searchReplace, (382, 680))

        if 600+250 > mousePosition[0] > 600 and 640 + 100 > mousePosition[1] > 640:
            pygame.draw.rect(screen, GRAY, (600, 640, 250, 100))
        else:
            pygame.draw.rect(screen, WHITE, (600, 640, 250, 100))

        wordCount = textChoice.render("wordCount", True, NAVYBLUE)
        screen.blit(wordCount, (670, 680))

        pygame.display.update()
#########################################################################################
    eval(selected)

arrayIntro()

【问题讨论】:

    标签: python python-3.x button pygame


    【解决方案1】:

    while 循环无限期地继续运行。在当前代码中,您检测到鼠标点击,然后屏幕变蓝,但只是瞬间,然后它继续执行下一个函数,直到它到达末尾,循环继续,从而完全发生在上次迭代中发生的事情。

    看看下面的代码。我稍微改了一下

    #Import the necessary modules
    import pygame
    import sys
    import os
    
    #Initialize pygame
    pygame.init()
    
    # Set the size for the surface (screen)
    screenSize = (900,750)
    screen = pygame.display.set_mode((screenSize),0)
    
    # Set the caption for the screen
    pygame.display.set_caption("Array")
    
    #Define Colours
    WHITE = (255,255,255)
    BLUE = (0,0,255)
    BLACK = (0,0,0)
    GRAY = (128, 128, 128)
    MAROON = (128, 0, 0)
    NAVYBLUE = (0, 0, 128)
    OLIVE = (128, 128, 0)
    PURPLE = (128, 0, 128)
    TEAL = (0,128,128)
    PINK = (226,132,164)
    MUTEDBLUE = (155,182,203)
    PLUM = (221,160,221)
    
    #function here
    def selectedFirst():
        runFirst = True
    
        while runFirst:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
            screen.fill(MUTEDBLUE)
            pygame.display.update()
    
    def arrayIntro():
        intro = True
        while intro:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.MOUSEBUTTONUP:
                    mousePosition = pygame.mouse.get_pos()
    
            screen.fill(MUTEDBLUE)
    
            textChoice = pygame.font.SysFont('americantypewriter', 20)
    
            mousePosition = pygame.mouse.get_pos()
            mouseClick = pygame.mouse.get_pressed()
    
    #Top Row Draw Rects
            if 50+250 > mousePosition[0] > 50 and 50 + 100 > mousePosition[1] > 50:
                pygame.draw.rect(screen, GRAY, (50, 50, 250, 100))
            else:
                pygame.draw.rect(screen, WHITE, (50, 50, 250, 100))
    
            enterInt = textChoice.render("enterInt", True, NAVYBLUE)
            screen.blit(enterInt, (135, 90))
    
            if mouseClick[0] ==1:
                selectedFirst() #change here. Made a new function temporarily to be executed on the click
    
    
            if 325+250 > mousePosition[0] > 325 and 50 + 100 > mousePosition[1] > 50:
                pygame.draw.rect(screen, GRAY, (325, 50, 250, 100))
            else:
                pygame.draw.rect(screen, WHITE, (325, 50, 250, 100))
    
            display = textChoice.render("display", True, NAVYBLUE)
            screen.blit(display, (414, 90))
    
            if 600+250 > mousePosition[0] > 600 and 50 + 100 > mousePosition[1] > 50:
                pygame.draw.rect(screen, GRAY, (600, 50, 250, 100))
            else:
                pygame.draw.rect(screen, WHITE, (600, 50, 250, 100))
    
            countInt = textChoice.render("countInt", True, NAVYBLUE)
            screen.blit(countInt, (680, 90))
    
    #Middle Row Draw Rects
            if 50+250 > mousePosition[0] > 50 and 250 + 100 > mousePosition[1] > 250:
                pygame.draw.rect(screen, GRAY, (50, 250, 250, 100))
            else:
                pygame.draw.rect(screen, WHITE, (50, 250, 250, 100))
    
            displayReverse = textChoice.render("displayReverse", True, NAVYBLUE)
            screen.blit(displayReverse, (102, 287))
    
            if 325+250 > mousePosition[0] > 325 and 250 + 100 > mousePosition[1] > 250:
                pygame.draw.rect(screen, GRAY, (325, 250, 250, 100))
            else:
                pygame.draw.rect(screen, WHITE, (325, 250, 250, 100))
    
            sumInt = textChoice.render("sumInt", True, NAVYBLUE)
            screen.blit(sumInt, (410, 287))
    
            if 600+250 > mousePosition[0] > 600 and 250 + 100 > mousePosition[1] > 250:
                pygame.draw.rect(screen, GRAY, (600, 250, 250, 100))
            else:
                pygame.draw.rect(screen, WHITE, (600, 250, 250, 100))
    
            average = textChoice.render("average", True, NAVYBLUE)
            screen.blit(average, (685, 287))
    
    #Middle Bottom Row Draw Rects
            if 50+250 > mousePosition[0] > 50 and 450 + 100 > mousePosition[1] > 450:
                pygame.draw.rect(screen, GRAY, (50, 450, 250, 100))
            else:
                pygame.draw.rect(screen, WHITE, (50, 450, 250, 100))
    
            findMaxORfindMin = textChoice.render("findMax/findMin", True, NAVYBLUE)
            screen.blit(findMaxORfindMin, (95, 488))
    
            if 325+250 > mousePosition[0] > 325 and 450 + 100 > mousePosition[1] > 450:
                pygame.draw.rect(screen, GRAY, (325, 450, 250, 100))
            else:
                pygame.draw.rect(screen, WHITE, (325, 450, 250, 100))
    
            search = textChoice.render("search", True, NAVYBLUE)
            screen.blit(search, (417, 488))
    
            if 600+250 > mousePosition[0] > 600 and 450 + 100 > mousePosition[1] > 450:
                pygame.draw.rect(screen, GRAY, (600, 450, 250, 100))
            else:
                pygame.draw.rect(screen, WHITE, (600, 450, 250, 100))
    
            writeToText = textChoice.render("writeToText", True, NAVYBLUE)
            screen.blit(writeToText, (665, 488))
    
    #Bottom Row Draw Rects
    
            if 50+250 > mousePosition[0] > 50 and 640 + 100 > mousePosition[1] > 640:
                pygame.draw.rect(screen, GRAY, (50, 640, 250, 100))
            else:
                pygame.draw.rect(screen, WHITE, (50, 640, 250, 100))
    
            fillFromText = textChoice.render("fillFromText", True, NAVYBLUE)
            screen.blit(fillFromText, (115, 680))
    
            if 325+250 > mousePosition[0] > 325 and 640 + 100 > mousePosition[1] > 640:
                pygame.draw.rect(screen, GRAY, (325, 640, 250, 100))
            else:
                pygame.draw.rect(screen, WHITE, (325, 640, 250, 100))
    
            searchReplace = textChoice.render("searchReplace", True, NAVYBLUE)
            screen.blit(searchReplace, (382, 680))
    
            if 600+250 > mousePosition[0] > 600 and 640 + 100 > mousePosition[1] > 640:
                pygame.draw.rect(screen, GRAY, (600, 640, 250, 100))
            else:
                pygame.draw.rect(screen, WHITE, (600, 640, 250, 100))
    
            wordCount = textChoice.render("wordCount", True, NAVYBLUE)
            screen.blit(wordCount, (670, 680))
    
            pygame.display.update()
    
    arrayIntro()
    

    它注释了我改变的地方是..

    定义了一个新的函数

    def selectedFirst():
        runFirst = True
    
        while runFirst:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
            screen.fill(MUTEDBLUE)
            pygame.display.update()
    

    点击时调用它

            if mouseClick[0] ==1:
                selectedFirst()
    

    试试看。

    http://imgur.com/a/wZkI3

    编辑:您不需要编写这么大的代码。可以通过在单个函数中循环 blitting 和鼠标跟踪来减少它

    【讨论】:

    • 谢谢!但是,我想知道eval(selected) 在代码中做了什么?
    • 另外,在我更改代码以合并矩形后,我尝试遵循与您更改的部分相同的逻辑;但是它给了我错误:TypeError: 'pygame.Surface' object is not callable,如果您愿意检查一下,我已经在上面添加了我的代码的新编辑。
    • 对那个 eval(选择)感到抱歉。在发布该代码之前,我使用了 selected = selectedFirst()。然后我调用在代码中运行 selectedFirst() 的 eval(selected) 。我忘了删除它。我的想法是您可以列出所有可调用函数 funcList = [func1, func2, func3...] 并且当单击鼠标按钮时,您可以调用相应的函数。前任。当第三个按钮被点击时..你调用 eval(funcList[3-1]).
    • 您的函数名称与文本表面对象的名称相同。前任。 display() 是一个函数,但 display 也是一个在其上呈现“显示”的表面。因此,更改函数的名称。这也是第一个函数 selectedFirst() 起作用的原因。因为没有其他同名元素。附:你不需要这么长的代码。你可以通过循环来减少它
    猜你喜欢
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多