Code Monkey home page Code Monkey logo

msgbox's Introduction

msgbox

Purpose: to create a simple and memorable function for producing a native windows message box.

1 required field followed by 4 optional fields:

msgbox(message text, [title, buttons, default button, icon])  # Only message text is required. 

Standard Usage Examples:

msgbox('Hello World')  # Produces Messagebox with single 'OK' button and no title.
msgbox('Hello World', 'My Title')
msgbox('Hello World', 'My Title', 'Yes No')  # Produces Messagebow with Yes and No buttons. 
msgbox('Hello World', 'My Title', 'Yes No Cancel', 'No')  # Indicates that 'No' will be the highlighted default button.
msgbox('Hello World', 'My Title', 'Ok Cancel', 'Cancel', '?')  # Adds '?' icon
msgbox('Hello World', 'My Title', 'YNC', 'C', '!')  # Instead of typing Yes, No and Cancel, YNC is also acceptable.

Lazy Usage Examples:

All arguments can be placed in the first string so long as comma separated. Why? I get tired of typing all of those quotes.

msgbox('Hello World, My Title, YN, , i')  # Produces Yes No Messagebox with text, title and the I info icon. 
msgbox('Hello\, World, My Title')  # Produces OK Messagebox with text "Hello, World" and title "My Title". 

Buttons:

The third and fourth arugument are for button options and are optional. If none is indicted, 'OK' will be used.

There are six options:

  • 'ok', 'ok cancel', 'abort retry ignore', 'yes no cancel', 'yes no', 'retry cancel'.
  • Alternately, initials may be used: 'o', 'oc', 'ari', 'ync', 'yn', 'rc'.
  • Case and spacing are ignored.
  • All of these are treated the same: 'Y N C', 'yesnocancel', 'YEsNocanCEL', 'YES NO CANCEL'

The fourth argment specifies the default button. If none is provided, the first button is the default.

Examples:

  • msgbox('Hello', 'My Title', 'YN') # Yes button is default
  • msgbox('Hello', 'My Title', 'YN', 'N') # No button is default

Icons:

The fifth argument is for the icon and is optional.

  • Allowed: 'x', '?', '!', 'i', '*'.
  • Alternately, you may spell out the icon descriptions:
  • 'error', 'stop', 'hand','question', 'questionmark', 'exclamation', 'exclamationpoint', 'info', 'information', 'asterisk'.

Using the return string:

user_response = msgbox("Would you like ice cream?", "Ice Cream", "YNC", 'Y', '?')
msgbox("You clicked: %s" % user_response)

user_response = msgbox("Tornado Warning Today!", "Weather Alert", '', '', '!')
msgbox("You clicked: %s" % user_response)

Concept

I like the simplicity and functionality of AHK Script (autohotkey) which allows for

MsgBox , Hello World

as well as providing additional fields to customize the native windows message box. I wanted to create something similar for python. I did some research and found that PyQt, ctypes, tkinter and others can do this, but with some complication. @asweigart developed a very nice cross platform pymsgbox module, but the native windows features were limited. What I would like to accomplish is available in ctypes, but requies passing hex code and other non-intuitive arguments. I intend to use ctypes import.

I wanted to allow the user to name the buttons rather than remember message box types. So, the user can choose "Yes No Cancel" or even just "YNC". The same with choosing which button is defaulted "Cancel". I wanted the function to return the string of the button name pressed. So, if "Cancel" is pressed, the function returns the string "Cancel".

I am only working on a windows desktop and am not interested at this time in cross- platform functionality. For that, one should use pymsgbox @asweigart.

Things to add

  • HWND field so that msgbox can have a parent window
  • Optional timeout feature

msgbox's People

Contributors

cruncher1 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.