Hello im trying to make a script where it has a control panel and u can select what application u want to open only problem is that it opens that application multiple times please help only want to open it once! I think the problem is that it checks if my mouse button is pressed multiple times a second and since my mouse is only being pressed for a second and it checks a couple times a second it does the if statement and completes it and opens the application multiple times.
# Importing Modules/Librarys.
import pygame
import subprocess
pygame.init()
pygame.font.init()
# Color Variables.
white_color = "#FFFFFF"
# Control Panel Variables.
ctrlpanel_run = True
ctrlpanel_program_select = True
ctrlpanel_program_select_input = True
# Main Arguements.
root = pygame.display.set_mode((700,400))
pygame.display.set_caption("AutoDraw Control Panel")
root.fill("#333333")
# Draws Control panel Program Select Screen.
if ctrlpanel_program_select == True:
title_font = pygame.font.Font("./fonts/Exo-Bold.otf", 100)
button_font = pygame.font.Font("./fonts/Exo-Bold.otf", 45)
title_text = button_font.render("What Application?", True, white_color)
root.blit(title_text, (165, 100))
paint_button_rect = pygame.draw.rect(root, '#006EE6', pygame.Rect(70,200,200,90))
paint3d_button_rect = pygame.draw.rect(root, '#006EE6', pygame.Rect(430,200,200,90))
paint_button_text = button_font.render("Paint", True, white_color)
root.blit(paint_button_text, (120, 225.5))
paint3d_button_text = button_font.render("Paint 3D", True, white_color)
root.blit(paint3d_button_text, (447, 225.5))
# Control panel Program Select Screen Input /// And Variables for Mouse positions and inputs.
mouse_x, mouse_y = pygame.mouse.get_pos()
mouse_l, mouse_m, mouse_r = pygame.mouse.get_pressed()
print(mouse_x, mouse_y)
while ctrlpanel_run:
pygame.display.flip()
mouse_x, mouse_y = pygame.mouse.get_pos()
mouse_l, mouse_m, mouse_r = pygame.mouse.get_pressed()
print(mouse_x, mouse_y)
for event in pygame.event.get():
if event.type == pygame.QUIT:
ctrlpanel_run = False
break
if ctrlpanel_program_select_input == True:
if mouse_x > 69 and mouse_x < 270:
if mouse_y > 199 and mouse_y < 290:
if mouse_l:
subprocess.call(["cmd", "/c", "start", "/max", "C:\Windows\System32\mspaint.exe"])
CodePudding user response:
You know the problem already, as you state in your question. The solution is to stop checking for the mouse click as long as mouse_l == True
. So something like this:
Initialize a click_detected
boolean variable before your main loop. Then inside the loop:
if ctrlpanel_program_select_input == True:
if mouse_x > 69 and mouse_x < 270:
if mouse_y > 199 and mouse_y < 290:
if mouse_l:
click_detected = True
if not mouse_l and click_detected:
click_detected = False
subprocess.call(["cmd", "/c", "start", "/max", "C:\Windows\System32\mspaint.exe"])
This should open the application when you let go of the button, instead of when you press the button.
CodePudding user response:
Instead of checking the state of mouse buttons, you could use events generated by pressing or releasing the buttons.
You've already defined Rects for your buttons, so you can use collidepoint()
to see if the mouse was clicked in the button.
for event in pygame.event.get():
if event.type == pygame.QUIT:
ctrlpanel_run = False
break
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1: # Left-click
if paint_button_rect.collidepoint(event.pos):
subprocess.call(["cmd", "/c", "start", "/max", r"C:\Windows\System32\mspaint.exe"])
elif paint3d_button_rect.collidepoint(event.pos)
# launch Paint 3D