Tkinter中Widget配置

通过设置属性来改变Widget的外观

常见的属性包括text(显示的文字) 、color(前景色和背景色)、size(宽度和高度)、 command callbacks(点击后的回调函数)等等

1.设置属性,有3种方式:

1)在创建Widget时,通过关键字参数设定

widgetclass(master, option=value, …) => widget

实例:

# coding=utf-8
from Tkinter import *

def touch():
    print ‘button clicked‘

root = Tk()
btn = Button(root, text="Touch me", fg="red", bg="blue", command=touch)  # 指定显示的文本,前景色,背景色,点击后的回调函数
btn.pack()
root.mainloop()

2) 在对象创建完毕后,通过类似字典的方式,以属性为key,进行访问修改

# coding=utf-8
from Tkinter import *

def touch():
    print ‘button clicked‘

root = Tk()
btn = Button(root, text="Touch me")
btn["fg"] = "red"
btn["bg"] = "blue"
btn["command"] = touch
btn.pack()
root.mainloop()

3)对象创建完毕后,调用config函数

# coding=utf-8
from Tkinter import *

def touch():
    print ‘button clicked‘

root = Tk()
btn = Button(root, text="Touch me")
btn.config(fg="red", bg="blue", command=touch)  #调用config函数,设置属性
btn.pack()
root.mainloop()

2.获取属性以及属性对应的值

获取属性: 调用Widget的keys()方法

同时获取属性以及对应的值: 调用Widget对应的无参config()方法

# coding=utf-8
from Tkinter import *

root = Tk()
btn = Button(root, text="Touch me")
print btn.keys()  # 只获取Button实例属性
print btn.config()  # 获取Button实例的属性和值
btn.pack()
root.mainloop()

输出:

[‘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‘]
{‘highlightthickness‘: (‘highlightthickness‘, ‘highlightThickness‘, ‘HighlightThickness‘, <pixel object at 02325330>, <pixel object at 02325330>), ‘text‘: (‘text‘, ‘text‘, ‘Text‘, ‘‘, ‘Touch me‘), ‘image‘: (‘image‘, ‘image‘, ‘Image‘, ‘‘, ‘‘), ‘compound‘: (‘compound‘, ‘compound‘, ‘Compound‘, <index object at 03045250>, ‘none‘), ‘height‘: (‘height‘, ‘height‘, ‘Height‘, 0, 0), ‘borderwidth‘: (‘borderwidth‘, ‘borderWidth‘, ‘BorderWidth‘, <pixel object at 023253C0>, <pixel object at 023253C0>), ‘pady‘: (‘pady‘, ‘padY‘, ‘Pad‘, <pixel object at 023254F8>, <pixel object at 023254F8>), ‘padx‘: (‘padx‘, ‘padX‘, ‘Pad‘, <pixel object at 023253D8>, <pixel object at 023253D8>), ‘font‘: (‘font‘, ‘font‘, ‘Font‘, <font object at 0230DEB8>, ‘TkDefaultFont‘), ‘activeforeground‘: (‘activeforeground‘, ‘activeForeground‘, ‘Background‘, <color object at 02301EE8>, ‘SystemButtonText‘), ‘activebackground‘: (‘activebackground‘, ‘activeBackground‘, ‘Foreground‘, <border object at 02302398>, ‘SystemButtonFace‘), ‘underline‘: (‘underline‘, ‘underline‘, ‘Underline‘, -1, -1), ‘width‘: (‘width‘, ‘width‘, ‘Width‘, 0, 0), ‘state‘: (‘state‘, ‘state‘, ‘State‘, <index object at 02325378>, ‘normal‘), ‘highlightcolor‘: (‘highlightcolor‘, ‘highlightColor‘, ‘HighlightColor‘, <color object at 02325570>, ‘SystemWindowFrame‘), ‘textvariable‘: (‘textvariable‘, ‘textVariable‘, ‘Variable‘, ‘‘, ‘‘), ‘overrelief‘: (‘overrelief‘, ‘overRelief‘, ‘OverRelief‘, ‘‘, ‘‘), ‘takefocus‘: (‘takefocus‘, ‘takeFocus‘, ‘TakeFocus‘, ‘‘, ‘‘), ‘bd‘: (‘bd‘, ‘-borderwidth‘), ‘foreground‘: (‘foreground‘, ‘foreground‘, ‘Foreground‘, <color object at 03047630>, ‘SystemButtonText‘), ‘bg‘: (‘bg‘, ‘-background‘), ‘repeatinterval‘: (‘repeatinterval‘, ‘repeatInterval‘, ‘RepeatInterval‘, 0, 0), ‘repeatdelay‘: (‘repeatdelay‘, ‘repeatDelay‘, ‘RepeatDelay‘, 0, 0), ‘background‘: (‘background‘, ‘background‘, ‘Background‘, <border object at 022BE118>, ‘SystemButtonFace‘), ‘fg‘: (‘fg‘, ‘-foreground‘), ‘bitmap‘: (‘bitmap‘, ‘bitmap‘, ‘Bitmap‘, ‘‘, ‘‘), ‘highlightbackground‘: (‘highlightbackground‘, ‘highlightBackground‘, ‘HighlightBackground‘, <border object at 02325390>, ‘SystemButtonFace‘), ‘disabledforeground‘: (‘disabledforeground‘, ‘disabledForeground‘, ‘DisabledForeground‘, <color object at 0230DA50>, ‘SystemDisabledText‘), ‘wraplength‘: (‘wraplength‘, ‘wrapLength‘, ‘WrapLength‘, <pixel object at 023252D0>, <pixel object at 023252D0>), ‘default‘: (‘default‘, ‘default‘, ‘Default‘, <index object at 0230DFF0>, ‘disabled‘), ‘cursor‘: (‘cursor‘, ‘cursor‘, ‘Cursor‘, ‘‘, ‘‘), ‘command‘: (‘command‘, ‘command‘, ‘Command‘, ‘‘, ‘‘), ‘relief‘: (‘relief‘, ‘relief‘, ‘Relief‘, <index object at 02325420>, ‘raised‘), ‘anchor‘: (‘anchor‘, ‘anchor‘, ‘Anchor‘, <index object at 02301D68>, ‘center‘), ‘justify‘: (‘justify‘, ‘justify‘, ‘Justify‘, <index object at 02325348>, ‘center‘)}

备注: keys()方法中并没有包含name属性,因为name属性只能在Widget对象创建的时候指定。

因此通过btn[‘name‘] = ‘ok‘ 的形式指定‘name’属性

时间: 2024-08-28 01:54:27

Tkinter中Widget配置的相关文章

Tkinter中Widget名称

If you wish to get the full name of a Tkinter widget, simply use the str function on the widget instance: Tkinter中欲获取Widget名称,只要使用print打印对应Widget实例即可 而在Widget创建时,可以通过设置name属性,指定名称,而name属性只能在对象创建时使用 未设置name属性 from Tkinter import * root = Tk() frame =

JNDI在Tomcat中的配置

使用JNDI获取连接对象 java:comp/env/jdbc/news 这段为固定写法java:comp/env/ 在Tomcat中的配置 配置在:context.xml文件内 F:\apache-tomcat-7.0.68-windows-x64\apache-tomcat-7.0.68\conf\context.xml ---------mySQL<Resource name="jdbc/news"auth="Container" type="

DICOM医学图像处理:DCMTK在VS2012中的配置

背景: 最近由于项目需要,将原本的开发IDE环境由VS2008升级到了VS2012.本以为编译完成后的DCMTK开源库可以直接从VS2008移植到VS2012.但是通过项目属性添加完包含目录和依赖库后,编译会出现大量的链接错误(大多是跟dcmdata.lib.oflog.lib有关). 解决方法: 重新按照原本的博客前辈柳北风儿(大神目前已经博客转移到网易:http://blog.163.com/[email protected]/),利用CMake工具,选择VS2012本地编译器对DCMTK3

web.xml的配置中&lt;context-param&gt;配置作用

<context-param>的作用: web.xml的配置中<context-param>配置作用1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context-param></context-param> 2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文. 3.容器将&l

在CentOS6.4中安装配置LAMP环境的详细步骤

原文:在CentOS6.4中安装配置LAMP环境的详细步骤 本文详细介绍了CentOS6.4系统中安装LAMP服务并对其进行配置的过程,即安装Apache+PHP+Mysql,参照了网上大神的设置,其他Linux发行系统可以参考~ 在本文中部分命令操作需要root权限,输入‘su -’命令后输入密码即可切换root身份. 一.修改设置对安装做准备 1. 防火墙设置 设置/etc/sysconfig/iptables文件允许80端口和3306端口.因为80端口是http协议所使用的端口,如果防火墙

Tomcat插件与Jetty插件在MyEclipse中的配置

-Djetty.port=8101 jetty:run tomcat6:run <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat6-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8888</port>

XCode中Architecturs配置及常见问题

http://lanvige.github.io/2014/03/19/architecturs-in-xcode/ XCode 5.1升级后因arm64和CocoaPods的原因,痛了一天,终于解决了问题,同时也记录下这次的学习成果. ARMv6/7/7s & ARM64 在了解Architecture之前,先来认识这几个名字.armv6, armv7, armv7s, arm64是ARM CPU的不同指令集,就像CPU内潜入的软件版本.其在iPhone处理器型号为A4, A8… arm 芯片

在Win7中IIS配置Asp.Net虚拟目录的方法及错误总结!

在Win7中IIS配置Asp.Net虚拟目录的方法总结! 一.右键[网站],点击[添加虚拟目录]或[虚拟应用程序],笔者建议最好建立虚拟应用程序,因为这就跟一个网站差不多,不用考虑路径问题. 二.直接输入相应内容选择路径就行了,如果要指定[应用程序池],需要先建立一个新的[应用程序池],配置与网站差不多了. 三.运行后,如果出现以下错误:HTTP 错误 500.19 一般是web.config配置问题,很简单,找到以下内容 <system.webServer> <defaultDocum

Maven(一)——如何在Windows操作系统中安装配置Maven环境

今天难得的周末,借此难的机会总结一下关于maven的一些操作: 1.在安装maven之前要确认计算机已经安装并配置了JDK: 2.下载maven: maven-3.0.3:http://download.csdn.net/detail/wangshuxuncom/7367413 maven-3.0.5:http://download.csdn.net/detail/wangshuxuncom/7551799 说明:上述资源均免费下载 这里选择maven-3.0.3来演示安装.将maven-3.0