Python Pygame按钮软锁计算机

Python Pygame按钮软锁计算机,python,pygame,Python,Pygame,这个项目的目标是有两个独立的功能,一个是TKinter,使用无线电框定义Pygame窗口的分辨率。这部分工作得很好 程序的第二部分是Pygame窗口,我想在其中显示4个框,它们根据第一部分确定的大小进行不同的缩放 当运行代码时,程序会突然弹出,创建一个Pygame窗口,然后在不重新启动电脑的情况下关闭程序几乎是不可能的。我希望有人能告诉我这里发生了什么,以及为什么有些东西不能按我希望的方式工作 如果运行此代码,请小心,您的电脑可能已被软锁定。 导入pygame,pygame.locals,sys

这个项目的目标是有两个独立的功能,一个是TKinter,使用无线电框定义Pygame窗口的分辨率。这部分工作得很好

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

当运行代码时,程序会突然弹出,创建一个Pygame窗口,然后在不重新启动电脑的情况下关闭程序几乎是不可能的。我希望有人能告诉我这里发生了什么,以及为什么有些东西不能按我希望的方式工作

如果运行此代码,请小心,您的电脑可能已被软锁定。

导入pygame,pygame.locals,sys,time
显示=[(800600),(1024786),(12801024),(16001200),(19201080)]
def Test():
root=Tk()
根标题(“决议”)
root.iconbitmap(“c:\ResizeImage.ico”)
根几何(“230x200”)
def确认(标签1):
全局分辨率
全局选择
Selection=v.get()
如果选择==1:
分辨率=(显示[0])
睡眠时间(0.2)
root.destroy()
elif选择==2:
分辨率=(显示[1])
睡眠时间(0.2)
root.destroy()
elif选择==3:
分辨率=(显示[2])
睡眠时间(0.2)
root.destroy()
elif选择==4:
分辨率=(显示[3])
睡眠时间(0.2)
root.destroy()
elif选择==5:
分辨率=(显示[4])
睡眠时间(0.2)
root.destroy()
v=IntVar()
v、 第(1)组
Radio1=Radiobutton(根,font=“Veranda 10”,text=“800 x 600”,变量=v,值=1)。包装(锚定=W)
Radio2=Radiobutton(根,font=“Veranda 10”,text=“1024 x 768”,变量=v,值=2)。包装(锚定=W)
Radio3=Radiobutton(根,font=“Veranda 10”,text=“1280 x 1024”,变量=v,值=3)。包装(锚定=W)
Radio4=Radiobutton(根,font=“Veranda 10”,text=“1600 x 1200”,变量=v,值=4)。包装(锚定=W)
Radio5=Radiobutton(根,font=“Veranda 10”,text=“1920 x 1080”,变量=v,值=5)。包装(锚定=W)
#从单选按钮收集输入并转发至确认功能
label1=标签(根,文本=)
b=按钮(root,font=“Veranda 10”,text=“Confirm”,command=lambda:Confirm(label1))
b、 包(锚=SE)
标签1.pack()
mainloop()
测试()
def主菜单():
pygame.init()
screen=pygame.display.set_模式((分辨率))
屏幕填充((100100100))
pygame.display.update()
类按钮():
def uuu init uuuuu(自身、颜色、x、y、宽度、高度、文本=”):
颜色
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.text=文本
def绘图(自身、屏幕、轮廓=无):
如果概述:
pygame.draw.rect(屏幕,轮廓,(self.x-2,self.y-2,self.width+4,self.height+4),0)
pygame.draw.rect(屏幕,self.color,(self.x,self.y,self.width,self.height),0)
如果self.text!="":
font=pygame.font.SysFont(“segouisemibold”,14)
text=font.render(self.text,1,(0,0,0))
blit(text,(self.x+(self.width/2-text.get\u width()/2),self.y+(self.height/2-text.get\u height()/2)))
def isOver(自身,位置):
如果位置[0]>self.x和位置[0]self.y和位置[1]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()