[Tkinter 教程01] 入门: Label 控件

来源:互联网 发布:mac磁盘工具抹掉失败 编辑:程序博客网 时间:2024/05/22 03:51

原系列地址: Python Tkinter



Hello Tkinter Label


下面我们将以 Tkinter 中最简单的控件: Label 控件, 开始这个系列的教程. 在 Tkinter 中, Label 控件用以显示文字和图片. Label 通常被用来展示信息, 而非与用户交互. (译者注: Label 也可以绑定点击等事件, 只是通常不这么用).

程序员的教程, 怎么能少了 Hello World . 我们尊重这个传统, 但我们不说 Hello World, 让我们来秀出 Hello Tkinter 吧.

下面的 Python 脚本用 Tkinter 创建了一个带有 "Hello Tkinter" 字样的窗口. 你可以在 Python 的命令行解释器中逐行输入下面的脚本, 也可以将之存入一份文件, 比如 hello.py .

from Tkinter import *# if you are working under Python 3, comment the previous line and comment out the following line#from tkinter import *root = Tk()w = Label(root, text="Hello Tkinter!")w.pack()root.mainloop()


运行例子


如果你将上述脚本存入文件 hello.py, 那么可以这样启动它:

$ python hello.py

如果你在 Linux Gnome 环境下运行上述命令, 会显示如下窗口:


在 Windows 系统下是这个样子:


逐行解释


在 Tkinter 编程中, 一定少不了 Tkinter 中的 Tk 类. 上例中我们通过星号 ( * ) 来将 Tkinter 中的所有模块引入命名空间.

from Tkinter import *

要初始化一个 Tkinter 程序, 我们需要一个 root 控件, 即根窗口, 它包括标题栏和其他一些由本地窗口系统提供的装饰. root 控件需要在创建其他控件前创建, 并且一个窗口只能有一个 root 控件.

root = Tk()

这之后的一行代码, 我们创建了一个 Label 控件. 它的第一个参数是其父控件, 在我们这个例子里就是上面的 root 控件. 因此这个 Label 控件是上面的 root 控件的一个子控件. 这个 Label 控件的第二个参数指示其所要显示的文字.

w = Label(root, text="Hello Tkinter!")

pack() 方法指示这个 Label 的大小为正好可以包裹其中的文字.

w.pack()

当我们启动了 Tkinter 的消息循环 (event loop) 后, 窗口就会被显示出来:

root.mainloop()

上面的脚本会一直运行在这个消息队列中, 直到这个窗口被关闭.


在 Label 中显示图片


上面我们已经提到, Label 既可以显示文字, 也可以显示图片. 下面的例子中我们将创建两个 Label, 一个用以显示文字, 一个用以显示图片:

from Tkinter import *root = Tk()logo = PhotoImage(file="../images/python_logo_small.gif")w1 = Label(root, image=logo).pack(side="right")explanation = """At present, only GIF and PPM/PGMformats are supported, but an interface exists to allow additional image fileformats to be added easily."""w2 = Label(root,            justify=LEFT,           padx = 10,            text=explanation).pack(side="left")root.mainloop()


上面的脚本运行后, 在 Ubuntu 下显示如下:


"justify" 参数指示文字的对齐方向, 可选值为 RIGHT, CENTER, LEFT, 默认为 Center.

"padx" 参数指定水平方向的边距, 默认为1像素.

"pady" 参数指定竖直方向的边距, 默认为1像素.

上面的例子中, 如果去掉 justify 和 padx 参数, 那么上面的窗口会显示为这个样子:


想让文字显示在图片上面? 好办! 我们只需要在一个 Label 控件中同时使用图片和文字的相关选项即可. 默认情况下, 如果为一个 Label 控件指定了图片, 那么这个 Label

就会只显示图片. 要让图片和文字一同显示, 就要使用 compound 选项. 设置 compound 为 CENTER 将使文字显示在图片上方:

from Tkinter import *root = Tk()logo = PhotoImage(file="../images/python_logo_small.gif")explanation = """At present, only GIF and PPM/PGMformats are supported, but an interface exists to allow additional image fileformats to be added easily."""w = Label(root,           compound = CENTER,          text=explanation,           image=logo).pack(side="right")root.mainloop()


我们可以让图片显示在左侧, 文字显示在右侧, 让文字向左对齐, 并在左右两侧空出 10 像素的边距:

w = Label(root,           justify=LEFT,          compound = LEFT,          padx = 10,           text=explanation,           image=logo).pack(side="right")

设置 compound 为 BOTTOM, LEFT, RIGHT, TOP, 图片就会显示在相应的位置上.

颜色和字体


诸如 Label, Text, Canvas 等控件, 支持指定字体, 通过 "font" 属性设置即可实现. 需要特别注意的是字体不是平台独立的.

"fg" 属性可以指定字体的颜色, "bg" 属性可以指定控件的背景颜色.

from Tkinter import *root = Tk()Label(root,  text="Red Text in Times Font", fg = "red", font = "Times").pack()Label(root,  text="Green Text in Helvetica Font", fg = "light green", bg = "dark green", font = "Helvetica 16 bold italic").pack()Label(root,  text="Blue Text in Verdana bold", fg = "blue", bg = "yellow", font = "Verdana 10 bold").pack()root.mainloop()


上例脚本运行后显示如下:


改变控件内容


在下面的例子中, Label 中的文字将被自动加1, 直到按钮被点击时停止计数:

import Tkinter as tkcounter = 0 def counter_label(label):  def count():    global counter    counter += 1    label.config(text=str(counter))    label.after(1000, count)  count()  root = tk.Tk()root.title("Counting Seconds")label = tk.Label(root, fg="green")label.pack()counter_label(label)button = tk.Button(root, text='Stop', width=25, command=root.destroy)button.pack()root.mainloop()

上面的脚本运行后窗口显示如下:




译者水平有限, 如有疏漏, 欢迎指正.

已联系原作者授权. 原文地址: Saying Hello with Labels



0 0
原创粉丝点击