Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Banner text
#1
Sharing a tool that a wrote that creates a uniform 'banner'.

I use use this for creating code block markers as well as banners for cli based apps, but use it how you will.

def banner(**args):
    if not args:
        print("Remember: do one thing and do it well.")
        return
    else:
        text = args['text']
        length = args['length']
    print("#", end='')
    if len(text) % 2:
        print(f"<{text}>".center(length, '='), end='')
    else:
        print(f"<{text}>".center(length + 1, '='), end='')
    print("#", end='')
    print()
    return


length = 44
print()

text = input("Banner text: ")
if text:
    banner(text=text, length=length)
else:
    banner()
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#2
Update:

I've been learning to use a code generator called PAGE by converting this CLI app of mine, into a GUI app, which I'm happy to share with you all.

The app needs two files:

File 1: banner.py
#! /usr/bin/env python3
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 7.6
#  in conjunction with Tcl version 8.6
#    May 12, 2023 12:27:46 AM BST  platform: Linux

import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import os.path

_script = sys.argv[0]
_location = os.path.dirname(_script)

import banner_support

_bgcolor = '#d9d9d9'  # X11 color: 'gray85'
_fgcolor = '#000000'  # X11 color: 'black'
_compcolor = 'gray40' # X11 color: #666666
_ana1color = '#c3c3c3' # Closest X11 color: 'gray76'
_ana2color = 'beige' # X11 color: #f5f5dc
_tabfg1 = 'black' 
_tabfg2 = 'black' 
_tabbg1 = 'grey75' 
_tabbg2 = 'grey89' 
_bgmode = 'light' 

class bannerMain:
    def __init__(self, top=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''

        top.geometry("650x325+359+232")
        top.minsize(1, 1)
        top.maxsize(1351, 738)
        top.resizable(0,  0)
        top.title("Banner Generator")
        top.configure(highlightcolor="black")
        top.configure(pady="5")

        self.top = top
        self.inputVar = tk.StringVar()
        self.HScaleData = tk.DoubleVar()

        self.labInput = tk.Label(self.top)
        self.labInput.place(relx=0.083, rely=0.055, height=23, width=96)
        self.labInput.configure(activebackground="#f9f9f9")
        self.labInput.configure(anchor='w')
        self.labInput.configure(compound='left')
        self.labInput.configure(font="-family {DejaVu Sans} -size 12")
        self.labInput.configure(text='''Input''')
        self.bannerOut = tk.Text(self.top)
        self.bannerOut.place(relx=0.085, rely=0.542, relheight=0.169
                , relwidth=0.826)
        self.bannerOut.configure(background="white")
        self.bannerOut.configure(font="-family {DejaVu Sans Mono} -size 12")
        self.bannerOut.configure(pady="12")
        self.bannerOut.configure(selectbackground="#c4c4c4")
        self.bannerOut.configure(wrap="word")
        self.labOut = tk.Label(self.top)
        self.labOut.place(relx=0.077, rely=0.437, height=23, width=72)
        self.labOut.configure(activebackground="#f9f9f9")
        self.labOut.configure(anchor='w')
        self.labOut.configure(compound='left')
        self.labOut.configure(font="-family {DejaVu Sans} -size 12")
        self.labOut.configure(text='''Output''')
        self.btnGenerate = tk.Button(self.top)
        self.btnGenerate.place(relx=0.255, rely=0.769, height=33, width=73)
        self.btnGenerate.configure(activebackground="beige")
        self.btnGenerate.configure(borderwidth="2")
        self.btnGenerate.configure(command=banner_support.on_btn_Gen)
        self.btnGenerate.configure(compound='left')
        self.btnGenerate.configure(text='''Generate''')
        self.btnQuit = tk.Button(self.top)
        self.btnQuit.place(relx=0.568, rely=0.769, height=33, width=73)
        self.btnQuit.configure(activebackground="beige")
        self.btnQuit.configure(borderwidth="2")
        self.btnQuit.configure(command=banner_support.on_btn_Quit)
        self.btnQuit.configure(compound='left')
        self.btnQuit.configure(state='active')
        self.btnQuit.configure(text='''Quit''')
        self.menubar = tk.Menu(top,font="TkMenuFont",bg=_bgcolor,fg=_fgcolor)
        top.configure(menu = self.menubar)

        self.bannerIn = tk.Entry(self.top)
        self.bannerIn.place(relx=0.085, rely=0.123, height=50, relwidth=0.825)
        self.bannerIn.configure(background="white")
        self.bannerIn.configure(font="-family {DejaVu Sans Mono} -size 12")
        self.bannerIn.configure(selectbackground="#c4c4c4")
        self.bannerIn.configure(textvariable=self.inputVar)
        self.HScale =  tk.Scale(self.top, from_=20.0, to=50.0, resolution=1.0)
        self.HScale.place(relx=0.206, rely=0.326, relheight=0.203
                , relwidth=0.615)
        self.HScale.configure(activebackground="beige")
        self.HScale.configure(command=banner_support.on_HScale)
        self.HScale.configure(label="Width")
        self.HScale.configure(length="354")
        self.HScale.configure(orient="horizontal")
        self.HScale.configure(troughcolor="#d9d9d9")
        self.HScale.configure(variable=self.HScaleData)

def start_up():
    banner_support.main()

if __name__ == '__main__':
    banner_support.main()
File 2: banner_support.py
#! /usr/bin/env python3
#  -*- coding: utf-8 -*-
#
# Support module generated by PAGE version 7.6
#  in conjunction with Tcl version 8.6
#    May 11, 2023 09:20:41 PM BST  platform: Linux

import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *

import banner

_debug = False  # False to eliminate debug printing from callback functions.


def main(*args):
    '''Main entry point for the application.'''
    global root
    root = tk.Tk()
    root.protocol('WM_DELETE_WINDOW', root.destroy)
    # Creates a toplevel widget.
    global _top1, _w1
    _top1 = root
    _w1 = banner.bannerMain(_top1)
    root.mainloop()


def banner_gen(**args):
    if _debug:
        print(f'banner_gen args: {args}')
    if len(args['text']) > 0:
        msg = args['text']
        length = args['length']
    else:
        msg = "Remember: do one thing and do it well."
        length = 50
    if len(msg) % 2:
        msg = "#" + f"<{msg}>".center(length, '=')
    else:
        msg = "#" + f"<{msg}>".center(length + 1, '=')
    msg += "#"
    return msg


def on_HScale(*args):
    if _debug:
        print('banner_support.on_HScale')
        for arg in args:
            print('    another arg:', arg)
        sys.stdout.flush()


def on_btn_Gen(*args):
    if _debug:
        print('banner_support.on_btn_Gen')
        for arg in args:
            print('    another arg:', arg)
        sys.stdout.flush()
        valu = _w1.HScaleData.get()
        text = _w1.inputVar.get()
        print(f'_w1.inputVar.get(): {text}')
        print(f'Len of _w1.inputVar.get(): {len(text)}')
        BannerText = banner_gen(text=text, length=int(valu))
        print(BannerText)
    valu = _w1.HScaleData.get()
    text = _w1.inputVar.get()
    BannerText = banner_gen(text=text, length=int(valu)-1) # 'off-by-one' bug fix
    _w1.bannerOut.delete('1.0', END)
    _w1.bannerOut.insert('1.0', BannerText)



def on_btn_Quit(*args):
    if _debug:
        print('banner_support.on_btn_Quit')
        for arg in args:
            print('    another arg:', arg)
        sys.stdout.flush()
    root.destroy()

if __name__ == '__main__':
    banner.start_up()
If you have any questions about this, then I'll answer them the best way that I can.

Thanks for reading.
Gribouillis likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
It works very well, thanks!

Suggestion: add code to copy the banner to the X selection (assuming you are in Linux). Here is code to do that
import subprocess as sp
banner = '#========<spam eggs ham>=========#'
sp.Popen(['xsel', '-i', '-b'], stdin=sp.PIPE).communicate(banner.encode())
Then paste the banner in other apps with Ctrl-V
rob101 likes this post
Reply
#4
(May-18-2023, 09:26 AM)Gribouillis Wrote: It works very well, thanks!

Suggestion: add code to copy the banner to the X selection (assuming you are in Linux).

You're very welcome and thank you for the suggestion, as well as the code for said.

Yes, I'm a Linux user and I will take note of the code that you've kindly provided.

As is, the app should be cross-platform friendly (Note: I don't have any way of testing that), so I'll not update the posted code, if the change will break anything. That said, I know that the OS on which the app is being hosted can be queried so that any compatibility issues can be dealt with, but I've other projects from which I do not want to take any time out.

For anyone using a 'mouse' with a clickable scroll wheel:
1. Triple L/click the text in the Output box
2. Go to the app into which you want the Banner to be placed
3. Single click the scroll wheel at an insertion point
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020