'''
Created on 2017年8月30日
@author: Nick
'''
#_*_coding:utf-8_*_
import tkinter as tk
from tkinter import *
#定义Button回调函数
def helloButton():
print('hello world!')
def cb1():
print('button1 clicked')
def cb2(event):
print('button2 clicked')
def cb3():
print('button3 clicked')
def printEventInfo(event):
print('event.time:'+ event.time)
print('event.type:'+ event.type)
print('event.Widgetld:'+ event.Widgetld)
print('event.KeySymbol:'+ event.KeySymbol)
def changeText():
if b['text'] == 'text':
v.set('change')
print('change')
else:
v.set('text')
print('text')
if __name__ == '__main__':
root = tk.Tk()
root.wm_title('Hello')#设置窗体标题
root.wm_minsize(400, 400)#设置窗口最小化大小
root.wm_maxsize(2800, 2800)#设置窗口最大化大小
root.resizable(width=False, height=True)#设置窗口宽度不可变,高度可变
'''
root['height'] = 300 #设置高
root['width'] = 500 #设置宽
root.title('魔方小站') #设置标题
root['bg'] = '#0099ff' #设置背景色
root.geometry('500x300') #设置窗口大小 是x不是*
root.geometry("500x300+120+100") #设置窗口大小 并初始化桌面位置
root.resizable(width=False,height=True) #宽不可变 高可变 默认True
root.minsize(300,600) #窗口可调整的最小值
root.maxsize(600,1200) #窗口可调整的最大值
root.attributes("-toolwindow", 1) #工具栏样式
root.attributes("-topmost", -1) #置顶窗口
root.state("zoomed") #窗口最大化
root.iconify() #窗口最小化
root.attributes("-alpha",1) #窗口透明化 透明度从0-1,1是不透明,0是全透明
root.iconbitmap('c:\\logo.ico') #设置左上角图标
'''
#app = App(root)
'''
1、执行的结果:每次点击一次,调用回调函数command,程序向标准输出打印'hello button',以上为Button使用方法
通过设置relief可以看到Button的不同效果,风格,也可以不加回调函数
FLAT, GROOVE, RAISED, RIDGE, SOLID, SUNKEN
relief = FLAT设置,就是一个Label了!!!
'''
#在屏幕上创建一块矩形区域,多作为容器来布局窗体
fram_one = Frame(root)
Button(fram_one,text = 'helloButton',command = helloButton).pack(side=LEFT)
Button(fram_one,text = 'helloButton',command = helloButton,relief = FLAT).pack(side=LEFT)
Button(fram_one,text = 'helloButton',command = helloButton,relief = GROOVE).pack(side=LEFT)
Button(root,text = 'helloButton',command = helloButton,relief = RAISED).pack(side=LEFT)
Button(fram_one,text = 'helloButton',command = helloButton,relief = RIDGE).pack(side=LEFT)
Button(fram_one,text = 'helloButton',command = helloButton,relief = SOLID).pack(side=LEFT)
Button(fram_one,text = 'helloButton',command = helloButton,relief = SUNKEN).pack(side=RIGHT)
fram_one.pack(side=TOP)
"""
2、Button显示图像
image:可以使用gif图像,图像的加载方法img = PhotoImage(root,file = filepath
bitmap:使用X11 格式的bitmap,Windows的Bitmap没法显示的,在Windows下使用GIMP2.4将windows
Bitmap转换为xbm文件,依旧无法使用.linux下的X11 bitmap编辑器生成的bitmap还没有测试,但可
以使用内置的位图。
"""
#3.与Label一样,Button也可以同时显示文本与图像,使用属性compound
#图像居下,居上,居右,居左,文字位于图像之上
fram_two = Frame(root)
Button(fram_two,text = 'botton',compound = 'bottom',bitmap = 'error').pack(side=LEFT)
Button(fram_two,text = 'top',compound = 'top',bitmap = 'error').pack(side=LEFT)
Button(fram_two,text = 'right',compound = 'right',bitmap = 'error').pack(side=LEFT)
Button(fram_two,text = 'left',compound = 'left',bitmap = 'error').pack(side=LEFT)
Button(fram_two,text = 'center',compound = 'center',bitmap = 'error').pack(side=LEFT)
fram_two.pack(side = TOP)
'''
4、上例中使用了bind方法,它建立事件与回调函数(响应函数)之间的关系,每当产生<Enter>事件
后,程序便自动的调用cb2,与cb1,cb3不同的是,它本身还带有一个参数----event,这个参数传递
响应事件的信息。
'''
fram_three = Frame(root)
b1 = Button(fram_three,text = 'Button1',command = cb1)
b2 = Button(fram_three,text = 'Button2')
b2.bind("<Return>",cb2)
b3 = Button(fram_three,text = 'Button3',command = cb3)
b1.pack(side = LEFT)
b2.pack(side = LEFT)
b3.pack(side = LEFT)
b2.focus_set()
b = Button(fram_three,text = 'Infomation')
b.bind("<Return>",printEventInfo)
b.pack(side = LEFT)
b.focus_set()
fram_three.pack(side = TOP)
'''
5.指定Button的宽度与高度
width: 宽度
heigth: 高度
使用三种方式:
1.创建Button对象时,指定宽度与高度
2.使用属性width和height来指定宽度与高度
3.使用configure方法来指定宽度与高度
'''
fram_four = Frame(root)
#方法1:
Button1 = Button(fram_four,text = '按钮1',width = 50,height = 6)
Button1.pack(side = LEFT)
#方法2:
Button2 = Button(fram_four,text = '按钮2')
Button2['width'] = 30
Button2['height'] = 3
Button2.pack(side = LEFT)
#方法 3:
Button3 = Button(fram_four,text = '按钮3')
Button3.configure(width = 10,height = 1)
Button3.pack(side = LEFT)
# 上述的三种方法同样也适合其他的控件
fram_four.pack(side = TOP)
'''
6.设置Button文本在控件上的显示位置
anchor:
使用的值为:n(north-上),s(south-下),w(west-左),e(east-右)和ne-右上,nw-左上,se-右下,sw-左下,就是地图上的标识位置了,使用
width和height属性是为了显示各个属性的不同。
'''
fram_five = Frame(root)
for a in ['n','s','e','w','ne','nw','se','sw']:
Button(fram_five,text = '按钮位置',anchor = a,width = 20,height = 2).pack(side = LEFT)
fram_five.pack(side = TOP)
#也可以单个设置
fram_six = Frame(root)
Button(fram_six,text = 'anchor1',width = 30,height =4).pack(side = LEFT)
Button(fram_six,text = 'anchor2',anchor = 'center',width = 30,height =4).pack(side = LEFT)
Button(fram_six,text = 'anchor3',anchor = 'n',width = 30,height = 4).pack(side = LEFT)
Button(fram_six,text = 'anchor4',anchor = 's',width = 30,height = 4).pack(side = LEFT)
Button(fram_six,text = 'anchor5',anchor = 'e',width = 30,height = 4).pack(side = LEFT)
Button(fram_six,text = 'anchor6',anchor = 'w',width = 30,height = 4).pack(side = BOTTOM)
Button(fram_six,text = 'anchor7',anchor = 'ne',width = 30,height = 4).pack(side = BOTTOM)
Button(fram_six,text = 'anchor8',anchor = 'nw',width = 30,height = 4).pack(side = BOTTOM)
Button(fram_six,text = 'anchor9',anchor = 'se',width = 30,height = 4).pack(side = BOTTOM)
Button(fram_six,text = 'anchor10',anchor = 'sw',width = 30,height = 4).pack(side = BOTTOM)
fram_six.pack(side = TOP)
'''
7.改变Button的前景色与背景色
fg: 前景色
bg:背景色
'''
fram_seven = Frame(root)
Button(fram_seven,text = '前景色',width = 30,height = 5,fg = 'red').pack(side = LEFT)
Button(fram_seven,text = '背景色',width = 30,height = 5,fg = 'green').pack(side = RIGHT)
fram_seven.pack(side = TOP)
'''
8.设置Button的边框
bd(bordwidth):缺省为1或2个像素
'''
# 创建5个Button边框宽度依次为:0,2,4,6,8,10
fram_eight = Frame(root)
for i in [0,2,4,6,8,10]:
Button(fram_eight,text = '边框'+ str(i),command = helloButton,bd = i).pack(side = LEFT)
fram_eight.pack(side = TOP)
'''
9.设置Button的风格
relief = flat/groove/raised/ridge/solid/sunken
'''
fram_nine = Frame(root)
for r in ['flat', 'groove', 'raised', 'ridge', 'solid', 'sunken']:
Button(fram_nine,text = '风格'+ r,command = helloButton,relief = r).pack(side = LEFT)
fram_nine.pack(side = TOP)
'''
10.设置Button状态
state = normal/active/disabled
'''
fram_ten = Frame(root)
for s in ['normal','active','disabled']:
Button(fram_ten,text = '状态' + s,command = helloButton,state = s).pack(side =LEFT)
fram_ten.pack(side = TOP)
'''
11.绑定Button与变量
设置Button在textvariable属性
'''
fram_elev = Frame(root)
v = StringVar()
v.set('change')
Button(fram_elev,textvariable = v,command = changeText).pack(side = LEFT)
fram_elev.pack(side =TOP)
root.mainloop()
Tkinker之Button篇
本文转载:CSDN博客