Dup Ver Goto 📝

BigMessage

PT2/lang/python/tk/utilities does not exist
To
90 lines, 287 words, 2467 chars Page 'BigMessage' does not exist.

A simple example of how to put a big message on the screen. Useful for telling you a long running background job is done.

#!/usr/bin/env python3

prompt = r"""
python tkinter command line display message in big text
"""

prompt = r"""
It takes the message from the command line, uses a main() and argparse, and
allows options to be configured by command line switches
"""

prompt = r"""
It should allow pressing q or escape to exit.
"""

prompt = r"""
It should join all arguments with a space (like echo does)
"""

import tkinter as tk
from tkinter import font
import argparse
import sys

def display_message(message, font_size, font_color, bg_color):
    root = tk.Tk()
    root.title("Message Display")
    root.configure(bg=bg_color)

    # Start with a centered window
    root.geometry("900x500")

    # Define the font
    custom_font = font.Font(family="Helvetica", size=font_size, weight="bold")

    # Create the label
    # 'wraplength' allows text to move to a new line if it's too wide
    label = tk.Label(
        root, 
        text=message, 
        font=custom_font, 
        fg=font_color, 
        bg=bg_color,
        wraplength=800,
        justify="center"
    )
    label.pack(expand=True, fill="both")

    # Exit bindings
    root.bind("<Escape>", lambda e: root.destroy())
    root.bind("q", lambda e: root.destroy())
    root.bind("Q", lambda e: root.destroy())

    # Focus the window so keys work immediately
    root.focus_force()
    root.attributes('-topmost', True)

    root.mainloop()

def main():
    parser = argparse.ArgumentParser(
        description="Display a large text message. Joins all arguments like 'echo'."
    )

    # Using nargs='+' collects all words into a list
    parser.add_argument("message", nargs="+", help="The text message to display")

    # Options
    parser.add_argument("-s", "--size", type=int, default=80, help="Font size (default: 80)")
    parser.add_argument("-f", "--fg", default="black", help="Text color (default: black)")
    parser.add_argument("-b", "--bg", default="white", help="Background color (default: white)")

    args = parser.parse_args()

    # Join the list of words into a single string
    full_message = " ".join(args.message)

    try:
        display_message(full_message, args.size, args.fg, args.bg)
    except tk.TclError as e:
        print(f"Error: Could not display window. {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()