Update Label Text in Python TkInter

前端 未结 3 1340
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 10:47

Is there any possible way to create a TkInter label that uses a string and a variable as the text?

For example:

name = \"bob\"
Label(root, text=\"hel         


        
3条回答
  •  梦毁少年i
    2021-01-07 11:13

    Yes -- standard Tkinter <variable>-s mechanics does that:

    There is a Tkinter StringVar() ( and similarly IntVar(), DoubleVar(), BoolVar() ) object constructor, that prepares a smart object that is ready to be later used for this very purpose in Tkinter Widgets.

    You may use .set() / .get() methods for manipulating with such object's value(s).

    name              = StringVar()        # this creates a Tkinter object
    name.set( "bob" )                      # .set() assigns / .get() retrieves
    L = Label( root, textvariable = name ) # makes the  used in Label Widget
    

    Label text gets changed right by a new value gets assigned in <variable>

    name.set( "alice" )                    # .set() assigns a new value -> promoted
    print L['text']                        # show, a value has been promoted in L
    

    FYI: Advanced <variable>-s' Tools

    You may also want to know about a more advanced tools for Tkinter variables. There are also more powerfull tools associated with Tkinter variables -- called trace-er(s) -- which set the Tkinter system to "watch" any change to a "traced" variable and this can associate further automated responsive activities, automatically launched upon a traced-event-type.

    aWriteTraceID = name.trace_variable( "w", f2CallOnWriteAccessToTracedVariable )
    aRead_TraceID = name.trace_variable( "r", f2CallOnRead_AccessToTracedVariable )
    aDel__TraceID = name.trace_variable( "u", f2CallOnDel__AccessToTracedVariable )
    
    name.trace_vinfo()                        # show all associated <>
    
    >>> name.trace_vinfo()
    [('u', '12945528f2CallOnDel__AccessToTracedVariable'),
     ('r', '12251384f2CallOnRead_AccessToTracedVariable'),
     ('w', '12760924f2CallOnWriteAccessToTracedVariable')
    ]
    
    name.trace_vdelete( aRead_TraceID )       # delete an identified <>
    name.trace_vdelete( aWriteTraceID )       # delete an identified <>
    
    del( name )                               # del() makes name undefined
    # so this will "auto-launch" the last, still active <>
    # you assigned above -- the function f2CallOnDel__AccessToTracedVariable()
    

    This instrumentation helps you create your GUI toolbox strategies very powerfull for an efficient, event-driven, fully self-reflecting layered [Model-Visual-Controller], supervised under the hood of the Tkinter.mainloop() scheduler

    How to put it together?

    As abarnert has proposed, the automated version may look in principle like this

    name = StringVar()                         # a pure name holder
    show = StringVar()                         # a post-processed text
    
    L = Label( root, textvariable = show )     # L will display a post-processed string
    L.pack()                                   # L goes into GUI framework's geometry manager
    
    #                                          # prepare the <> function
    def autoProcessAndPropagateOnNameVarCHANGE( p1, p2, p3, p4 = name, p5 = show ):
        #                                      # this function will get called
        #                                      # upon WRITE-ACCESS <>
        #
        # .set( a post-processed value ) into [show], that is connected to GUI[Label]
        p5.set( "Hello, " + p4.get() )         
        #                                      # Always be carefull not to fire
        #                                      # an unstoppable chain-reaction ;)
        #                                      # of <>-related events
        #                                      # as more <>-s get used
    
    #                                          # create <> / <> pair
    aWriteTraceID = name.trace_variable( "w", autoProcessAndPropagateOnNameVarCHANGE )
    
    # -------------------------------------------------------------------------------
    # test <>:
    name.set( "Craig" )                        # <>-watched WRITE-ACCESS
    # test <> result: GUI Label L shall show "Hello, Craig" -----------------
    
    # -------------------------------------------------------------------------------
    # erase all <>-s assigned:
    name.trace_vinfo()                         # initial state of <>-s
    
    for aTracerRECORD in name.trace_vinfo():
        name.trace_vdelete( aTracerRECORD[0], aTracerRECORD[1] )
    
    # erase [[[DONE]]] --------------------------------------------------------------
    name.trace_vinfo()                         # final   state of <>-s
    

提交回复
热议问题