在Tkinter中可以获取小部件设置吗?
如果我能得到下面这样的东西,那就太好了。
伪代码:
U = widget1.SettingsGet()
Print U
当我打印U的时候,会返回类似这样的东西:
widget1(background='green',foreground='grey',boarderwidth=10, relief='flat')
能够获取小部件的设置会非常有用。这样我就可以根据这些设置来调整其他小部件。
3 个回答
这段代码可能不适合用在正式的生产环境中,因为它使用了 _tkinter
,不过你可以这样做:
import tkinter as tk
import _tkinter
def con_dict(w):
options = {}
for i in w.keys():
value = w.cget(i)
options[i] = value.string if type(value) is _tkinter.Tcl_Obj else value
return options
root = tk.Tk()
widget = tk.Label(
root, text='test', relief='raised', borderwidth=3)
widget.pack()
print(con_dict(widget))
root.mainloop()
这段代码对我输出了:
{'activebackground': 'SystemButtonFace', 'activeforeground': 'SystemButtonText', 'anchor': 'center', 'background': 'SystemButtonFace', 'bd': 3, 'bg': 'SystemButtonFace', 'bitmap': '', 'borderwidth': 3, 'compound': 'none', 'cursor': '', 'disabledforeground': 'SystemDisabledText', 'fg': 'SystemButtonText', 'font': 'TkDefaultFont', 'foreground': 'SystemButtonText', 'height': 0, 'highlightbackground': 'SystemButtonFace', 'highlightcolor': 'SystemWindowFrame', 'highlightthickness': 0, 'image': '', 'justify': 'center', 'padx': 1, 'pady': 1, 'relief': 'raised', 'state': 'normal', 'takefocus': '0', 'text': 'test', 'textvariable': '', 'underline': -1, 'width': 0, 'wraplength': 0}
要从一个小部件获取所有属性,可以先用keys()来获取这些属性的名字,然后再用cget来获取这些属性的具体值。比如:
from tkinter import *
root=Tk()
w=Button(root)
for item in w.keys():
print(item)
print(w.cget(item))
这段代码的返回结果是:
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
那是很多空格 >.<
在这个代码块中,第一个是属性的名字,第二个是这个属性的值。keys方法(适用于所有小部件)会返回这个小部件的所有属性名。Config方法则返回属性名和它们所对应的内容(比如cursor和Cursor),而keys方法只是给出所有属性的一个列表。
如果你知道需要哪些设置,可以直接使用 cget
方法来获取这些值,比如:
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
print w.cget('text')
root.mainloop()
这样会打印出结果:
Hello, world!
如果你想知道所有可用的选项,可以查看 widget.config,这里面包含了所有的配置。你可以从中选择你需要的全部或部分设置,比如:
import pprint
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
pprint.pprint(w.config())
root.mainloop()
输出结果:
{'activebackground': ('activebackground',
'activeBackground',
'Foreground',
<border object at 00C665D0>,
'SystemButtonFace'),
'activeforeground': ('activeforeground',
'activeForeground',
'Background',
<color object at 00C66C48>,
'SystemButtonText'),
'anchor': ('anchor',
'anchor',
'Anchor',
<index object at 00C66588>,
'center'),
'background': ('background',
'background',
'Background',
<border object at 0119A3E0>,
'SystemButtonFace'),
'bd': ('bd', '-borderwidth'),
'bg': ('bg', '-background'),
'bitmap': ('bitmap', 'bitmap', 'Bitmap', '', ''),
'borderwidth': ('borderwidth',
'borderWidth',
'BorderWidth',
<pixel object at 0119A0F8>,
<pixel object at 0119A0F8>),
'compound': ('compound',
'compound',
'Compound',
<index object at 011A90D8>,
'none'),
'cursor': ('cursor', 'cursor', 'Cursor', '', ''),
'disabledforeground': ('disabledforeground',
'disabledForeground',
'DisabledForeground',
<color object at 011A5DB0>,
'SystemDisabledText'),
'fg': ('fg', '-foreground'),
'font': ('font',
'font',
'Font',
<font object at 011A5E40>,
(('MS', 'Sans', 'Serif'), '8')),
'foreground': ('foreground',
'foreground',
'Foreground',
<color object at 011A5F48>,
'SystemButtonText'),
'height': ('height', 'height', 'Height', 0, 0),
'highlightbackground': ('highlightbackground',
'highlightBackground',
'HighlightBackground',
<border object at 011A5EE8>,
'SystemButtonFace'),
'highlightcolor': ('highlightcolor',
'highlightColor',
'HighlightColor',
<color object at 011A5F78>,
'SystemWindowFrame'),
'highlightthickness': ('highlightthickness',
'highlightThickness',
'HighlightThickness',
<pixel object at 011A5FA8>,
<pixel object at 011A5FA8>),
'image': ('image', 'image', 'Image', '', ''),
'justify': ('justify',
'justify',
'Justify',
<index object at 011A5F30>,
'center'),
'padx': ('padx',
'padX',
'Pad',
<pixel object at 011A5FC0>,
<pixel object at 011A5FC0>),
'pady': ('pady',
'padY',
'Pad',
<pixel object at 011A5FD8>,
<pixel object at 011A5FD8>),
'relief': ('relief', 'relief', 'Relief', <index object at 011A5FF0>, 'flat'),
'state': ('state', 'state', 'State', <index object at 011A5EA0>, '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',
<pixel object at 00C50908>,
<pixel object at 00C50908>)}