【问题标题】:How to write to text boxes sequentially [duplicate]如何按顺序写入文本框[重复]
【发布时间】:2021-07-12 02:00:29
【问题描述】:

我正在尝试让文本框按升序填充,例如如果第一次单击,按钮 2 会填充文本框 1。目前,下面的代码将每个按钮都绑定到一个文本框,但我正在尝试解决这个问题,以便在更大规模的应用程序上使用(有 12 个按钮可用)。

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

这是运行所需的最少代码


import pygame, sys

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

font3 = pygame.font.SysFont(None, 25)
Item1 = ''
Item2 = ''
Item1_rect = pygame.Rect(780, 33, 400, 547)
Item2_rect = pygame.Rect(780, 58, 400, 547)
Colour = pygame.Color(0,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,0))
        screen.blit(text1_surface, (Item1_rect.x+20 , Item1_rect.y +100))

        text2_surface = font3.render(Item2, True, (0,0,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_w, 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, Colour, button_4, 2)
        
        screen.blit((font3.render("Scrunchie1", True, (0,0,0))),(b3_x+20, b3_y+15))
        screen.blit((font3.render("Scrunchie2", True, (0,0,0))),(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()


```

【问题讨论】:

    标签: python pygame


    【解决方案1】:

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

    如果 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 不是QUIT

    【讨论】:

      【解决方案2】:

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

      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,0)))
          text1_surface = font3.render(Item1, True, (0,0,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
                          # [...]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-07
        • 1970-01-01
        • 2013-01-08
        • 2017-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多