label 标签
用于显示文字和图片
from tkinter import *
root = Tk()
root.title('主窗口')
root.geometry('400x300')
# 创建一个标签
label = Label(root,text = 'hello world', width = 15, height = 2)
label.pack()
root.mainloop()
属性 |
类型 |
默认值 |
说明 |
master |
|
|
父容器 |
text |
|
|
标签文字 |
textvariable |
|
|
可变文本 |
bg |
|
|
背景色 |
fg |
|
|
前景色 |
highlightcolor |
|
|
高亮颜色 |
image |
|
|
图像链接 |
justify |
|
|
对齐方式 |
padx |
|
|
横向边框距离 |
pady |
|
|
纵向边框距离 |
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title('主窗口')
root.geometry('200x100')
def click_button():
'''
:return:
'''
# 弹出窗口
messagebox.showinfo(title='友情提示',message='你点击了按钮')
button = Button(root,text='点击',width = 15, height = 2,command = click_button)
button.pack()
root.mainloop()
text 标签
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.title('主窗口')
root.geometry('200x100')
content = tk.Text(root,height = 3)
content.pack()
def click_button():
'''
当按钮被点击时执行该函数
:return:
'''
text = content.get('0.0','end') # 获取 text 文本消息
msg = '输入内容为:{text}'.format(text = text)
messagebox.showinfo(title='友情提示',message=msg)
button = tk.Button(root,text='发表',width = 15, height = 2, command = click_button)
button.pack()
root.mainloop()
Listbox列表框控件
import tkinter as tk
root = tk.Tk()
root.title('主窗口')
root.geometry('200x100')
list_items = tk.StringVar()
list_items.set(('python', 'C++', 'java', 'php'))
# 创建列表框
lb = tk.Listbox(root, listvariable = list_items)
lb.pack()
no_select = '没有选中任何选项'
def click_button():
'''
当按钮被点击时执行该函数
:return:
'''
select = lb.curselection()
print(select)
if len(select) == 0:
label_text.set(no_select)
else:
text = lb.get(select)
label_text.set('你选择了{text}'.format(text=text))
# 创建 button
button = tk.Button(root, text='显示所选', width = 15, height = 2, command= click_button)
button.pack()
# 创建 label 标签用于显示所选择的列表框选项
label_text = tk.StringVar()
label_text.set(no_select)
label = tk.Label(root,width = 15, height = 2, textvariable = label_text)
label.pack()
root.mainloop()
import tkinter as tk
root = tk.Tk()
root.title('主窗口')
root.geometry('200x150')
var = tk.StringVar()
label = tk.Label(root,bg = 'yellow', width = 20, text = '')
label.pack()
def select():
label.config(text = '你选择了' + var.get())
r1 = tk.Radiobutton(root,text = 'python', variable = var, value = 'python',command = select)
r1.pack()
r2 = tk.Radiobutton(root,text = 'java', variable = var, value = 'java',command = select)
r2.pack()
r3 = tk.Radiobutton(root,text = 'php', variable = var, value = 'php',command = select)
r3.pack()
root.mainloop()
import tkinter as tk
root = tk.Tk()
root.title('主窗口')
root.geometry('200x200')
label = tk.Label(root, bg='yellow', width = 20, text = '')
label.pack()
def select():
select_lst = []
if var1.get() == 1:
select_lst.append('python')
if var2.get() == 1:
select_lst.append('java')
if var3.get() == 1:
select_lst.append('php')
text = '你选择了' + ','.join(select_lst)
label.config(text=text)
var1 = tk.IntVar()
var2 = tk.IntVar()
var3 = tk.IntVar()
c1 = tk.Checkbutton(root, text = 'python', variable = var1, onvalue = 1, offvalue = 0,command = select)
c2 = tk.Checkbutton(root, text = 'java', variable = var2, onvalue = 1, offvalue = 0,command = select)
c3 = tk.Checkbutton(root, text = 'php', variable = var3, onvalue = 1, offvalue = 0,command = select)
c1.pack()
c2.pack()
c3.pack()
root.mainloop()
scale 范围控件
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.title('主窗口')
root.geometry('450x100')
label = tk.Label(root, bg = 'yellow', width = 20, text = '')
label.pack()
def select(value):
label.config(text='你选择的价格是' + value)
scale = tk.Scale(root, label = '选择价格范围', from_ = 50, to = 200, orient = tk.HORIZONTAL, length = 400, tickinterval = 20, resolution = 0.1, command = select)
scale.pack()
root.mainloop()
属性 |
类型 |
默认值 |
说明 |
label |
|
|
提示语 |
from_ |
|
|
最小值 |
to |
|
|
最大值 |
orient |
|
|
显示方向 |
length |
|
|
长度 |
tickinterval |
|
|
刻度间隔 |
resolution |
|
|
精确程度 |
command |
|
|
执行操作 |
Canvas 画布控件
绘制直线
import tkinter as tk
root = tk.Tk()
root.title('主窗口')
root.geometry('300x300')
canvas = tk.Canvas(root, bg = 'yellow', height = 200, width = 300)
canvas.pack()
line = canvas.create_line(0, 0, 300, 200)
root.mainloop()
绘制圆形/矩形
import tkinter as tk
root = tk.Tk()
root.title('主窗口')
root.geometry('300x300')
canvas = tk.Canvas(root, bg = 'yellow', height = 200, width = 300)
canvas.pack()
oval = canvas.create_oval(50, 50, 90, 90, fill = 'red')
rect = canvas.create_rectangle(100, 50, 150, 100)
root.mainloop()
移动圆
import tkinter as tk
root = tk.Tk()
root.title('主窗口')
root.geometry('300x300')
canvas = tk.Canvas(root, bg = 'yellow', height = 200, width = 300)
canvas.pack()
oval = canvas.create_oval(50, 50, 90, 90, fill = 'red')
def moveit():
canvas.move(oval, 50, 50)
button = tk.Button(root, text = '移动圆', command = moveit)
button.pack()
root.mainloop()
展示图片
import tkinter as tk
from PIL import ImageTk
import PIL
root = tk.Tk()
root.title('主窗口')
root.geometry('700x700')
canvas = tk.Canvas(root, bg = 'blue', height = 700, width = 700)
canvas.pack()
image = PIL.Image.open("/Users/henry/Documents/practice/tkinter_learn/album/python.jpeg")
image_file = ImageTk.PhotoImage(image)
img = canvas.create_image(10, 10, anchor = 'nw', image = image_file)
root.mainloop()