Tk

My collection of old Tk/Tkinter "Stuff"

Example of Tk

image missing

#! /usr/bin/python3
#=======================================================================
# Simple graphics program to execute LDAP command line tools
#=======================================================================

from tkinter import *
import tkinter.font
import os, string, sys

#-----------------------------------------------------------------------
# global variables
#
# cmdflag          display command that is executed
#                    0 - do not display commmand
#                    1 - display command
#
# displaylines     number of lines in the display window
#                  (scroll bars for more thant 10 lines)
#
# ldapindex        index of the currrent ldapserver and ldapbase
#
# ldapserver       list of ldap server built into tool
#
# ldapbase         list of ldap base DN built into the tool
#                  (one for each entry in the ldapserver list)
# 
# ldapadmin        ldap server admin DN
#                  (one for each entry in the ldapserver list)
#
# Note: The password "secret" is built in for every admin DN.
#       This could be changed, but is is OK for now.  The a tool
#       for the class, and not for general use.  There are better
#       (free) tools available on the web.
#
#-----------------------------------------------------------------------

cmdflag      = 0

displaylines = 10

ldapindex    = 0

ldapserver   = ['localhost', 'remoteserver']

ldapbase     = ['dc=mycompany,dc=com', 'dc=othercompany,dc=com']

ldapadmin    = ['cn=Admin,dc=mycompany,dc=com',
                'cn=Admin,dc=othercompany,dc=com']


#-----------------------------------------------------------------------
# LDAP Command Line Tools (OpenLDAP)
#-----------------------------------------------------------------------

ldap_add_tool     = 'ldapmodify'
ldap_compare_tool = 'ldapcompare'
ldap_delete_tool  = 'ldapdelete'
ldap_modify_tool  = 'ldapmodify'
ldap_modrdn_tool  = 'ldapmodrdn'
ldap_passwd_tool  = 'ldappasswd'
ldap_search_tool  = 'ldapsearch'


#-----------------------------------------------------------------------
# MainWindow
#-----------------------------------------------------------------------

# create main window

root = Tk()

f = Frame(root)

f0 = Frame(f, relief="groove", borderwidth=2)

f1 = Frame(f, relief="groove", borderwidth=2)

f2 = Frame(f, relief="groove", borderwidth=2)

f3 = Frame(f, relief="groove", borderwidth=2)

# set main window title
        
root.title("LDAP CLI Tools")


#-----------------------------------------------------------------------
# MainWindow fonts
#-----------------------------------------------------------------------

hfont10b = tkinter.font.Font(family="Helvetica", weight="bold", size=10)
cfont12  = tkinter.font.Font(family="Courier", size=12)

wtfont = hfont10b       # window title font
bhfont = hfont10b       # block title font
btfont = hfont10b       # button font  
bbfont = cfont12        # block body font
twfont = cfont12        # text window font

#-----------------------------------------------------------------------
# button callback routines
#-----------------------------------------------------------------------
    
def ldapadd_command():
    clear_display()
    run_command(ldap_add_tool) 

def ldapadd_init():
    global ldapindex
    global ldapserver
    global ldapbase
#   print "LDAPADD"
    clear_command()
    cmd.insert(END,'-a -x -h "' + ldapserver[ldapindex] +
               '" -f "add_entries.ldif"\n' +
               '-D "' + ldapadmin[ldapindex] +'"\n' +
               '-w "secret"\n')

def ldapcompare_command():
    clear_display()
    run_command(ldap_compare_tool)

def ldapcompare_init():
    global ldapindex
    global ldapserver
    global ldapbase
#   print "LDAPCOMPARE"
    clear_command()
    cmd.insert(END,'-x -v -h "' + ldapserver[ldapindex] + '"\n' +
    '"cn=groupname,ou=personnel,' + ldapbase[ldapindex] + '"' +
    '\n' + '"uniqueMember:' +
    'uid=useruid,ou=personnel,' + ldapbase[ldapindex] + '"')

def ldapdelete_command():
    clear_display()
    run_command(ldap_delete_tool)

def ldapdelete_init():
    global ldapindex
    global ldapserver
    global ldapbase
#   print "LDAPDELETE"
    clear_command()
    cmd.insert(END,'-x -v -h "' + ldapserver[ldapindex] + '"\n' +
               '-D "' + ldapadmin[ldapindex] + '"\n' +
               '-w "secret"\n' +
               '"uid=auser,' + ldapbase[ldapindex] + '"\n')

def ldapmodify_command():
    clear_display()
    run_command(ldap_modify_tool)

def ldapmodify_init():
    global ldapindex
    global ldapserver
    global ldapbase
#   print "LDAPMODIFY"
    clear_command()
    cmd.insert(END,'-x -v -h "' + ldapserver[ldapindex] +
               ' " -f "modify_entries.ldif"\n' +
               '-D "cn=Manager,dc=mycompany,dc=com"\n' +
               '-w "secret"\n')

def ldapmodrdn_command():
    clear_display()
    run_command(ldap_modrdn_tool)

def ldapmodrdn_init():
    global ldapindex
    global ldapserver
    global ldapbase
#   print "LDAPMODRDN"
    clear_command()
    cmd.insert(END,'-v -x -h "' + ldapserver[ldapindex] + '" -r"\n' +
               '-D "' + ldapadmin[ldapindex] + '"\n' +
               '-w "secret"\n' +
               '"uid=juser,' + ldapbase[ldapindex] + '"\n' +
               '"uid=kuser"\n')

def ldappasswd_command():
    clear_display()
    run_command(ldap_passwd_tool)

def ldappasswd_init():
    global ldapindex
    global ldapserver
    global ldapbase
#   print "LDAPPASSWD"
    clear_command()
    cmd.insert(END,'-x -v -h "' + ldapserver[ldapindex] + '" ' +
               '-s "newpassword"\n' +
               '-D "' + ldapadmin[ldapindex] + '"\n' +
               '-w "secret"\n' +
               '"uid=juser,dc=mycompany,dc=com"')

def ldapsearch_command():
    clear_display()
    run_command(ldap_search_tool)

def ldapsearch_init():
    global ldapindex
    global ldapserver
    global ldapbase
#   print "LDAPSEARCH"
    clear_command()
    cmd.insert(END,'-x -s base -h "' + ldapserver[ldapindex] +
               '"\n' + '-b "' + ldapbase[ldapindex] + '"\n' +
               '"(uid=juser)"')
        
def run_window_command():
    clear_display()
    run_command('')

def run_command(c):
    cc = c + ' ' + string.replace(cmd.get(0.0,END),'\n',' ') + ' 2>&1'
    if (cmdflag):
       display_message(cc + '\n')
       display_message('----------------------------------------' +
                       '----------------------------------------\n')

    for l in os.popen(cc).readlines():
        msg.insert(END,l)
        msg.see(END)

def clear_command():
    cmd.delete("0.0",END)

def clear_display():
    msg.delete("0.0",END)

def display_command():
    global cmdflag
    if (cmdflag):
       cmdflag = 0
    else:
       cmdflag = 1

def display_message(m):
    msg.insert(END,m)
    msg.see(END)

def change_server():
    global ldapindex
    clear_command()
    clear_display()
    ldapindex = ldapindex + 1
    if ldapindex >= len(ldapserver):
       ldapindex=0
    server.delete(0,END)
    server.insert(0,ldapserver[ldapindex])


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# create command display - main window
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Label(f0, text="Command",
      font=bhfont).grid(row=0, column=0, padx=4, pady=4)
ff = Frame(f0, relief="flat")
cmd = Text(ff, font=twfont, height=5, width=80, padx=4, pady=4)
cmd.grid(row=0, column=0)
cmdy = Scrollbar(ff, orient=VERTICAL, command=cmd.yview)
cmdy.grid(row=0, column=1, padx=2, pady=0, sticky=N+S)
cmd["yscrollcommand"] = cmdy.set
ff.grid(row=1, column=0, sticky=N+S+E+W)


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# create action buttons - main window
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

ff = Frame(f1)

Button(ff, text="INIT SEARCH", font=btfont, takefocus=0,        \
        command=ldapsearch_init).grid(row=0, column=0,          \
        padx=4, pady=4, sticky=E+W)
Button(ff, text="INIT ADD", font=btfont, takefocus=0,           \
        command=ldapadd_init).grid(row=0, column=1,             \
        padx=4, pady=4, sticky=E+W)
Button(ff, text="INIT MODIFY", font=btfont, takefocus=0,        \
        command=ldapmodify_init).grid(row=0, column=2,          \
        padx=4, pady=4, sticky=E+W)
Button(ff, text="INIT DELETE", font=btfont, takefocus=0,        \
        command=ldapdelete_init).grid(row=0, column=3,          \
        padx=4, pady=4, sticky=E+W)
Button(ff, text="INIT MODRDN", font=btfont, takefocus=0,        \
        command=ldapmodrdn_init).grid(row=0, column=4,          \
        padx=4, pady=4, sticky=E+W)
Button(ff, text="INIT PASSWD", font=btfont, takefocus=0,        \
        command=ldappasswd_init).grid(row=0, column=5,          \
        padx=4, pady=4, sticky=E+W)
Button(ff, text="INIT COMPARE", font=btfont, takefocus=0,       \
        command=ldapcompare_init).grid(row=0, column=6,         \
        padx=4, pady=4, sticky=E+W)

Button(ff, text="SEARCH", font=btfont, takefocus=0,             \
        command=ldapsearch_command).grid(row=1, column=0,       \
        padx=4, pady=4, sticky=E+W)
Button(ff, text="ADD", font=btfont, takefocus=0,                \
        command=ldapadd_command).grid(row=1, column=1,          \
        padx=4, pady=4, sticky=E+W)
Button(ff, text="MODIFY", font=btfont, takefocus=0,             \
        command=ldapmodify_command).grid(row=1, column=2,       \
        padx=4, pady=4, sticky=E+W)
Button(ff, text="DELETE", font=btfont, takefocus=0,             \
        command=ldapdelete_command).grid(row=1, column=3,       \
        padx=4, pady=4, sticky=E+W)
Button(ff, text="MODRDN", font=btfont, takefocus=0,             \
        command=ldapmodrdn_command).grid(row=1, column=4,       \
        padx=4, pady=4, sticky=E+W)
Button(ff, text="PASSWD", font=btfont, takefocus=0,             \
        command=ldappasswd_command).grid(row=1, column=5,       \
        padx=4, pady=4, sticky=E+W)
Button(ff, text="COMPARE", font=btfont, takefocus=0,            \
        command=ldapcompare_command).grid(row=1, column=6,      \
        padx=4, pady=4, sticky=E+W)

ff.grid(row=0, padx=4, pady=4, sticky=E+W)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# create results display - main window
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# execute buttons

ff = Frame(f2, relief="flat")
Button(ff, text="Clear DISPLAY", font=btfont, takefocus=0,          \
       command=clear_display).grid(row=4, column=0,                 \
                                   padx=4, pady=4, sticky=E+W)
Button(ff, text="Clear COMMAND", font=btfont, takefocus=0,          \
       command=clear_command).grid(row=4, column=1,                 \
                                        padx=4, pady=4, sticky=E+W)
Button(ff, text="Run COMMAND", font=btfont, takefocus=0,            \
       command=run_window_command).grid(row=4, column=2,            \
                                        padx=4, pady=4, sticky=E+W)
Button(ff, text="Display Command", font=btfont, takefocus=0,        \
       command=display_command).grid(row=4, column=3,               \
                                   padx=4, pady=4, sticky=E+W)

# LDAP server name or IP address

fff = Frame(ff, relief="flat")
Button(fff, text=">", font=btfont, takefocus=0,                     \
       command=change_server).grid(row=0, column=0,                 \
                                   padx=4, pady=4, sticky=E+W)
server = Entry(fff, width=20,font=bbfont)
server.grid(row=0, column=1, pady=4, sticky=W)
fff.grid(row=4, column=4, padx=4, pady=4, sticky=E+W)
server.insert(0,ldapserver[ldapindex])
ff.grid(row=0, padx=4, pady=4, sticky=E+W)

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# create results display - main window
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Label(f3, text="Display",
      font=bhfont).grid(row=0, column=0, padx=4, pady=4)
ff = Frame(f3, relief="flat")
msg = Text(ff, font=twfont, height=displaylines, width=80, padx=4, pady=4)
msg.grid(row=0, column=0)
msgy = Scrollbar(ff, orient=VERTICAL, command=msg.yview)
msgy.grid(row=0, column=1, padx=2, pady=0, sticky=N+S)
msg["yscrollcommand"] = msgy.set
ff.grid(row=0, column=0, sticky=N+S+E+W)


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# main loop
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

f0.grid(row=0, column=0, padx=4, pady=4, sticky=N+S+E+W)

f1.grid(row=1, column=0, padx=4, pady=4, sticky=S+E+W)

f2.grid(row=2, column=0, padx=4, pady=4, sticky=S+E+W)

f3.grid(row=3, column=0, padx=4, pady=4, sticky=N+S+E+W)

f.grid(padx=4, pady=4)

root.mainloop()