Python 点击按钮后如何清除屏幕?
我试图构建一个基于菜单的程序,如果我点击一个按钮,它对应一个函数;它将执行某些条件。但是,我无法让按钮本身工作。我希望有它,这样当我点击按钮时,屏幕上会充满另一种颜色,这样我就可以有另一个屏幕,无论它点击了什么功能。我在下面添加了我的代码;非常感谢您的帮助 这是我遇到问题的按钮:Python 点击按钮后如何清除屏幕?,python,python-3.x,button,pygame,Python,Python 3.x,Button,Pygame,我试图构建一个基于菜单的程序,如果我点击一个按钮,它对应一个函数;它将执行某些条件。但是,我无法让按钮本身工作。我希望有它,这样当我点击按钮时,屏幕上会充满另一种颜色,这样我就可以有另一个屏幕,无论它点击了什么功能。我在下面添加了我的代码;非常感谢您的帮助 这是我遇到问题的按钮: def arrayIntro(): intro = True while intro: for event in pygame.event.get(): if event.type == pyg
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()
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()
试试看
编辑:你不需要写这么大的代码。它可以通过在单个函数中循环光点显示和鼠标跟踪来减少谢谢!然而,我想知道
eval(selected)
在代码中做了什么?另外,在我更改代码以合并rect之后,我尝试遵循您更改的部分的相同逻辑;但是它给了我一个错误:TypeError:“pygame.Surface”对象不可调用
,如果您愿意签出,我已经在上面添加了我的代码的新编辑。对于此评估(已选择),我深表歉意。在发布代码之前,我使用selected=selectedFirst()。然后我调用eval(selected),它在代码中运行selectedFirst()。我忘了把它取下来。我的想法是,您可以列出所有可调用函数funcList=[func1,func2,func3…],当单击鼠标按钮时,您可以调用相应的func。例如,当第三个按钮点击时。。调用eval(funcList[3-1])。函数名与文本曲面对象的函数名相同。例如display()是一个函数,但display也是一个曲面,在该曲面上渲染“display”。因此,请更改函数的名称。这也是第一个selectedFirst()函数工作的原因。。因为没有其他同名元素。另外,你不需要这么长的代码。你可以通过循环来减少它