A natural magnet for the craziest TKinter lovers out there

Dan Kolis dankolis at gmail.com
Wed Jan 18 20:01:01 EST 2023


Hangs after maybe between 4 and 50 screen rewrites. sometimes CTRL C under Ubuntu starts it up again. Click go rewrites al the fonts the thing can find in a few windows.... Repeated.

TKinter has a cult following. I don''t really expect others to fix my problems, but you know, es since its a cult thing to love TCL ( or something .... ) Here is is.

 Same on two different computers in Python 3.6 and 3.8

Plus I suppose you get to see for sure all your finest fonts as rendered in a real TK thing, not that this changes the worlds so much.


Thanks for the advice so far...
Regards,
Dan Kolis

# Code version 18Jan2023 19:48 EST Dan Kolis
# Click 'go' on main page. when run over and over after a while it freezes


import tkinter as           tk
from tkinter import         ttk
from tkinter import font    as      luFont


# Empty object maker ( M T ) ... get it say it !
class MT():
    pass
    

# Module level variables
mod = MT()
mod.itsPlaces = MT()
mod.itsPlaces = []


# Apply XY Size to things like windows, variable place 31Jul22
def apply_Width_Height_Position( toThis, aWidth, aHeight, *argsPlace ):

    # Width and height is expected

    # Argument overflow is quite variable size is it its usual hint for me
    aLen = len( argsPlace )

    # Sometimes, just width and height are given ( not that + position )
    wAndH = str( int( aWidth ) ) +'x' + str( int( aHeight ) )                   # Size has a funny text line interface
    toThis.geometry( wAndH )                                                    # Apply

    # Sometimes position too
    if aLen == 2:
        eP = "+" + str( argsPlace[ 0 ] ) + "+" +  str( argsPlace[ 1 ] )
        param4 = wAndH + eP                                                     # Combine size and position
        toThis.geometry( param4 )                                               # Apply
    
    jjjjjj = 560


# Create local   toplevel   to whoever calls me 11Dec22 bc-endive
def make_Tk_Window( sReason, *attribKc ):

    # Top level window maker and rememberer
    # Kc = 'Kind code'

    aCode = "na"                                                                        # Not available
    if attribKc:    
        aCode = attribKc [ 0 ]                                                          # Better code then 'na'

    # Make window
    ansWin = tk.Toplevel()

    # Keep track of this creation step
    newEntry = {}
    newEntry[ 'tkaddress' ] = ansWin                                                    # Real TK window accessor
    newEntry[ 'ideanamed' ] = sReason                                                   # Casual description
    newEntry[ 'kindcode'  ] = aCode                                                     # So can be worked with together

    mod.itsPlaces.append( newEntry )

    return ansWin


# Idle time rewrite
def justRedisplayNow( wThings,  *argsR ):

    # Redisplay a named tj thing
    # Dont use this frivolously, if it's needed its here you have to study it

    wThings.update()      


# Look at fonts possible 6Oct22
def showAllFonts():

    # Show fonts possible

    mod.lines_In_Window = 30; mod.window_Seq_Number = 1

    # What we want to see
    mod.seeFonts = list( luFont.families() )
    mod.seeFonts.sort()                                                                 # Alpha order is nice

    # Fill windows as a look at me report
    try:
        populateW()
    except:
        tttttt = 456

    rrrrrr = 245


# Show fonts regarding avoiding BAD ones that crash, etc size limits in buffers too 19Dec22
def populateW():

    # Put in the fonts

    aLen = len( mod.seeFonts )                                                          # Fun to know, debug really
    last_considered_name = "gghfjhljfl"                                                 # Don't show doubles

    # Loop through all fonts the system named
    thisPassMakeWindow = True; a_Line_Count = 0; list_Number = 0
    for item in mod.seeFonts:

        # Multiple windows ( Python has funny ideas on this, ok like this )
        if thisPassMakeWindow:
            winTk_Now = winMakerFontsW(); thisPassMakeWindow = False; a_Line_Count = 0  # We gots one now

        if a_Line_Count < mod.lines_In_Window:                                               # Will a window we made
            list_Number += 1                                                            # Counts
            aN = str( list_Number ) + " "; name_In_lower = item.lower()

            # Various reasons to not render this one
            isOk = True
            if name_In_lower == last_considered_name:
                isOk = False
            a_Bad_Thing = name_In_lower.find( 'emoji' ) > -1
            if a_Bad_Thing:
                isOk = False
            a_Bad_Thing = item.find( 'Noto Sans Mono CJK' ) > -1
            if a_Bad_Thing:
                isOk = False
            
            # Good good
            if isOk:
                label = "listlabel" + str( list_Number )
                label = tk.Label( winTk_Now, text=aN + item, font=( item, 18 ), anchor='w' ).pack()
            a_Line_Count += 1
            last_considered_name = name_In_lower
        else:
            justRedisplayNow( winTk_Now )
            thisPassMakeWindow = True; mod.window_Seq_Number += 1                               # New window next time
    
    # Done
    a_Msg = "on this machine"


# Look at fonts possible 6Oct22
def showAllFonts():

    # Show fonts possible

    mod.lines_In_Window = 30; mod.window_Seq_Number = 1

    # What we want to see
    mod.seeFonts = list( luFont.families() )
    mod.seeFonts.sort()                                                                 # Alpha order is nice

    # Fill windows as a look at me report
    try:
        populateW()
    except:
        tttttt = 456

    rrrrrr = 245


# Just window shape aka size
def applyWidthHeight( toThis, aWidth, aHeight ):

    # TK window size no change in code anyway, to placement

    # Op sys like thing likes this x that numbers
    wAndH = str( int( aWidth ) ) +'x' + str( int( aHeight ) )                   # Size
    toThis.geometry( wAndH )                                                    # Apply
    tttttt = 385


# Window making
def winMakerFontsW():

    # Window with scroll bars, etc canvassy, ready to fill up 

    # The real window gets created
    mInf = ' ' + str( mod.window_Seq_Number )
    wsFr = make_Tk_Window( 'Font peek' + mInf, 'fontpeek' ); 
    wsFr.title( 'Fonts' + mInf )
    start_Hor = 50; Kh = 110; start_Ver = 35; Kv = 40
    moved_Some_H = start_Hor + ( mod.window_Seq_Number * ( Kh - 1 ) )
    moved_Some_V = start_Ver + ( mod.window_Seq_Number * ( Kv - 1 ) )
    apply_Width_Height_Position( wsFr, 480, 500, moved_Some_H, moved_Some_V )

    # Canvas is the form pretty much
    my_Canvas = tk.Canvas( wsFr, borderwidth=0 )
    myFrm = tk.Frame( my_Canvas )                       # Now the 'do it on me' thing

    # Vertical scroll bars go onto form
    vsb = tk.Scrollbar( wsFr, orient="vertical", command=my_Canvas.yview )
    my_Canvas.configure( yscrollcommand=vsb.set )

    # Assemble via relations of pieces
    vsb.pack( side="right", fill="y" )
    my_Canvas.pack( side="left", fill="both", expand=True )
    my_Canvas.create_window( (4, 4 ), window=myFrm, anchor="nw" )

    return myFrm


def callIt():

    # A click do it

    showAllFonts()


# This is main 
root = tk.Tk()

applyWidthHeight( root, 300, 300 )

b1 = tk.Button( root, text='go', command=callIt )
b1.grid( row=0, column=0 )


root.mainloop()







More information about the Python-list mailing list