commit 9099fbc9566719c8bcc8be3729e6503bc763f046 Author: mlot Date: Thu Jan 15 11:13:45 2026 -0500 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3092bfd --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2025 mlot + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b43d7c0 --- /dev/null +++ b/README.md @@ -0,0 +1,100 @@ +[![License](https://img.shields.io/badge/license-MIT-green.svg)](https://gitlab.com/lotspaih/msgBoxPy/blob/master/LICENSE) [![Language](https://img.shields.io/badge/language-python-blue.svg)](https://www.python.org/) + +# 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 better (e.g., [guizero](https://lawsie.github.io/guizero/)), 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](https://wiki.python.org/moin/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) + +![Example Image](https://tildeforge.dev/mlot/msgBoxPy/raw/branch/main/ex_msgBoxPyM.png "Example Infobox Image") + +## Requirements +* [ ] Python 3.5 or higher (*with Tkinter in Standard Library*) + +Tested with Windows 11 23H2 x64, Manjaro Linux 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](https://curl.haxx.se/): + +``` +curl -o msgBoxPy.py https://tildeforge.dev/mlot/msgBoxPy/raw/branch/main/msgBoxPy.py +``` + +## Example Use +Copy msgBoxPy.py into the same directory as your Python script and import it into your script: +```python +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: +```python +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 +``` + +## Contributing +If you want to contribute new functions or improve the exisiting ones, feel free to clone this repo and create a pull request. And thanks for any time and help! + +## License +[MIT License](https://opensource.org/licenses/MIT) for msgBoxPy diff --git a/ex_msgBoxPyM.png b/ex_msgBoxPyM.png new file mode 100644 index 0000000..731860b Binary files /dev/null and b/ex_msgBoxPyM.png differ diff --git a/msgBoxPy.py b/msgBoxPy.py new file mode 100644 index 0000000..8421706 --- /dev/null +++ b/msgBoxPy.py @@ -0,0 +1,145 @@ +#! /usr/bin/env python3 + +# Copyright (c) 2016 mlot +# MIT License - https://opensource.org/licenses/MIT + +'''msgBoxPy - Quick import or reference for making simple GUI messages, input, +and dialog boxes in Python. +''' +# USAGE: "import msgBoxPy" or copy the examples (including imports) below. +# VERSION: 03/07/2017 + +from tkinter import Tk +from tkinter import Frame +from tkinter import Listbox +from tkinter import Button +from tkinter import filedialog +from tkinter import simpledialog +import tkinter.messagebox as box + + +def infobox(title='Title', message='Empty'): + '''Show information messagebox GUI''' + Tk().withdraw() + box.showinfo(title, message) + return None + + +def warningbox(title='Title', message='Empty'): + '''Show warning messagebox GUI''' + Tk().withdraw() + box.showwarning(title, message) + return None + + +def errorbox(title='Title', message='Empty'): + '''Show error messagebox GUI''' + Tk().withdraw() + box.showerror(title, message) + return None + + +def yesnobox(title='Title', message='Empty'): + '''Show Yes/No messagebox GUI and return yes or no answer''' + Tk().withdraw() + answer = box.askquestion(title, message) + return answer + + +def okcancelbox(title='Title', message='Empty'): + '''Show Ok/Cancel messagebox GUI and returns True for Ok and False for + Cancel''' + Tk().withdraw() + answer = box.askokcancel(title, message) + return answer + + +def retrycancelbox(title='Title', message='Empty'): + '''Show Retry/Cancel messagebox GUI and returns True for Retry and False + for Cancel''' + Tk().withdraw() + answer = box.askretrycancel(title, message) + return answer + + +def inputbox(title='Title', prompt='Prompt text:'): + '''Creates a simple string input box with Ok/Cancel buttons. Ok returns + the string entered unless the string is empty then "empty" is returned. + Cancel returns "cancel".''' + Tk().withdraw() + enteredString = simpledialog.askstring(title, prompt) + if enteredString is None: + return 'cancel' + elif enteredString == '': + return 'empty' + else: + return enteredString + + +def integerbox(title='Title', prompt='Prompt text:'): + '''Creates a simple integer input box with Ok/Cancel buttons. Ok returns + the integer entered. Cancel returns "cancel".''' + Tk().withdraw() + enteredInteger = simpledialog.askinteger(title, prompt) + if enteredInteger is None: + return 'cancel' + else: + return enteredInteger + + +def selectfilenamebox(**kwargs): + '''Creates a select file dialog box and returns path to the file + selected or returns empty if Cancel was selected. + Example keyword options are "title" and "initialdir".''' + Tk().withdraw() + filePath = filedialog.askopenfilename(**kwargs) + return filePath + + +def savefilenamebox(**kwargs): + '''Creates a save file dialog box and returns path to the file + selected or name entered. Returns empty if Cancel was selected. + Example keyword options are "title" and "initialdir".''' + Tk().withdraw() + filePath = filedialog.asksaveasfilename(**kwargs) + return filePath + + +def selectdirectorybox(**kwargs): + '''Creates a select directory dialog box and returns path to the directory + selected or returns empty if Cancel was selected. + Example keyword options are "title", "initialdir", and "mustexist".''' + Tk().withdraw() + filePath = filedialog.askdirectory(**kwargs) + return filePath + + +def listbox(*args): + ''' + Creates a listbox based on multiple arguments in the order that they + are listed. Returns the argument selected or 'None' if the window was + closed or no list options were choosen. + ''' + def getSelection(): + global selectionC + try: + selectionC = listbox.get(listbox.curselection()) + except: + selectionC = None + window.destroy() + window = Tk() + window.title('Choices:') + frame = Frame(window) + listbox = Listbox(frame) + for count, arg in enumerate(args): + count += 1 + listbox.insert(count, arg) + btn = Button(frame, text='Select', command=getSelection) + btn.pack(side='bottom', padx=5, pady=10) + listbox.pack(side='top') + frame.pack(padx=30, pady=30) + window.mainloop() + if 'selectionC' in globals(): + return selectionC + else: + return None