tkinter之 Label & Button 标签和按钮

Posted jyfootprint

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tkinter之 Label & Button 标签和按钮相关的知识,希望对你有一定的参考价值。

‘‘‘
# ------------------------------------------------------------
# # 1、 使用tkinter设置一个标签
# # # 设计tkinter内容:   title;geometry;Label;mainloop
# ------------------------------------------------------------
‘‘‘ 

  代码如下:

import tkinter as tk

window = tk.Tk()                        # 实例化
window.title("my window")               # 设置标题名称,设置输出显示的窗口显示的名称
window.geometry("200x100")              # 长200 x 高100; 单位是字符串的长度,即200个字符串长度


‘‘‘
tk.Label:  设置标签
l = tk.Label(window, text= "OMG! this is TK!", bg = ‘green‘, font = (‘Arial‘, 12),             width = 15, height = 2  )
window :   实例
text:     标签显示的内容
bg:       标签的背景颜色
font:     标签Label中的text文本的字体的显示格式。
font = (‘Arial‘, 12)  : 字体格式,Arial;     大小,12
width = 15:  标签Label 的宽度是  15个字符的大小
 height = 2   标签Label 的高度是  2 个字符的大小
‘‘‘
l = tk.Label(window, text= "OMG! this is TK!", bg = ‘green‘, font = (‘Arial‘, 12),             width = 20, height = 2  )

# 把标签放在窗口上
l.pack()

# 循环运行window实例
window.mainloop()

  

运行结果如下:

?技术分享图片技术分享图片??

 

 

 

 

 

‘‘‘
# ------------------------------------------------------------
# # 2、 使用tkinter设置一个标签
# # # 设计tkinter内容:   title;geometry;Label;mainloop
# ------------------------------------------------------------
‘‘‘

代码如下:

 

import tkinter as tk

window = tk.Tk()                        # 实例化
window.title("my window")               # 设置标题名称,设置输出显示的窗口显示的名称
window.geometry("200x100")              # 长200 x 高100; 单位是字符串的长度,即200个字符串长度


var = tk.StringVar()

‘‘‘
tk.Label:  设置标签
l = tk.Label(window, text= "OMG! this is TK!", bg = ‘green‘, font = (‘Arial‘, 12),             width = 15, height = 2  )
window :   实例
text:     标签显示的内容
bg:       标签的背景颜色
font:     标签Label中的text文本的字体的显示格式。
font = (‘Arial‘, 12)  : 字体格式,Arial;     大小,12
width = 15:  标签Label 的宽度是  15个字符的大小
height = 2   标签Label 的高度是  2 个字符的大小
‘‘‘
l = tk.Label(window, textvariable=var, bg=‘green‘, font=(‘Arial‘, 12),             width=20, height=2)

# 把标签放在窗口上
l.pack()
on_hit = False

def hit_me():
    global on_hit
    if on_hit == False:
        on_hit = True
        var.set(‘You hit me‘)
        
    else:
        on_hit = False
        var.set("")

b = tk.Button(window, text=‘hit_me‘, width=15,height=2,command=hit_me)

b.pack()
# 循环运行window实例
window.mainloop()
# ------------------------------------------------分割线-------------------------------------------------

  

效果如下: 

状态1,开始运行的时候

技术分享图片技术分享图片?

 

状态2,点击按钮的时候:

技术分享图片技术分享图片?

 

之后的运行循环1,2

 

参考资料:

用 python 和 tkinter 做简单的窗口视窗 - 网易云课堂
http://study.163.com/course/courseMain.htm?courseId=1003216011

 




以上是关于tkinter之 Label & Button 标签和按钮的主要内容,如果未能解决你的问题,请参考以下文章

Tkinter 之Label标签

Python3 Tkinter 之 Label

python之tkinter使用举例-Button

python之tkinter使用-滚动条

tkinter之button

tkinter使用之简单登陆注册界面