Python tkinter学习笔记(1)-Label
admin
2023-07-30 20:50:29
0

Python是最好的语言

  • Python 版本:3.5
  • 系统:OS X 10.11.4
  • IDE:PyCharm
  • 参考:http://blog.chinaunix.net

刚接触Python的GUI(其实Python也是刚接触的)。原本是想研究kivy,但是尝试了一上午,失败了,因为kivy需要Pygame,Pygame又需要XQuartz,前几天刚把它卸载了, 今天安装时卡死在验证程序。NND。
不过没关系,学习框架只是为了有趣的学习语言,况且tkinter是原生的。其他伟大的GUI框架,比如wxPython却不支持3.0以上。

开始了

from tkinter import *

# 初始化tk
root = Tk()
# 设置窗口的title
root.title(\'Hello\')

# 指定master、标题
label = Label(root, text=\'Hello\')
# 显示label
label.pack()

# 指定内置位图
# 其他位图:error hourglass info questhead question warning gray12 gray25 gray50 gray75
Label(root, bitmap=\'error\').pack()

# 使用颜色,前景色、背景色(可写颜色名称(red,green,blue,yello,lightbule)与颜色值)
Label(root, fg=\'lightblue\', bg=\'#FF00FF\', text=\'Hello\').pack()

# 指定宽高。如果没有指定,以文字的长度决定
# 宽高的单位到底是什么呢?
# 高度是行高
Label(root, bg=\'red\', width=10, height=3).pack()

\'\'\'
使用图像与文本
compound:指定文本(text)与图像(bitmap/image)是如何在 Label 上显示,缺省为 None,当指定 image/bitmap 时,文本(text)将被覆盖,只显示图像了。可以使用的值:
    left: 图像居左
    right: 图像居右
    top: 图像居上
    bottom:图像居下
    center:文字覆盖在图像上
bitmap/image:显示在 Label 上的图像
text: 显示在 Label 上的文本
\'\'\'
Label(root, text=\'Error\', compound=\'left\', bitmap=\'error\').pack()

\'\'\'
多行显示与自动对齐:自动换行功能
使用自动换行功能,及当文 本长度大于控件的宽度时,文本应该换到下一行显示,Tk 不会自动处理,但提供了属性:
wraplength: 指定多少单位后开始显示文字。也就是指定空白的宽度。单行不起作用。
justify: 指定多行的对齐方式。单行不起作用。
ahchor: 指定文本(text)或图像(bitmap/image)在 Label 中的显示位置
可用的值:
e/w/n/s/ne/se/sw/sn/center
布局如下图
nw n ne
w center e
sw s se
\'\'\'
# 先来一段Python之禅
ZenOfPython = \'\'\'
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren\'t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you\'re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it\'s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let\'s do more of those!
\'\'\'
# 左对齐,文本居中,不指定宽高就伸展到最长一行的宽度
Label(root, text=ZenOfPython, bg=\'yellow\', justify=\'center\').pack()

# 进入消息循环。其实就是显示
root.mainloop()

结果:


Labels

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
python清除字符串里非数字... 本文实例讲述了python清除字符串里非数字字符的方法。分享给大家供大家参考。具体如下: impor...