Tkinter极简实例——Lable篇/Button篇

Tkinter Label

Lable的极简例子

from tkinter import *

root = Tk()

label = Label(root, text='timing')
label.pack()  # 显示Lable必需

root.mainloop()  # 显示主界面必需

Lable使用内置位图

from tkinter import*

root = Tk()

label = Label(root, text='timing')
label.pack()

for bitmap in ['error', 'hourglass', 'info', 'questhead', 'question', 'warning', 'gray12', 'gray25', 'gray50', 'gray75']:
    label = Label(root, bitmap=bitmap)
    label.pack()

root.mainloop()

Label的前景色和后景色

from tkinter import *

root = Tk()

label = Label(root, text='timing', fg='red', bg='gray')
label.pack()

root.mainloop()

Label的高度和宽度

from tkinter import *

root = Tk()

for color in ['red', 'blue', 'yellow']:
    label = Label(root, text=color, bg=color)
    label.pack()

for color in ['red', 'blue', 'yellow']:
    label = Label(root, text=color, bg=color, width=10, height=3)
    label.pack()

root.mainloop()

Label使用图像和文本

compound指定文本与图像是如何在文本上显示的,缺省为None,当指定图像时,文本将被覆盖,只显示图像。
left:图像居左
right:图像居右
top:图像居上
bottom:图像居下
center:文字覆盖在图像上

from tkinter import *

root = Tk()

for location in ['left', 'right', 'top', 'bottom', 'center']:
    label = Label(root, text=location, bitmap='error', compound=location)
    label.pack()

root.mainloop()

Label文本的多行显示

from tkinter import *

root = Tk()

for justify in ['left', 'right']:
    for anchor in ['e', 'w', 'n', 's', 'ne', 'se', 'sw', 'se', 'center']:
        label = Label(root, text='test for the label justify and anchor',
                      bg='yellow',
                      width=40,
                      height=3,
                      wraplength=80,  # 每行最大的字符数
                      justify=justify,  # 文本对齐方式
                      anchor=anchor)  # 指定文本在Label中的显示位置
        label.pack()

root.mainloop()

Tkinter Button

Button极简例子

from tkinter import *


def hello():
    print('hello button')


root = Tk()

Button(root, text='click button', command=hello).pack()

root.mainloop()

Button的外观

from tkinter import *


def hello():
    print('hello button')


root = Tk()

for flat in [FLAT, GROOVE, RAISED, RIDGE, SOLID, SUNKEN]:
    Button(root, text='%s button' % str(flat).lower(), relief=flat, command=hello).pack()

root.mainloop()

Button的图像显示

可使用位图或gif图

from tkinter import *


def hello():
    print('hello button')


root = Tk()

Button(root, bitmap='error', command=hello).pack()

root.mainloop()

Button显示文本和图像

与Label类似

from tkinter import *


def hello():
    print('hello button')


root = Tk()

for location in ['left', 'right', 'top', 'bottom', 'center']:
    Button(root, text=location, bitmap='error', compound=location, command=hello).pack()

root.mainloop()

Button的焦点

from tkinter import *


def cb1():
    print('button1 clicked')


def cb2(event):
    print('button2 clicked')


def cb3():
    print('button3 clicked')


root = Tk()
b1 = Button(root, text='Button1', command=cb1)
b2 = Button(root, text='Button2')
b2.bind("<Return>", cb2)
b3 = Button(root, text='Button3', command=cb3)
b1.pack()
b2.pack()
b3.pack()
b2.focus_set()  # 焦点在button2上,按回车直接调用cb2
root.mainloop()

Button的宽度、高度、文本位置、前景色、背景色与Label方法相同

Button的边框

from tkinter import *


def hello():
    print('hello button')


root = Tk()

for b in [0, 2, 4, 6, 7, 9]:
    Button(root, text=str(b), bd=b, command=hello).pack()


root.mainloop()

Button的外观效果与Lable相同

Button的状态

from tkinter import *


def hello():
    print('hello button')


root = Tk()

for state in ['normal', 'active', 'disabled']:
    Button(root, text=state,
           state=state,
           command=hello).pack()


root.mainloop()

Button与变量

from tkinter import *


def change():
    v.set('changed')


root = Tk()

v = StringVar()
button = Button(root, textvariable=v, command=change)
v.set('click here to change')  # 设置初始显示文本
button.pack()

root.mainloop()

二十弱冠,三十而立,四十不惑,五十而知天命。自勉

欢迎留言,你觉得学了python能干什么(并非指能从事什么行业)?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ALittleHigh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值