Tkinter教程之Button篇(2)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811300
# Tkinter教程之Button篇(2)
'''5.指定Button的宽度与高度
width: 宽度
heigth: 高度
使用三种方式:
1.创建Button对象时,指定宽度与高度
2.使用属性width和height来指定宽度与高度
3.使用configure方法来指定宽度与高度
'''
from Tkinter import *
root = Tk()
b1 = Button(root,text = '30X1',width = 30,height = 2)
b1.pack()
b2 = Button(root,text = '30X2')
b2['width'] = 30
b2['height'] = 3
b2.pack()
b3 = Button(root,text = '30X3')
b3.configure(width = 30,height = 3)
b3.pack()
root.mainloop()
# 上述的三种方法同样也适合其他的控件
'''6.设置Button文本在控件上的显示位置
anchor:
使用的值为:n(north),s(south),w(west),e(east)和ne,nw,se,sw,就是地图上的标识位置了,使用
width和height属性是为了显示各个属性的不同。
'''
from Tkinter import *
root = Tk()
#简单就是美!
for a in ['n','s','e','w','ne','nw','se','sw']:
Button(root,
text = 'anchor',
anchor = a,
width = 30,
height = 4).pack()
#如果看的不习惯,就使用下面的代码。
# Button(root,text = 'anchor',width = 30,height =4).pack()
# Button(root,text = 'anchor',anchor = 'center',width = 30,height =4).pack()
# Button(root,text = 'anchor',anchor = 'n',width = 30,height = 4).pack()
# Button(root,text = 'anchor',anchor = 's',width = 30,height = 4).pack()
# Button(root,text = 'anchor',anchor = 'e',width = 30,height = 4).pack()
# Button(root,text = 'anchor',anchor = 'w',width = 30,height = 4).pack()
# Button(root,text = 'anchor',anchor = 'ne',width = 30,height = 4).pack()
# Button(root,text = 'anchor',anchor = 'nw',width = 30,height = 4).pack()
# Button(root,text = 'anchor',anchor = 'se',width = 30,height = 4).pack()
# Button(root,text = 'anchor',anchor = 'sw',width = 30,height = 4).pack()
root.mainloop()
'''7.改变Button的前景色与背景色
fg: 前景色
bg:背景色
'''
from Tkinter import *
root = Tk()
bfg = Button(root,text = 'change foreground',fg = 'red')
bfg.pack()
bbg = Button(root,text = 'change backgroud',bg = 'blue')
bbg.pack()
root.mainloop()
'''8.设置Button的边框
bd(bordwidth):缺省为1或2个像素
'''
# 创建5个Button边框宽度依次为:0,2,4,6,8
from Tkinter import *
root = Tk()
for b in [0,1,2,3,4]:
Button(root,
text = string(b),
bd = b).pack()
root.mainloop()
'''9.设置Button的风格
relief/raised/sunken/groove/ridge
'''
from Tkinter import *
root = Tk()
for r in ['raised','sunken','groove','ridge']:
Button(root,
text = r,
relief = r,
width = 30).pack()
root.mainloop()
'''10.设置Button状态
normal/active/disabled
'''
from Tkinter import *
root = Tk()
def statePrint():
print 'state'
for r in ['normal','active','disabled']:
Button(root,
text = r,
state = r,
width = 30,
command = statePrint).pack()
root.mainloop()
#例子中将三个Button在回调函数都设置为statePrint,运行程序只有normal和active激活了回调函数,而disable按钮则没有,对于暂时不
#需要按钮起作用时,可以将它的state设置为disabled属性
'''11.绑定Button与变量
设置Button在textvariable属性
'''
from Tkinter import *
root = Tk()
def changeText():
if b['text'] == 'text':
v.set('change')
print 'change'
else:
v.set('text')
print 'text'
v = StringVar()
b = Button(root,textvariable = v,command = changeText)
v.set('text')
b.pack()
root.mainloop()
'''
将变量v与Button绑定,当v值变化时,Button显示的文本也随之变化
'''
Tkinter教程之Button篇(2)的更多相关文章
- Tkinter教程之Button篇(1)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811298 #Tkinter教程之Button篇(1)#Button功能触发事件'''1.一个 ...
- Tkinter教程之Event篇(3)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1823550 '''Tkinter教程之Event篇(3)''''''11.两个事件同时绑定到一 ...
- Tkinter教程之Event篇(2)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1823548 '''Tkinter教程之Event篇(2)''''''5.测试离开(Leave) ...
- Tkinter教程之Event篇(1)'
本文转载自:http://blog.csdn.net/jcodeer/article/details/1823544 ''Tkinter教程之Event篇(1)'''# 事件的使用方法'''1.测试鼠 ...
- Tkinter教程之Grid篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1813196 '''Tkinter教程之Grid篇'''# Tkinter参考中最推荐使用的一个 ...
- Tkinter教程之Canvas篇(4)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1812091 '''Tkinter教程之Canvas篇(4)''''''22.绘制弧形'''# ...
- Tkinter教程之Text篇(3)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811348 '''Tkinter教程之Text篇(3)''''''14.自定义tag的两个内置 ...
- Tkinter教程之Toplevel篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811341 '''Tkinter教程之Toplevel篇'''#TopLevel与Frame类 ...
- Tkinter教程之Radiobutton篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811308 #Tkinter教程之Radiobutton篇#Radiobutton为单选按钮, ...
随机推荐
- JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)
String对象(*) length属性:获取字符串的字符个数.(无论中文字符还是英文字符都算1个字符.) charAt(index)方法:获取指定索引位置的字符.(索引从0开始) indexOf(‘ ...
- Servlet编写登录界面
package com.mhb; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.Servlet ...
- .NET 框架 (转载)
转载:http://www.tracefact.net/CLR-and-Framework/DotNet-Framework.aspx .NET框架 三年前写的<.NET之美>的第六章,现 ...
- 创建支持复杂脚本Complex Scripts的WINCE6.0系统
如果要创建支持复杂脚本(Complex Scripts)的系统,我们需要完成下面一系列步骤来确保系统包含所有需要支持的具体区域设置 (locale–specific). 1. 选择intern ...
- bzoj2800
这题好难,翻了一下波兰文的题解……这好像是当年唯一没人A的题目 首先区间修改不难想到差分,我们令d1=x1,dn+1=-xn,di=xi-xi-1 注意Σdi=0,这样对于[l,r]的修改(比如+a) ...
- return File
public ActionResult DownloadMessage() { string strExportData = "无数据!"; byte[] data = Syste ...
- 面向函数范式编程(Functional programming)
函数编程(简称FP)不只代指Haskell Scala等之类的语言,还表示一种编程思维,软件思考方式,也称面向函数编程. 编程的本质是组合,组合的本质是范畴Category,而范畴是函数的组合. 首先 ...
- Android中Java反射技术的使用示例
import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Metho ...
- dynamic_cast,const_cast,static_cast,reinterpret_cast 详解
如果直接指针直接强转,将只能访问虚函数的内容,而不能访问特定类中的特定成员或方法!!!! 强制类型转换运算符:C++有四种强制类型转换符,分别是dynamic_cast,const_cast,stat ...
- Java [Leetcode 122]Best Time to Buy and Sell Stock II
题目描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...