首页 > 解决方案 > Pygame 按钮软锁计算机

问题描述

这个项目的目标是拥有两个独立的功能,一个是用于 TKinter 并使用 Radio box 来定义 Pygame 窗口的分辨率。这部分工作完美。

程序的第二部分是 Pygame 窗口,我希望在其中显示 4 个框,根据第一部分确定的大小进行不同的缩放。

运行代码时,程序会弹出,创建一个 Pygame 窗口,然后实际上不可能在不重新启动 PC 的情况下关闭程序。我希望有人能告诉我这里发生了什么以及为什么某些事情没有按照我想要的方式工作。

如果你运行这段代码要小心,你的电脑可能会被软锁定。

import pygame, pygame.locals, sys, time


DISPLAYS = [(800, 600), (1024, 786), (1280, 1024), (1600, 1200), (1920, 1080)]

def Test():

    root = Tk()
    root.title("Resolution")
    root.iconbitmap("c:\ResizeImage.ico")
    root.geometry("230x200")


    def CONFIRM(label1): 

        global resolution
        global Selection
        Selection = v.get()

        if Selection == 1:
            resolution = (DISPLAYS[0])
            time.sleep(0.2)
            root.destroy()
                    
        elif Selection == 2:
            resolution = (DISPLAYS[1])
            time.sleep(0.2)
            root.destroy()
            
        elif Selection == 3:
            resolution = (DISPLAYS[2])
            time.sleep(0.2)
            root.destroy()
                    
        elif Selection == 4:
            resolution = (DISPLAYS[3])
            time.sleep(0.2)
            root.destroy()
                    
        elif Selection == 5:
            resolution = (DISPLAYS[4])
            time.sleep(0.2)
            root.destroy()
            
    v = IntVar()

    v.set(1)
    Radio1 = Radiobutton(root, font = "Veranda 10", text = "800 x 600", variable = v, value = 1).pack(anchor = W)
    Radio2 = Radiobutton(root, font = "Veranda 10", text = "1024 x 768", variable = v, value = 2).pack(anchor = W)
    Radio3 = Radiobutton(root, font = "Veranda 10", text = "1280 x 1024", variable = v, value = 3).pack(anchor = W)
    Radio4 = Radiobutton(root, font = "Veranda 10", text = "1600 x 1200", variable = v, value = 4).pack(anchor = W)
    Radio5 = Radiobutton(root, font = "Veranda 10", text = "1920 x 1080", variable = v, value = 5).pack(anchor = W)
    # Gather input from the Radio Buttons and forward it to the CONFIRM function
    label1 = Label(root,text="")
    b = Button(root, font = "Veranda 10", text = "Confirm", command = lambda:CONFIRM(label1))
    b.pack(anchor = SE)
    label1.pack()
    mainloop()

Test()





def MainMenu():

    pygame.init()
    screen = pygame.display.set_mode((resolution))
    screen.fill((100, 100, 100))
    pygame.display.update()
    

    class button():
        def __init__(self, colour, x, y, width, height, text =""):

            self.colour = colour
            self.x = x
            self.y = y
            self.width = width
            self.height = height
            self.text = text

        def draw(self, screen, outline = None):

            if outline:
                pygame.draw.rect(screen, outline, (self.x - 2, self.y - 2, self.width + 4, self.height +4), 0)

            pygame.draw.rect(screen, self.colour, (self.x, self.y, self.width, self.height), 0)

            if self.text != "":
                font = pygame.font.SysFont("segoeuisemibold", 14)
                text = font.render(self.text, 1, (0, 0, 0))
                screen.blit(text, (self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))

            def isOver(self, pos):
                if pos[0] > self.x and pos[0] < self.x + self.width:
                    if pos[1] > self.y and pos[1] < self.y + self.height:
                        return True
                return False                


    def drawItems():
        startButton.draw(screen, (0, 0, 0))
        loadButton.draw(screen, (0, 0, 0))
        optionButton.draw(screen, (0, 0, 0))
        quitButton.draw(screen, (0, 0, 0))
    

    running = True
    while running == True:

        screen.fill((100, 100, 100))
        pygame.display.update()


        if resolution == DISPLAYS[0]:
            startButton = button((0, 0, 0), 100, 150, 200, 50, "Start Simulation")
            loadButton = button((0, 0, 0), 500, 150, 200, 50, "Load Simulation")
            optionButton = button((0, 0, 0), 100, 300, 200, 50, "Options")
            quitButton = button((0, 0, 0), 500, 300, 200, 50, "Quit Program")

        elif resolution == DISPLAYS[1]:
            startButton = button((0, 0, 0), 100, 150, 300, 50, "Start Simulation")
            loadButton = button((0, 0, 0), 624, 150, 300, 50, "Load Simulation")
            optionButton = button((0, 0, 0), 100, 300, 300, 50, "Options")
            quitButton = button((0, 0, 0), 624, 300, 300, 50, "Quit Program")

        elif resolution == DISPLAYS[2]:
            startButton = button((0, 0, 0), 100, 150, 400, 50, "Start Simulation")
            loadButton = button((0, 0, 0), 780, 150, 400, 50, "Load Simulation")
            optionButton = button((0, 0, 0), 100, 300, 400, 50, "Options")
            quitButton = button((0, 0, 0), 780, 300, 400, 50, "Quit Program")

        elif resolution == DISPLAYS[3]:
            startButton = button((0, 0, 0), 100, 150, 500, 50, "Start Simulation")
            loadButton = button((0, 0, 0), 1000, 150, 500, 50, "Load Simulation")
            optionButton = button((0, 0, 0), 100, 300, 500, 50, "Options")
            quitButton = button((0, 0, 0), 1000, 300, 500, 50, "Quit Program")

        elif resolution == DISPLAYS[4]:
            startButton = button((0, 0, 0), 100, 150, 600, 50, "Start Simulation")
            loadButton = button((0, 0, 0), 1220, 150, 600, 50, "Load Simulation")
            optionButton = button((0, 0, 0), 100, 300, 600, 50, "Options")
            quitButton = button((0, 0, 0), 1220, 300, 600, 50, "Quit Program")

        



        for event in pygame.event.get():
            pos = pygame.mouse.get_pos()
        

        if event.type == pygame.K_ESCAPE:
            running = False
            pygame.quit()
            sys.exit()



MainMenu()

标签: pythonpygame

解决方案


如果要评估是否ESC按下了,则必须测试event.typeispygame.KEYDOWNevent.keyis 是否pygame.K_ESCAPE在事件循环中。注意缩进。我也建议对pygame.QUIT事件进行评估:

running = True
while running == True:

    # [...]

    for event in pygame.event.get():

        if event.type == pygame.QUIT
            running = False

        elif event.type == pygame.KEYDOWN:  
            if event.key == pygame.K_ESCAPE:
                running = False

    # [...]

pygame.quit()
sys.exit() 

推荐阅读