微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何按顺序写入文本框

如何解决如何按顺序写入文本框

我试图让文本框按升序填充,例如如果首先单击,按钮 2 将填充文本框 1。目前,波纹管代码将每个按钮绑定到一个文本框,但我正在尝试修复此问题以用于更大规模的应用程序(有 12 个可用按钮)。

这是我在学校的主要项目,因此感谢任何帮助(完整版运行并且看起来更好)

这是运行所需的最少代码


import pygame,sys

click = bool
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((1200,800),32)

font3 = pygame.font.SysFont(None,25)
Item1 = ''
Item2 = ''
Item1_rect = pygame.Rect(780,33,400,547)
Item2_rect = pygame.Rect(780,58,547)
Colour = pygame.Color(0,0)
found =False
keys = pygame.key.get_pressed()
click = pygame.MOUSEBUTTONDOWN
sc1_num = 0
sc1_price = 0
sc2_num = 0
sc2_price = 0



def Main_sales():
#this area of the code was reused from a prevIoUs file
#which is an example of the RAD approach.

#~~~~~~~~~~~~~~~~~Start Code applied from prevIoUs programs~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     
    
    global Item1,Item2,Item1_rect,Item2_rect,click,sc1_num,sc1_price,sc2_num,sc2_price 
   

    while True:           
        for event in pygame.event.get():

            if event.type == QUIT:
                print("Quit")
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                       click = True

        screen.fill((255,255,255))

        text1_surface = font3.render(Item1,True,(0,0))
        screen.blit(text1_surface,(Item1_rect.x+20,Item1_rect.y +100))

        text2_surface = font3.render(Item2,0))
        screen.blit(text2_surface,(Item2_rect.x+20,Item2_rect.y +100))

        
        mx,my = pygame.mouse.get_pos()

       

        b_w = 180
        b_h = 70
        
        b3_x = 55
        b3_y = 200
        b4_x = 275
        b4_y = 200
        
        button_3 = pygame.Rect( b3_x,b3_y,b_w,b_h)
        button_4 = pygame.Rect( b4_x,b4_y,b_h)
        

        if Item1 == '':
            Empty_row = 1
        elif Item2 == '':
            Empty_row = 2
    
        print(Empty_row)
      
        
        if button_3.collidepoint(mx,my):
            if click:

                sc1_num = sc1_num+1
                sc1_price = sc1_price+15
                Item1 = (f"Scrunchie1            {sc1_num}    ${sc1_price}")
                print(" Button3")
               
        if button_4.collidepoint(mx,my):
            if click:
 
                sc2_num = sc2_num+1
                sc2_price = sc2_price+15
                Item2 = (f"Scrunchie2            {sc2_num}    ${sc2_price}")
                print(" Button4")
        
        pygame.draw.rect(screen,Colour,button_3,2)
        pygame.draw.rect(screen,button_4,2)
        
        screen.blit((font3.render("Scrunchie1",0))),(b3_x+20,b3_y+15))
        screen.blit((font3.render("Scrunchie2",(b4_x+20,b4_y+15))
 
        click = False
        #update screen

        pygame.display.update()
        clock.tick(60)

def QUIT():
    running = True
    while running:
        pygame.quit()
        quit()
      
       

#run the main menu
Main_sales()
pygame.quit()


```

解决方法

创建项目列表并使用列表来管理按钮。创建按钮并在循环中计算按钮位置。在应用程序循环之前执行此操作:

itmes = [Item1,Item2]

b_w = 180
b_h = 70
        
b_x = 55
b_y = 200

button_rects = []
button_surfs = []
for i in range(len(Items)):
    button_rects.append(pygame.Rect(b_x + i*220,b_y,b_w,b_h)) 
    button_surfs.append(font3.render(items[i],True,(0,0)))
    text1_surface = font3.render(Item1,0))    

while True:             
    for event in pygame.event.get():
        # [...] 

在循环中绘制按钮并查看是否在循环中单击按钮:

while True:
    for event in pygame.event.get():
        # [...]

    screen.fill((255,255,255))

    for rect,text_surf in zip(button_rects,button_surfs):
        screen.blit(text_surf[i],text_surf.get_rect(center = rect.center))
    
    # [...]

    if click:
        for i,rect in enumerate(button_rects):
            if rect.collidepoint(mx,my):

                if i == 0:
                    # Item1 clicked
                    # [...]

                if i == 1:
                    # Item2 clicked
                    # [...]
,

您最好制作一个文本框列表并以这种方式进行跟踪。或者更好的是为文本框创建一个简单的类,并以这种方式使用列表。这暂时有效

如果 button_3.collidepoint(mx,my): 如果点击:

            sc1_num = sc1_num+1
            sc1_price = sc1_price+15
            if current_textbox == 0:
                Item1 = (f"Scrunchie1            {sc1_num}    ${sc1_price}")
            else:
                Item2 = (f"Scrunchie1            {sc1_num}    ${sc1_price}")
                current_textbox = 1
                
            print(" Button3")
           
    if button_4.collidepoint(mx,my):
        if click:

            sc2_num = sc2_num+1
            sc2_price = sc2_price+15
            if current_textbox == 1:
                Item2 = (f"Scrunchie1            {sc1_num}    ${sc1_price}")
            else:
                Item1 = (f"Scrunchie1            {sc1_num}    ${sc1_price}")
                current_textbox = 1
            print(" Button4")
    

还有它

pygame.QUIT 不是退出。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。