Giter Site home page Giter Site logo

lotspaih / msgboxpy Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 17 KB

Simple Cross-platform Python GUI Message, Dialog, and Input Boxes Using Tkinter

License: MIT License

Python 100.00%
tkinter-python tkinter-gui input dialog-boxes python-script tkinter

msgboxpy's Introduction

License Language

msgBoxPy

Simple Cross-platform Python Message, Dialog, and Input Boxes Using the Tkinter Standard Library

Purpose and Background

I just needed a simple, small, and easy way to display information, warnings, get input, select a file or directory, or ask a simple response question in a GUI format for my Python scripts. Sure there are many great third party modules I could use that are much more sophisticated, but I prefer to stay as close to the standard Python library as possible and only needed some basic GUI options. That's why I pieced together msgBoxPy's examples to use Tkinter, included in the standard Python library, to show cross-platform message, input, and dialog boxes.

The message boxes available are:

  • Information
  • Warning
  • Error
  • Yes/No (which returns an answer of 'yes' or 'no')
  • Ok/Cancel (which returns True or False)
  • Retry/Cancel (which returns True or False)

The input boxes available are:

  • String Input Box (returns the string if entered, otherwise returns 'empty' or 'cancel')
  • Integer Input Box (returns a validated integer, otherwise returns 'cancel')
  • Listbox Input (returns the selection from a given list, otherwise returns 'None')

The dialog boxes available are:

  • Select Filename (returns full path to file selected)
  • Save As Filename (returns full path to file and name for saving)
  • Select Directory (returns full path to directory selected)

alt text

Requirements

  • Python 3.5 or higher (with Tkinter in Standard Library)

Tested with Windows 7 SP1 x64, Ubuntu 16.04 x64, and OSX 10.11.6

Installation

No install required. Just copy the msgBoxPy.py file to the same directory as your Python script and import it, or copy and paste the examples from within the msgBoxPy.py file directly into your own script and modify as needed.

Downloading msgBoxPy.py using cURL:

curl -o msgBoxPy.py https://github.com/lotspaih/msgBoxPy/raw/master/msgBoxPy.py

Example Use

Copy msgBoxPy.py into the same directory as your Python script and import it into your script:

import msgBoxPy

msgBoxPy.infobox('Title', 'Here is my info message.') # Displays an information box with OK button
msgBoxPy.warningbox('Title', 'Here is my warning message!') # Displays a warning box with OK button
msgBoxPy.errorbox('Title', 'Here is my error message!') # Displays an error box with OK button

answer = msgBoxPy.integerbox(title='Your Age?', prompt='Enter your age: ') # Displays Integer Input
if answer == 'cancel': # Cancel button was selected
    msgBoxPy.errorbox(title='Error', message='Cancelled!')
else: # Integer was input and returned
    msgBoxPy.infobox(title='Your Age?', message='Your age is: {}'.format(answer))

answer = msgBoxPy.retrycancelbox(title='Title', message='Empty') # Displays Retry/Cancel box
if not answer: # Answer was False (Cancel button was selected)
    msgBoxPy.warningbox('Retry?', 'You have cancelled the retry!')
else: # Answer was True (Retry was selected)
    retryAgain()
    
# Displays Select Directory dialog starting at the initial directory "/etc"
myDirectory = msgBoxPy.selectdirectorybox(initialdir='/etc', mustexist=True)
if not myDirectory: # Cancel button was selected
    msgBoxPy.errorbox(title='Error', message='Cancelled!')
else: # Directory was selected and is returned 
    msgBoxPy.infobox(title='Your Directory', message=myDirectory)
    
# Displays a Listbox with the arguments passed and returns the selected argument
answer = msgBoxPy.listbox('Mother', 'Father', 'Brother', 'Sister')
if not answer: # Nothing was selected or window was closed
    msgBoxPy.errorbox(title='Listbox', message='You did not make a selection!')
elif answer == 'Mother':
    msgBoxPy.infobox(title='Listbox', message='Hi, Mom!')
else:
    msgBoxPy.infobox(title='Listbox', message='Hi, family member!')

Copy and paste the examples from within the msgBoxPy.py file directly into your script and call the function passing the required or optional arguments:

from tkinter import Tk
import tkinter.messagebox as box

def yesnobox(title='Title', message='Empty'):
    '''Show Yes/No messagebox GUI and return answer'''
    Tk().withdraw()
    answer = box.askquestion(title, message)
    return answer  # Returns 'yes' or 'no'
    
response = yesnobox('Title', 'Do you want to continue?') # Call function and pass title and message
print(response) # Shows a 'yes' or 'no' response

TODO

  • Clean up script (make it more "Pythonic")

License

MIT License for msgBoxPy

msgboxpy's People

Contributors

ml0t 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.