Is it possible to get widget settings in Tkinter?

后端 未结 2 1744
南笙
南笙 2020-12-03 15:19

It\'d be awesome if I could get something like the below.

Pseudo Code:

U = widget1.SettingsGet()
Print U 

Upon printing U something

2条回答
  •  攒了一身酷
    2020-12-03 15:30

    To get all attributes of a widget from cget, you can use keys() to get the attributes and then cget to get the value of those atributes. IE:

        from tkinter import *
        root=Tk()
        w=Button(root)
        for item in w.keys():
            print(item)
            print(w.cget(item))
    

    This code returns:

        activebackground
        systemButtonFacePressed
        activeforeground
        systemPushButtonPressedText
        anchor
        center
        background
        White
        bd
        2
        bg
        White
        bitmap
    
        borderwidth
        2
        command
    
        compound
        none
        cursor
    
        default
        disabled
        disabledforeground
        #a3a3a3
        fg
        systemButtonText
        font
        TkDefaultFont
        foreground
        systemButtonText
        height
        0
        highlightbackground
        White
        highlightcolor
        systemButtonFrame
        highlightthickness
        4
        image
    
        justify
        center
        overrelief
    
        padx
        12
        pady
        3
        relief
        flat
        repeatdelay
        0
        repeatinterval
        0
        state
        normal
        takefocus
    
        text
    
        textvariable
    
        underline
        -1
        width
        0
        wraplength
        0
    

    That was a lot of spaces >.<

    In this block the first is the key and the second is the value of said key. The keys method (for all widgets) returns all of the keys in said widget. Config returns the keys and what they apply to (cursor vs Cursor) where keys just gives a list of all attributes.

提交回复
热议问题