0

How do I have to code it like I want to make new png file? When I press "P" save the png file name as "A(number)".

import time
import pyautogui as pag
import keyboard

A = 0

while True:  # making a loop
    try:  # used try so that if user pressed other than the given key error will not be shown
        if keyboard.read_key() == "p":  # if key 'q' is pressed 

            myScreenshot = pag.screenshot()
            myScreenshot.save(r'C:\Users\Users\Desktop\"A".png') #name as "A".png 
            time.sleep(1)
            A = A+1
            
            break  # finishing the loop
    except:
        break  # if user pressed a key other than the given key the loop will break
khelwood
  • 55,782
  • 14
  • 81
  • 108

1 Answers1

1

You can use f-strings to have the variable A be part of the string.

myScreenshot.save(fr'C:\Users\Users\Desktop\"A({A})".png')

For A = 0, this would result in the filename being "A(0)".png

shriakhilc
  • 2,922
  • 2
  • 12
  • 17