使用输入模块获取按键启动另一个进程

2024-04-25 06:01:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的代码从用户那里获取输入,并检查按下了哪些键。当我运行程序时,它会创建程序的另一个进程,我不希望发生这种情况,也不知道如何停止这种情况的发生

import pygame, inputs
from multiprocessing import freeze_support

pygame.init()
width,height = 800,600
screen = pygame.display.set_mode((width,height), pygame.RESIZABLE)

class Config:

    config = ["Dark Mode"]


class greeter:

    def login(args):
        self,screen,width,height = args[0],args[1],args[2],args[3]
        activeTextBox = [False]
        typeLock = False
        pygame.key.set_repeat(500, 1)
        username,password = '',''
        if Config.config != None:
            if Config.config[0] == "Dark Mode":
                display = "dark"
            else:
                display = "bright"
        pygame.key.set_repeat(2,80)
        while True:
            mouse,click = pygame.mouse.get_pos(),pygame.mouse.get_pressed()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                if event.type == pygame.VIDEORESIZE:
                    screen = pygame.display.set_mode((event.w,event.h), pygame.RESIZABLE)
                    width,height = event.w,event.h
            if display == "dark":
                screen.fill((119,136,153))
                pygame.draw.rect(screen, (0,255,0), (0,height-80,width,80))
            else:
                screen.fill((192,192,192))
                pygame.draw.rect(screen, (0,255,0), (0,height-80,width,80))
            GUI.text(GUI,"Login",width/2-80,20,200,60,(0,0,0),"Segoe UI",60)
            if display == "dark":
                #GUI.inputBar(keyBoxs,mouse,click,(255,255,255),(0,0,0),(int(width/2)-180,200,width-200,80))
                activeTextBox = GUI.inputBar(GUI,activeTextBox,mouse,click,(47,79,79),(255,255,255),(100,200,width-200,50),'Username')
##            GUI.button(GUI,"Login",int(width/2)+180,350,100,20,(0,0,0),(0),(0),True,[self,screen,width,height,username,password],greeter.checkUser)
##            GUI.button(GUI,"Create Account",(width/2)-230,350,100,20,(0,0,0),(0),(0),True,[self,screen,width,height],self.createNew)
            pygame.display.flip()


class GUI:

    def inputBar(self,activeBox,mouse,click,bgColour,textColour,rect,inactiveText):
        pygame.draw.rect(screen, bgColour, rect)
        for active in activeBox:
            if active == False:
                self.text(self,inactiveText,102,rect[1]+10,150,25,textColour,'Segoe UI',30)
                if rect[0]+rect[2] > mouse[0] > rect[0] and rect[1]+rect[3] > mouse[1] > rect[1]:
                    if click[0] == 1:
                        del activeBox[active]
                        activeBox.append(True)
            if active == True:
                eventKeys = inputs.get_key()
                for key in eventKeys:
                    if key.code == 0:
                        print("key pressed")

        return activeBox

    def text(self,msg,x,y,w,h,colour,font,size,surf=screen):
        font = pygame.font.SysFont(font, int(size))
        textSurf = font.render(str(msg), True, colour)
        textRect = textSurf.get_rect()
        textRect.center = ( (x+(w/2)), (y+(h/2)) )
        surf.blit(textSurf, (textRect))

greeter.login([greeter,screen,width,height])

当我点击用户名框时,第二个进程将打开,因为在那里我尝试获取用户输入。这会创建第二个窗口,然后尝试使用第二个窗口。它关闭,第一个窗口没有响应


Tags: keyrectselfeventtrueifdisplayargs