tkinter的Setting Options——参数设置选项

原文链接:Setting Options

Google翻译:

方便参考
设置选项
选项控制小部件的颜色和边框宽度等内容。可以通过三种方式设置选项:

① 在对象创建时,使用关键字参数

fred = Button(self, fg="red", bg="blue")

②创建对象后,将选项名称视为字典索引

fred["fg"] = "红色"
fred["bg"] = "蓝色"

③创建对象后,使用 config() 方法更新多个属性

fred.config(fg="red", bg="blue")

有关给定选项及其行为的完整说明,请参阅相关小部件的 Tk 手册页。

请注意,手册页列出了每个小部件的“标准选项”和“小部件特定选项”。前者是许多小部件共有的选项列表,后者是该特定小部件特有的选项。标准选项记录在 options(3) 手册页上。

本文档中不区分标准选项和特定于小部件的选项。某些选项不适用于某些类型的小部件。给定的小部件是否响应特定选项取决于小部件的类别;按钮有一个命令选项,标签没有。

给定小部件支持的选项列在该小部件的手册页中,或者可以在运行时通过调用不带参数的 config() 方法或调用该小部件上的 keys() 方法来查询。这些调用的返回值是一个字典,其键是字符串形式的选项名称(例如,'relief'),其值为 5 元组。

一些选项,例如 bg 是具有长名称的常用选项的同义词(bg 是“背景”的简写)。将 config() 方法传递给速记选项的名称将返回一个 2 元组,而不是 5 元组。传回的 2 元组将包含同义词的名称和“真实”选项(例如 ('bg', 'background'))。

Index

Meaning

Example

0

选项名称

'relief'

1

数据库查找的选项名称

'relief'

2

用于数据库查找的选项类

'Relief'

3

默认值

'raised'

4

当前值

'groove'

例如:

>>> print(fred.config())
{'relief': ('relief', 'relief', 'Relief', 'raised', 'groove')}

当然,打印的字典将包括所有可用选项及其值。 这仅作为示例。


下面内容不是原文的内容,是演示fred.keys()、fred.config()用的

import tkinter as tk

##import colorama#解决颜色显示乱码
##colorama.init(autoreset=True)

import os#解决颜色显示乱码
if os.name == "nt":
    os.system("")

fred = tk.Label(bg="blue")

print(f"\033[1;32m fred.keys()=\n{fred.keys()} \033[0m")#绿

print(f"\033[1;33m fred.config()['bg']=\n{fred.config()['bg']}\033[0m")#黄
print(f"\033[1;34m fred.config()['background']=\n{fred.config()['background']}\033[0m")#蓝
print(f"\033[1;31m fred.config()=\n{fred.config()}\033[0m")#红

运行结果:

 fred.keys()=
['activebackground', 'activeforeground', 'anchor', 'background', 'bd', 'bg', 'bitmap', 'borderwidth', 'compound', 'cursor', 'disabledforeground', 'fg', 'font', 'foreground', 'height', 'highlightbackground', 'highlightcolor', 'highlightthickness', 'image', 'justify', 'padx', 'pady', 'relief', 'state', 'takefocus', 'text', 'textvariable', 'underline', 'width', 'wraplength']

 fred.config()['bg']=
('bg', '-background')

 fred.config()['background']=
('background', 'background', 'Background', <string object: 'SystemButtonFace'>, 'blue')

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

演示Label、Button设置参数的差别:

import tkinter as tk


##import colorama#解决颜色显示乱码
##colorama.init(autoreset=True)

import os#解决颜色显示乱码
if os.name == "nt":
    os.system("")

Label = tk.Label(bg="blue")
Labelconfig=Label.config()
Button = tk.Button( bg="blue")
Buttonconfig=Button.config()

print(f"\033[1;33m fred.keys()={Labelconfig.keys()} \033[0m")#黄
print(f"\033[1;32m fred.keys()={Buttonconfig.keys()}\033[0m")#绿
ll=list(Labelconfig.keys())
ll.extend(Buttonconfig.keys())
ss=set(ll)
for key in ss:
    if key not in Labelconfig.keys():
        print("\033[1;33;41m  {:15}\033[0m".format(key))#黄
    if key not in Buttonconfig.keys():
        print("\033[1;32;41m  {:15}\033[0m".format(key))#绿

运行结果:

 fred.keys()=dict_keys(['activebackground', 'activeforeground', 'anchor', 'background', 'bd', 'bg', 'bitmap', 'borderwidth', 'compound', 'cursor', 'disabledforeground', 'fg', 'font', 'foreground', 'height', 'highlightbackground', 'highlightcolor', 'highlightthickness', 'image', 'justify', 'padx', 'pady', 'relief', 'state', 'takefocus', 'text', 'textvariable', 'underline', 'width', 'wraplength'])
 fred.keys()=dict_keys(['activebackground', 'activeforeground', 'anchor', 'background', 'bd', 'bg', 'bitmap', 'borderwidth', 'command', 'compound', 'cursor', 'default', 'disabledforeground', 'fg', 'font', 'foreground', 'height', 'highlightbackground', 'highlightcolor', 'highlightthickness', 'image', 'justify', 'overrelief', 'padx', 'pady', 'relief', 'repeatdelay', 'repeatinterval', 'state', 'takefocus', 'text', 'textvariable', 'underline', 'width', 'wraplength'])
  command       
  default            
  repeatinterval 
  repeatdelay    
  overrelief         


输出字体颜色参考:Python print()输出颜色设置

输出颜色乱码参考:Python 解决控制台输出颜色 (windows平台)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值