Is it possible to get widget settings in Tkinter?

后端 未结 2 1751
南笙
南笙 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:41

    If you know what settings you need, you can just use the cget method to get values e.g.

    from Tkinter import *
    
    root = Tk()
    
    w = Label(root, text="Hello, world!")
    w.pack()
    print w.cget('text')
    root.mainloop()
    

    It will print

    Hello, world!
    

    If you want to know all the available options, widget.config contains the config and from that if you wish you can create all or a subset of settings which you may need e.g.

    import pprint
    from Tkinter import *
    
    root = Tk()
    w = Label(root, text="Hello, world!")
    w.pack()
    pprint.pprint(w.config())
    root.mainloop()
    

    Output:

    {'activebackground': ('activebackground',
                          'activeBackground',
                          'Foreground',
                          ,
                          'SystemButtonFace'),
     'activeforeground': ('activeforeground',
                          'activeForeground',
                          'Background',
                          ,
                          'SystemButtonText'),
     'anchor': ('anchor',
                'anchor',
                'Anchor',
                ,
                'center'),
     'background': ('background',
                    'background',
                    'Background',
                    ,
                    'SystemButtonFace'),
     'bd': ('bd', '-borderwidth'),
     'bg': ('bg', '-background'),
     'bitmap': ('bitmap', 'bitmap', 'Bitmap', '', ''),
     'borderwidth': ('borderwidth',
                     'borderWidth',
                     'BorderWidth',
                     ,
                     ),
     'compound': ('compound',
                  'compound',
                  'Compound',
                  ,
                  'none'),
     'cursor': ('cursor', 'cursor', 'Cursor', '', ''),
     'disabledforeground': ('disabledforeground',
                            'disabledForeground',
                            'DisabledForeground',
                            ,
                            'SystemDisabledText'),
     'fg': ('fg', '-foreground'),
     'font': ('font',
              'font',
              'Font',
              ,
              (('MS', 'Sans', 'Serif'), '8')),
     'foreground': ('foreground',
                    'foreground',
                    'Foreground',
                    ,
                    'SystemButtonText'),
     'height': ('height', 'height', 'Height', 0, 0),
     'highlightbackground': ('highlightbackground',
                             'highlightBackground',
                             'HighlightBackground',
                             ,
                             'SystemButtonFace'),
     'highlightcolor': ('highlightcolor',
                        'highlightColor',
                        'HighlightColor',
                        ,
                        'SystemWindowFrame'),
     'highlightthickness': ('highlightthickness',
                            'highlightThickness',
                            'HighlightThickness',
                            ,
                            ),
     'image': ('image', 'image', 'Image', '', ''),
     'justify': ('justify',
                 'justify',
                 'Justify',
                 ,
                 'center'),
     'padx': ('padx',
              'padX',
              'Pad',
              ,
              ),
     'pady': ('pady',
              'padY',
              'Pad',
              ,
              ),
     'relief': ('relief', 'relief', 'Relief', , 'flat'),
     'state': ('state', 'state', 'State', , 'normal'),
     'takefocus': ('takefocus', 'takeFocus', 'TakeFocus', '0', '0'),
     'text': ('text', 'text', 'Text', '', ('Hello,', 'world!')),
     'textvariable': ('textvariable', 'textVariable', 'Variable', '', ''),
     'underline': ('underline', 'underline', 'Underline', -1, -1),
     'width': ('width', 'width', 'Width', 0, 0),
     'wraplength': ('wraplength',
                    'wrapLength',
                    'WrapLength',
                    ,
                    )}
    

提交回复
热议问题