Displaying message boxes

Now we would try to explore some cross-platform JavaScript style message boxes provided to us by pyAutoGUI. It uses Tkinter and PyMsgBox module to display these boxes. The code starts with importing modules, then we use different message boxes to display different messages. The .alert() function displays an alert in which we set the title and text to be blank with an “OK” button. Then the .confirm() function displays a confirm dialog box in which we again set the title and text to be blank and keep two buttons “OK” & “CANCEL” button. Then the .prompt() function displays a confirmation prompt box in which we again set the title, text, and default (what would be written by default in the prompt box before the user starts typing) to be blank. Finally, the .password() function displays a password dialog box in which we again set the title and text to be blank and set the mask (The character that gets replaced instead of the original letters in the password) to be “*”. The implementation is as follows:

Syntax: pyautogui.alert(), pyautogui.confirm(), pyautogui.prompt() and pyautogui.password()

Parameters: The .alert() function has three parameters defining the title, text and buttons to be placed. The .confirm() function also has three parameters for text, title and buttons. The .prompt() function has three parameters for text, title and default value. The .password() has four parameters for text, title, default value and mask (The character that gets replaced instead of the original letters in the password).  

Return Type: The functions don’t return anything but show up an alert in which we set the title and text to be blank with an “OK” button. Then it displays a confirm dialogue box in which we again set the title and text to be blank and keep two buttons “OK” & “CANCEL” button. Then the .prompt() function displays a confirmation prompt box in which we again set the title, text and default (what would be written by default in the prompt box before the user starts typing) to be blank. Finally, the .password() function displays a password dialogue box in which we again set the title and text to be blank and set the mask to be “*”. 

Below is the implementation:

Python3




import pyautogui
 
  
# a alert displays with a ok button
# on it
pyautogui.alert(text='', title='', button='OK')
 
# a confirm dialog box appears with ok
# and cancel buttons on it
pyautogui.confirm(text='', title='', buttons=['OK', 'Cancel'])
 
# a prompt displays that lets you to
# write something
pyautogui.prompt(text='', title='' , default='')
 
# a password field appears with entry box
# to fill a password
pyautogui.password(text='', title='', default='', mask='*')


Output:

GUI Automation using Python

In this article, we will explore how we can do GUI automation using Python. There are many modules that can do these things, but in this article, we will use a module named PyAutoGUI to perform GUI and desktop automation using python. 

We would explore two sections –

  • How to automatically use the mouse pointer to perform certain tasks like moving the cursor, clicking on a certain point on the screen, etc
  • Also, we would explore how we can automate the keyboard keystrokes.

Similar Reads

Installation

This module does not come preloaded with Python. To install it type the below command in the terminal....

Getting Started

We should know the screen size of my device before doing any automation. Luckily PyautoGUI helps us to easily get it using the .size() function. It returns a size object with two values that represent the width and height of the screen respectively. Implementation is as follows....

Automating Mouse Movements

...

Automating Keyboard

Getting the current position of the mouse cursor:...

Displaying message boxes

...

Taking screenshots

...