python之GUI(tkinter、ttk)编程学习笔记(自用)

Tkinter是python的默认GUI库

1.Button 按钮:类似标签,但提供额外的功能

# 鼠标左键
# 鼠标右键
# 鼠标中键
# 鼠标中建双击
# 鼠标左键三击

2.Label 标签:用来显示文字或图片,有表格布局、相对布局、绝对布局
(1)绝对布局

import tkinter
win = tkinter.Tk()
win.title("sunk")
win.geometry("400x400+200+20")
label1 = tkinter.Label(win, text="good", bg="pink")
label2 = tkinter.Label(win, text="nice", bg="blue")
label3 = tkinter.Label(win, text="cool", bg="red")
# 绝对布局,窗口的变化位置没有影响
label1.place(x=10, y=10)
label2.place(x=50, y=50)
label3.place(x=100, y=100)
win.mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第1张图片
(2)相对布局

import tkinter
win = tkinter.Tk()
win.title("sunk")
win.geometry("400x400+200+20")
label1 = tkinter.Label(win, text="good", bg="pink")
label2 = tkinter.Label(win, text="nice", bg="blue")
label3 = tkinter.Label(win, text="cool", bg="red")
# 相对布局,窗体改变对控件有影响
# tkinter.BOTH与fill=tkinter.Y类似
label1.pack(fill=tkinter.Y, side=tkinter.LEFT)
label2.pack(fill=tkinter.X, side=tkinter.TOP)
label3.pack()
win.mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第2张图片
(3)表格布局

import tkinter

win = tkinter.Tk()
win.title("sunk")
win.geometry("400x400+200+20")
label1 = tkinter.Label(win, text="good", bg="pink")
label2 = tkinter.Label(win, text="nice", bg="blue")
label3 = tkinter.Label(win, text="cool", bg="red")
label4 = tkinter.Label(win, text="head", bg="yellow")

label1.grid(row=1, column=1)
label2.grid(row=1, column=0)
label3.grid(row=0, column=1)
label4.grid(row=0, column=0)

win.mainloop()

在这里插入图片描述
3.Scale 进度条:线性“滑块”组件,可设定起始值和结束值,会显示当前位置的精确值

#Label Scale Button综合
from tkinter import *
def resize(event=None):
    label.config(font="Helvetica -%d bold" % \
                      scale.get())
top = Tk()
top.geometry("250x150")
label = Label(top, text="Hello world", font="Helvetica -12 bold")
label.pack(fill=Y, expand=1)
scale = Scale(top, from_="10", to="40", orient=HORIZONTAL,
              command=resize)
scale.set(12)
scale.pack(fill=X, expand=1)
quit = Button(top, text="QUIT",
              command=top.quit,
              activeforeground="white",
              activebackground="red")
quit.pack()
mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第3张图片
python之GUI(tkinter、ttk)编程学习笔记(自用)_第4张图片
4.Canvas 画布:提供绘图功能,可以包含图形或位图
5.Checkbutton:一组方框,可以选择其中的任意个

#复选框
import tkinter
win = tkinter.Tk()
win.title("sunk")
win.geometry("400x400+200+20")
def updata():
    message = ""
    if hobby1.get() == True:
        message += "money\n"
    if hobby2.get() == True:
        message += "power\n"
    if hobby3.get() == True:
        message += "mpeople\n"
        # 清除text的所有内容
	text.delete(0.0, tkinter.END)
	text.insert(tkinter.INSERT, message)
hobby1 = tkinter.BooleanVar()
check1 = tkinter.Checkbutton(win, text="money", variable=hobby1, command=updata)
check1.pack()
hobby2 = tkinter.BooleanVar()
check2 = tkinter.Checkbutton(win, text="power", variable=hobby2, command=updata)
check2.pack()
hobby3 = tkinter.BooleanVar()
check3 = tkinter.Checkbutton(win, text="people", variable=hobby3, command=updata)
check3.pack()
text = tkinter.Text(win, width=50, height=5)
text.pack()
win.mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第5张图片
6.Entry 文本框:单行文字域,用来收集键盘输入
7.Frame:框架框架控件,在屏幕上显示一个矩形区域,多作为容器控件

import tkinter

win = tkinter.Tk()
win.title("sunk")
win.geometry("400x400+200+20")

frm = tkinter.Frame(win)
frm.pack()
# left
frm_1 = tkinter.Frame(frm)

tkinter.Label(frm_1, text="左上", bg="pink").pack(
    side=tkinter.TOP)
tkinter.Label(frm_1, text="左下", bg="blue").pack(
    side=tkinter.TOP)
frm_1.pack(side=tkinter.LEFT)

# right
frm_r = tkinter.Frame(frm)

tkinter.Label(frm_r, text="右上", bg="red").pack(
    side=tkinter.TOP)
tkinter.Label(frm_r, text="右下", bg="yellow").pack(
    side=tkinter.TOP)
frm_r.pack(side=tkinter.RIGHT)

win.mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第6张图片

8.Listbox 列表框:一个选项列表,用户可以从中选择
作用:在listbox控件的小窗口显示一个字符串

import tkinter
win = tkinter.Tk()
win.title("sunk")
win.geometry("400x400+200+20")
# 创建一个listbox,添加几个元素
# SINGLE与BORWSE类似,但是不支持鼠标移动选中位置
lb = tkinter.Listbox(win, selectmode=tkinter.BROWSE)
lb.pack()
for item in ["good", "nice", "handsome", "cool"]:
    # 按顺序添加
    lb.insert(tkinter.END, item)
# 在开始添加
# 将列表当成元素添加
# 删除  参数1为开始的索引,参数2为结束的索引,
# 如果不指定参数2,只删除第一个索引的内容
lb.insert(tkinter.ACTIVE, "cool")
lb.insert(tkinter.END, ["very good ", "very nice"])
# lb.delete(1, 3)
lb.select_set(2, 5)
# 取消
# lb.select_clear(2, 4)
# 获取到列表中的元素的个数
# print(lb.size())
# 从列表中取值
# print(lb.get(2, 4))
# 返回当前的索引项,不是item元素
print(lb.curselection())
# 判断 一个选项是否被选中
print(lb.select_includes(1))
print(lb.select_includes(3))
win.mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第7张图片

import tkinter
win = tkinter.Tk()
win.title("sunk")
# win.geometry("400x400+200+20")
# EXTENDED 可以使listbox支持shift和ctrl
# MULTIPLE支持多选
lb = tkinter.Listbox(win, selectmode=tkinter.EXTENDED)
# lb.pack()
for item in ["good", "nice", "handsome", "vg", "vn",
             "good2", "nice3", "handsome4", "vg5", "vn4",
            "good3", "nice4", "handsome44", "vg5", "vn6"
             ]:
    lb.insert(tkinter.END, item)
# 按住shift,可以实现连选
# 按住ctrl,可以实现多选
# 加滚动条
sc = tkinter.Scrollbar(win)
sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)
lb.configure(yscrollcommand=sc.set)
lb.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
sc['command'] = lb.yview()
win.mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第8张图片
9.Menu 菜单:点下菜单按钮后弹出的一个选项列表,用户可以从中选择
(1)菜单栏

import tkinter

win = tkinter.Tk()
win.title("sunk")
win.geometry("400x400+200+20")


def func():
    print("sunk is a good man ")


# 菜单条
menubar = tkinter.Menu(win)
win.config(menu=menubar)
# 创建一个菜单选项
menu1 = tkinter.Menu(menubar, tearoff=False)
# 给菜单选项添加内容
for item in ["Python", "c", "c++", "OC",
             "SWITFG", "JAVA", "PHP", "JS"]:
    if item == "退出":
        # 添加分割线

        menu1.add_separator()
        menu1.add_command(label=item, command=win.quit)
    else:
        menu1.add_command(label=item, command=func)

# 添加菜单选项
menubar.add_cascade(label="语言", menu=menu1)
menu2 = tkinter.Menu(menubar, tearoff=False)
menu2.add_command(label="red")
menu2.add_command(label="blue")
menubar.add_cascade(label="颜色", menu=menu2)

win.mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第9张图片
(2)鼠标右键菜单

import tkinter

win = tkinter.Tk()
win.title("sunk")
win.geometry("400x400+200+20")

# 菜单条
menubar = tkinter.Menu(win)
# 菜单
# 创建一个菜单选项
menu = tkinter.Menu(menubar, tearoff=False)
# 给菜单选项添加内容
for item in ["Python", "c", "c++", "OC",
             "SWITFG", "JAVA", "PHP", "JS"]:
    menu.add_command(label=item)

menubar.add_cascade(label="语言", menu=menu)


def showMenu(event):
    menubar.post(event.x_root, event.y_root)


win.bind("", showMenu)

win.mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第10张图片
10.Menubutton:菜单按钮。用来包含菜单的组件
11.Message 消息框。类似于标签,但可以显示多行文本
12.Radiobutton 单选按钮:一组按钮,其中只有一个可被“按下”

"""
单选框
"""
import tkinter


def updata():
    print(r.get())


win = tkinter.Tk()
win.title("sunk")
win.geometry("400x400+200+20")

#一组单选框绑定一个变量
r = tkinter.IntVar()
radio1 = tkinter.Radiobutton(win, text="one ", value=1,
                             variable=r, command=updata)
radio1.pack()
radio2 = tkinter.Radiobutton(win, text="2222 ", value=2,
                             variable=r, command=updata)
radio2.pack()

win.mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第11张图片
13.Scrollbar 滚动条。对其支持的组件包括文本域、画布、列表框、文本框,提供滚动功能
14.ttk的很多组件同Tkinter都是相同的,在这种情况下,ttk将覆盖Tkinter的组件,将采用ttk的特性
(1)下拉数据

import tkinter
from tkinter import ttk

win = tkinter.Tk()
win.title("sunk")
win.geometry("400x400+200+20")
cv = tkinter.StringVar()
com = ttk.Combobox(win, )
com.pack()
# 设置下拉数据
com["value"] = ("黑龙江", "吉林", "辽宁")
com.current(0)


# 绑定事件
def func(event):
    print("sunck is a good man ")
    print(com.get())
    print(cv.get())


com.bind("<>", func)
win.mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第12张图片
(2)表格数据

import tkinter
from tkinter import ttk

win = tkinter.Tk()
win.title("sunk")
win.geometry("400x400+200+20")
# 表格
tree = ttk.Treeview(win)
tree.pack()
# 定义列
tree["columns"] = ("姓名", "身高", "体重", "年龄", "性别")

# 设置列,列还不显示
tree.column("姓名", width=100)
tree.column("身高", width=100)
tree.column("体重", width=100)
tree.column("年龄", width=100)
tree.column("性别", width=100)
# 设置表头
tree.heading("姓名", text="姓名-name")
tree.heading("身高", text="身高-height")
tree.heading("体重", text="体重-weight")
tree.heading("年龄", text="年龄-age")
tree.heading("性别", text="性别-gender")
# 添加数据
tree.insert("", 0, text="line1", values=("1", "2", "3", "4", "5"))
tree.insert("", 0, text="line2", values=("6", "7", "8", "9", "10"))
tree.insert("", 0, text="line3", values=("11", "12", "13", "14", "15"))
tree.insert("", 0, text="line4", values=("16", "17", "18", "19", "20"))

win.mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第13张图片
(3)树状数据

import tkinter
from tkinter import ttk

win = tkinter.Tk()
win.title("sunk")
win.geometry("400x400+200+20")

tree = ttk.Treeview(win)
tree.pack()
# 添加一级树枝
treeF1 = tree.insert("", 0, "中国", text="中国CHA", values=("F1"))
treeF2 = tree.insert("", 1, "美国 ", text="美国USA", values=("F2"))
treeF4 = tree.insert("", 2, "英国", text="英国UK", values=("F3"))

# 二级树枝
treeF1_1 = tree.insert(treeF1, 0, "黑龙江", text="中国黑龙江",
                       values=("F1_1"))
treeF1_2 = tree.insert(treeF1, 1, "吉林", text="中国吉林",
                       values=("F1_2"))
treeF1_3 = tree.insert(treeF1, 2, "辽宁", text="中国辽宁",
                       values=("F1_3"))

treeF2_1 = tree.insert(treeF2, 0, "重庆", text="中国重庆",
                       values=("F1_1"))
treeF2_2 = tree.insert(treeF2, 1, "海南", text="中国海南",
                       values=("F1_2"))
treeF2_3 = tree.insert(treeF2, 2, "广东", text="中国广东",
                       values=("F1_3"))

# 三级树枝
treeF1_1_1 = tree.insert(treeF1_1, 0, "哈尔滨市", text="中国哈尔滨市")
treeF1_1_2 = tree.insert(treeF1_1, 1, "广州市", text="中国广州市")
win.mainloop()

python之GUI(tkinter、ttk)编程学习笔记(自用)_第14张图片

你可能感兴趣的:(Python)