Python Forum
how to mouse click a specific item in pygame?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to mouse click a specific item in pygame?
#1
hello,

I want to mouse click a specific item.
When you click Activé which is button_A this line appear: img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, BLANC)
Edit:
i am now able to click at activé but text of button A appear over B

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
 # -*- coding: utf-8 -*-
 
import pygame
from pygame.locals import *
import sys
  
pygame.init()
display = pygame.display.set_mode((300, 300))
FPS_CLOCK = pygame.time.Clock()
#pygame.init()
 
BLEU = (0, 0, 255)
BLANC = (255, 255, 255)
ROUGE = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
GRIS = (200, 200, 200)
BLANC = (255, 255, 255)
NOIR = (0, 0, 0)
 
screen = pygame.display.set_mode([700, 500])
 
# Charger un police de caract�re
sysfont = pygame.font.get_default_font()
font = pygame.font.SysFont(None, 48)
 
def bouton_A(screen, pos, texte):
    rayon = 55
    pygame.draw.circle(screen, GREEN, pos, rayon)
    txtBtn = font.render(texte, True, NOIR)
    rectBtn = txtBtn.get_rect()
    rectBtn.center = (pos)
    screen.blit(txtBtn, rectBtn)
     
def bouton_D(screen, pos, texte):
    rayon = 55
    pygame.draw.circle(screen, ROUGE, pos, rayon)
    txtBtn = font.render(texte, True, NOIR)
    rectBtn = txtBtn.get_rect()
    rectBtn.center = (pos)
    screen.blit(txtBtn, rectBtn)
     
def bouton_nb(screen, pos, texte):
    rayon = 25
    pygame.draw.circle(screen, GRIS, pos, rayon)
    txtBtn = font.render(texte, True, NOIR)
    rectBtn = txtBtn.get_rect()
    rectBtn.center = (pos)
    screen.blit(txtBtn, rectBtn)
 
sysfont = pygame.font.get_default_font()
font = pygame.font.SysFont(None, 36)
 
 
 
running = True
 
while running:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            x , y = pygame.mouse.get_pos()
            print(x,y)
            if x < 185 and y < 285:
               img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, BLANC)
               screen.blit(img, (20, 100))
           
            if x < 538 and y < 292:
               img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, BLANC)
               screen.blit(img, (20, 100))
           
           
    #screen.fill(BLANC)
     
    bouton_A(screen, (150, 250), "Activ�")
    bouton_D(screen, (500, 250), "D�sactiv�")
    bouton_nb(screen, (265, 200), "1")
    bouton_nb(screen, (320, 200), "2")
    bouton_nb(screen, (375, 200), "3")
    bouton_nb(screen, (265, 255), "4")
    bouton_nb(screen, (320, 255), "5")
    bouton_nb(screen, (375, 255), "6")
    bouton_nb(screen, (265, 310), "7")
    bouton_nb(screen, (320, 310), "8")
    bouton_nb(screen, (375, 310), "9")
     
     
     
    #pygame.draw.circle(screen, GRIS, (260, 250), 25)
    pygame.display.flip()
 
    img = font.render("Bienvenue", True, ROUGE)
    screen.blit(img, (20, 20))
     
 
 
    pygame.display.flip()
     
    pygame.display.update()
    FPS_CLOCK.tick(30)
 
     
pygame.quit()
     
Thank you
Reply
#2
You need to erase the old text. You can do this by drawing a black rectangle over the text, or by redrawing the entire screen.
Frankduc likes this post
Reply
#3
i have found a quick solution, not pretty but it works

1
2
3
4
5
6
7
8
9
10
11
if x > 104 and x < 204 and y > 196 and y < 301:
               img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, NOIR)
               screen.blit(img, (20, 100))
               img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, BLANC)
               screen.blit(img, (20, 100))
           
            if x < 549 and x > 444 and y > 192 and y < 309:
               img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, NOIR)
               screen.blit(img, (20, 100))
               img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, BLANC)
               screen.blit(img, (20, 100))
Reply
#4
Hello again,

I try to ask here to avoid creating another post.
Now i am trying to create a countdown clock below the numbers.
I am king of stuck to how insert the timer in my actual panel without creating a new panel.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
 
import pygame
from pygame.locals import *
import sys
import time
clock = pygame.time.Clock()
  
pygame.init()
display = pygame.display.set_mode((300, 300))
pygame.display.set_caption('Panneau de contr�le')
FPS_CLOCK = pygame.time.Clock()
 
 
counter, text = 10, '10'.rjust(3)
pygame.time.set_timer(pygame.USEREVENT, 1000)
font = pygame.font.SysFont('Consolas', 30)
#pygame.init()
 
BLEU = (0, 0, 255)
BLANC = (255, 255, 255)
ROUGE = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
GRIS = (200, 200, 200)
BLANC = (255, 255, 255)
NOIR = (0, 0, 0)
 
screen = pygame.display.set_mode([700, 500])
 
# Charger un police de caract�re
sysfont = pygame.font.get_default_font()
font = pygame.font.SysFont(None, 48)
 
def bouton_A(screen, pos, texte):
    rayon = 55
    pygame.draw.circle(screen, GREEN, pos, rayon)
    txtBtn = font.render(texte, True, NOIR)
    rectBtn = txtBtn.get_rect()
    rectBtn.center = (pos)
    screen.blit(txtBtn, rectBtn)
     
def bouton_D(screen, pos, texte):
    rayon = 55
    pygame.draw.circle(screen, ROUGE, pos, rayon)
    txtBtn = font.render(texte, True, NOIR)
    rectBtn = txtBtn.get_rect()
    rectBtn.center = (pos)
    screen.blit(txtBtn, rectBtn)
     
def bouton_nb(screen, pos, texte):
    rayon = 25
    pygame.draw.circle(screen, GRIS, pos, rayon)
    txtBtn = font.render(texte, True, NOIR)
    rectBtn = txtBtn.get_rect()
    rectBtn.center = (pos)
    screen.blit(txtBtn, rectBtn)
 
sysfont = pygame.font.get_default_font()
font = pygame.font.SysFont(None, 36)
 
 
 
running = True
 
while running:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
     
        #mouse_presses = pygame.mouse.get_pressed()
  
        #if mouse_presses[0]:
        if event.type == pygame.MOUSEBUTTONDOWN:
            x , y = pygame.mouse.get_pos()
            print(x,y)
            if x > 104 and x < 204 and y > 196 and y < 301:
               img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, NOIR)
               screen.blit(img, (20, 100))
               img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, BLANC)
               screen.blit(img, (20, 100))
           
            if x < 549 and x > 444 and y > 192 and y < 309:
               img = font.render("Veuillez entrer votre code pour armer le systeme d'alarme!", True, NOIR)
               screen.blit(img, (20, 100))
               img = font.render("Entrez votre code pour d�sarmer le systeme d'alarme!", True, BLANC)
               screen.blit(img, (20, 100))
              
            if event.type == pygame.USEREVENT:
                counter -= 1
                text = str(counter).rjust(3) if counter > 0 else 'boom!'
            if event.type == pygame.QUIT:
                run = False
 
    screen.fill((255, 255, 255))
    screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
    #pygame.display.flip()
    clock.tick(60)
     
    #screen.fill(BLANC)
     
    bouton_A(screen, (150, 250), "Activ�")
    bouton_D(screen, (500, 250), "D�sactiv�")
    bouton_nb(screen, (265, 200), "1")
    bouton_nb(screen, (320, 200), "2")
    bouton_nb(screen, (375, 200), "3")
    bouton_nb(screen, (265, 255), "4")
    bouton_nb(screen, (320, 255), "5")
    bouton_nb(screen, (375, 255), "6")
    bouton_nb(screen, (265, 310), "7")
    bouton_nb(screen, (320, 310), "8")
    bouton_nb(screen, (375, 310), "9")
     
     
     
    #pygame.draw.circle(screen, GRIS, (260, 250), 25)
    #pygame.display.flip()
 
    img = font.render("Bienvenue", True, ROUGE)
    screen.blit(img, (20, 20))
     
 
 
    pygame.display.flip()
     
    pygame.display.update()
    FPS_CLOCK.tick(30)
 
     
pygame.quit()
     
Reply
#5
You should ask pygame questions in the pygame section.
Reply
#6
There is a pygame section? i have to check that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Non repetitive mouse click Sartre 5 3,345 Apr-22-2023, 06:46 PM
Last Post: deanhystad
  Remove an item from a list contained in another item in python CompleteNewb 19 9,114 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  Not able to make a specific thing pause in pygame cooImanreebro 4 4,798 Dec-13-2020, 10:34 PM
Last Post: cooImanreebro
  Move mouse and click in particular position biprabu 3 3,463 Sep-01-2020, 08:23 PM
Last Post: deanhystad
  Slide show with mouse click pausing aantono 1 2,897 Jan-28-2020, 04:25 AM
Last Post: Larz60+
  mouse 0.7.0 - mouse polling hate 125-1000hz penahuse 1 3,351 Dec-06-2019, 09:51 PM
Last Post: Larz60+
  Delete specific lines contain specific words mannyi 2 5,152 Nov-04-2019, 04:50 PM
Last Post: mannyi
  Why is left mouse click not working? yeto 3 7,842 Jul-15-2019, 05:23 AM
Last Post: Yoriz
  How to get mouse coordinates on click and release OhNoSegFaultAgain 1 4,448 May-17-2019, 06:56 PM
Last Post: buran
  Python catch mouse click in pure text, no graphics samtal 7 11,656 Sep-10-2018, 03:02 PM
Last Post: samtal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020