thinker

'''
导入tkinter模块
创建控件
指定控件master,这个控件属于哪一个
告诉GM产生了一个控件
'''
import tkinter


#创建主窗口
win = tkinter.Tk()
#设置标题
win.title("title")
#设置大小和位置
#长400,宽200,距离左侧200,距离顶部20
win.geometry("400x200+200+20")


win.mainloop()




    ####Label####


import tkinter


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


'''
label:标签控件
可以显示文本
win:父窗体
text:显示的文本内容
bg:背景颜色
fg:字体颜色
font:字体,大小
width:宽度(为字体的倍数)
height:高度(为字体的倍数)
'''
label = tkinter.Label(win,
                      text = "This is text.",
                      bg = "blue",
                      fg = "white",
                      font = ("黑体", 20),
                      width = 10,
                      height = 2
                      )
#显示在窗口上
label.pack()
win.mainloop()




    ####Button####
import tkinter


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


def func():
    print("this is function.")


#创建按钮
# text = 按钮上显示的文字
#command = 点击按钮执行函数func
#width, height = 设置宽,高
button1 = tkinter.Button(win,
                        text = "button",
                        command = func,
                        width = 15,
                        height = 2)
#显示按钮
button1.pack()
#调用随机函数lambda
button2 = tkinter.Button(win,
                         text = "button",
                         command = lambda:print("this is lambda function."))
button2.pack()
#调用win.quie结束窗口
button3 = tkinter.Button(win, text = "quit", command = win.quit)
button3.pack()
win.mainloop()




    ####Entry输入文本控件####
import tkinter as tk


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


'''
Entry
输入文本框控件
显示简单文本内容
show = "*"密文显示
entry = tk.Entry(win, show = "*")
'''
#创建变量
#textvariable = e输入内容存入e
e = tk.Variable()
entry = tk.Entry(win, textvariable = e)
entry.pack()


'''
#设置输入框内默认内容
e.set("this is entry text")
#获取输入框内默认内容
print(e.get())
'''
'''
#显示函数
def show():
    print(e.get())
#command显示show函数
button = tk.Button(win, text = "Button", command = show)
button.pack()
'''
win.mainloop()




    ####加入滚动条的text文本框####


import tkinter as tk
win = tk.Tk()
win.title("title")
# win.geometry("400x400+200+20")


'''
Text
文本控件
用于显示多行文本
'''
text = tk.Text(win, width = 30, height = 4)
#设置滚动条
scroll = tk.Scrollbar()
#tk.RIGHT滚动条位置在右边,fill = tk.Y滚动条填充满Y轴
scroll.pack(side = tk.RIGHT, fill = tk.Y)
#文本框位置左边,填充满Y轴
text.pack(side = tk.LEFT, fill = tk.Y)
#关联滚动条
#配置滚动条 与text的y轴关联
#滚动条动控制文本动
scroll.config(command = text.yview)
#配置文本 与滚动条的设置关联
#文本动控制滚动条动
text.config(yscrollcommand = scroll.set)
win.mainloop()


    ####复选框Checkbutton####
import tkinter as tk


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


'''
Checkbutton多选控件
选中并打印内容
'''
def update():
    message = ""
    if hobby1.get() == True:
        message += "money\n"
    if hobby2.get() == True:
        message += "power\n"
    if hobby3.get() == True:
        message += "people\n"
    #清空所有内容,0.0相当于前面为0的第0行,end为清除到最后
    text.delete(0.0, tk.END)
    #将内容显示到message中
    text.insert(tk.INSERT, message)


#定义变量hobby1
hobby1 = tk.BooleanVar()
#多选控件check1,variable绑定变量
check1 = tk.Checkbutton(win, text = "money", variable = hobby1, command = update)
check1.pack()


hobby2 = tk.BooleanVar()
check2 = tk.Checkbutton(win, text = "power", variable = hobby2, command = update)
check2.pack()


hobby3 = tk.BooleanVar()
check3 = tk.Checkbutton(win, text = "people", variable = hobby3, command = update)
check3.pack()


text = tk.Text(win, width = 50, height = 5)
text.pack()


win.mainloop()


    ####单选框Radiobutton####


import tkinter as tk


win = tk.Tk()
win.title("title")
win.geometry("400x400+200+20")
'''
Radiobutton单选控件
选中并打印内容
'''
#定义函数update输出r的数值
def update():
    print(r.get())
#一组单选框绑定同一个变量
#变量值为value值
r = tk.IntVar()
#定义单选框,value=1选了单选框,值为1
#定义其他单选框,设置不同的值
radio1 = tk.Radiobutton(win, text = "one", value = 1, variable = r, command = update)
radio1.pack()
radio2 = tk.Radiobutton(win, text = "two", value = 2, variable = r, command = update)
radio2.pack()
radio3 = tk.Radiobutton(win, text = "three", value = 3, variable = r, command = update)
radio3.pack()
win.mainloop()




    ####Listbox####
 
import tkinter as tk


win = tk.Tk()
win.title("title")
win.geometry("400x400+200+20")
'''
Listbox列表框控件
可以包含一个或多个文本框,可单选可多选
作用:可以再Listbox控件的小窗口显示一个字符串
'''
#创建一个列表框控件
#添加一个tk.BROWSE模式
lb = tk.Listbox(win, selectmode = tk.BROWSE)
lb.pack()
#按顺序将列表内容添加到列表框
for item in ["good", "nice", "handsome", "cool"]:
    #按顺序添加
    lb.insert(tk.END, item)
#在开始添加
lb.insert(tk.ACTIVE, "well")
'''
删除列表元素,以参数1开始,以参数2结束
lb.delete(1, 3)
选中
lb.select_set(2, 4)
#取消选中
lb.select_clear(2, 3)


#获取列表中的元素个数
print(lb.size())
#取值
print(lb.get(2, 4))
print(lb.current)
'''
win.mainloop()


    ####Scale刻度尺####


import tkinter as tk


win = tk.Tk()
win.title("title")
win.geometry("400x400+200+20")
'''
Scale:刻度尺
通过拖拽指示器改变数值
'''
# from_=0, to = 100,范围从0到100
# tk.HORIZONTAL水平刻度尺,tk.VERTICAL垂直刻度尺
# tickinterval = 10刻度每10标识,length = 200长度为200
scale1 = tk.Scale(win, from_= 0, to = 100, orient = tk.HORIZONTAL, tickinterval = 50, length = 200)
scale1.pack()
#设置数值
scale1.set(20)
#显示数值
print(scale1.get())
#通过button显示数值
def Num():
    print(scale1.get())
tk.Button(win, text = "button", command = Num).pack()


win.mainloop()


    ####Spinbox数值范围控件####


import tkinter as tk


win = tk.Tk()
win.title("title")
win.geometry("400x400+200+20")
'''
Spinbox
数值范围控件
'''
def update():
    print(v.get())
#绑定变量
v = tk.StringVar()
#increment步长
#values=(2,4,6,8)数值为2,4,6,8,不能与from_,to同时使用
sp = tk.Spinbox(win, from_=0, to=100, increment=5, textvariable=v, command=update)
sp.pack()
#赋值取值
v.set(20)
print(v.get())
win.mainloop()




    ####Menu菜单####


import tkinter as tk


win = tk.Tk()
win.title("title")
win.geometry("400x400+200+20")
'''
Menu顶层菜单
'''
def func():
    print("this is a function")


#菜单条
menubar = tk.Menu(win)
win.config(menu=menubar)
#开始创建菜单中内容
#tearoff=True子菜单可以单独分离
menu1 = tk.Menu(menubar, tearoff=False)
#win.quit退出窗口
for item in ["Python", "C", "C++", "OC", "Swift", "C#", "Java", "PHP", "汇编", "退出"]:
    if item =="退出":
        #添加分割线
        menu1.add_separator()
        menu1.add_command(label=item, command=win.quit)
    else:
        menu1.add_command(label=item, command=func)


#向菜单条上添加菜单
#label:菜单名
menubar.add_cascade(label="语言", menu=menu1)


menu2 = tk.Menu(menubar, tearoff=False)
menu2.add_command(label="red")
menu2.add_command(label="blue")
menubar.add_cascade(label="颜色", menu=menu2)


win.mainloop()


    ####Combobox下拉菜单控件####


import tkinter as tk
from tkinter import ttk


win = tk.Tk()
win.title("title")
win.geometry("400x400+200+20")
'''
Combobox下拉菜单控件
'''


com = ttk.Combobox(win)
com.pack()
#设置下拉菜单
com["value"] = ("黑龙江", "吉林", "辽宁")
#设置第0位为默认值
com.current(0)
#选定一个选项输出选项名
def func(event):
    print(com.get())
#绑定事件
com.bind("<<ComboboxSelected>>", func)


win.mainloop()


    ####Frame框架控件####


import tkinter as tk


win = tk.Tk()
win.title("title")
win.geometry("400x400+200+20")
'''
Frame框架控件
在平面上显示一个矩形区域,作为容器控件
'''
frm = tk.Frame(win)
frm.pack()


#left
frm_l = tk.Frame(frm)
tk.Label(frm_l, text="左上", bg="pink").pack(side=tk.TOP)
tk.Label(frm_l, text="左下", bg="blue").pack(side=tk.TOP)
frm_l.pack(side=tk.LEFT)


#right
frm_r = tk.Frame(frm)
tk.Label(frm_r, text="右上", bg="red").pack(side=tk.TOP)
tk.Label(frm_r, text="右下", bg="yellow").pack(side=tk.TOP)
frm_r.pack(side=tk.LEFT)


win.mainloop()




    ####表格数据####
import tkinter as tk
from tkinter import ttk


win = tk.Tk()
win.title("title")
win.geometry("600x400+200+20")


# 表格数据
# 表格
tree = ttk.Treeview(win)
#定义列
tree["columns"] = ("姓名", "年龄" ,"身高", "体重")
tree.pack()
# 设置列,还不显示,width宽度
tree.column("姓名", width=100)
tree.column("年龄", width=100)
tree.column("身高", width=100)
tree.column("体重", width=100)


# 设置表头
# 表头名称为姓名_name,"姓名"对应设置列中"姓名"
tree.heading("姓名", text="姓名_name")
tree.heading("年龄", text="年龄_age")
tree.heading("身高", text="身高_height")
tree.heading("体重", text="体重_weight")


# 添加数据
# 向第0、1行添加数据,其他数据依此推移到下一行
# 数据名称为line1、2,values依此添加表头数据
tree.insert("", 0, text="line1", values=("薇尔莉特", "20", "165", "45"))
tree.insert("", 0, text="line2", values=("木乃伊", "3000", "15", "0.5"))
tree.insert("", 1, text="line3", values=("第三行", "1", "2", "3"))


win.mainloop()




    ####Treeview树状数据####
import tkinter as tk
from tkinter import ttk


win = tk.Tk()
win.title("title")
win.geometry("600x400+200+20")


# 树状数据Treeview
tree = ttk.Treeview(win)
tree.pack()


# 添加一级树枝
treeF1 = tree.insert("", 0, "中国", text="中国_China", value=("F1"))
treeF2 = tree.insert("", 1, "美国", text="美国_USA", value=("F2"))
treeF3 = tree.insert("", 2, "英国", text="英国_UK", value=("F3"))
treeF4 = tree.insert("", 3, "日本", text="日本_JAP", value=("F4"))


# 添加二级树枝
#(一级树枝, 第0、1个数据,数据名称,数据显示名称, 数值)
treeF1_1 = tree.insert(treeF1, 0, "黑龙江", text="中国_黑龙江", value=("F1_1"))
treeF1_2 = tree.insert(treeF1, 1, "河南", text="中国_河南", value=("F1_2"))
treeF1_3 = tree.insert(treeF1, 2, "天津", text="中国_天津", value=("F1_3"))
treeF2_1 = tree.insert(treeF2, 0, "纽约", text="美国_纽约", value=("F2_1"))
treeF2_2 = tree.insert(treeF2, 1, "华盛顿", text="美国_华盛顿", value=("F2_2"))
treeF3_1 = tree.insert(treeF3, 0, "曼彻斯特", text="英国_曼彻斯特", value=("F3_1"))
treeF3_2 = tree.insert(treeF3, 1, "伦敦", text="英国_伦敦", value=("F3_2"))
treeF4_1 = tree.insert(treeF4, 0, "东京", text="日本_东京", value=("F4_1"))
treeF4_2 = tree.insert(treeF4, 1, "大阪", text="日本_大阪", value=("F4_2"))


win.mainloop()


    ####布局####
import tkinter as tk
from tkinter import ttk


win = tk.Tk()
win.title("title")
win.geometry("600x400+200+20")


label1 = tk.Label(win, text="good", bg="blue")
label2 = tk.Label(win, text="nice", bg="pink")
label3 = tk.Label(win, text="well", bg="red")


# 绝对布局 #
# 改变窗口大小布局位置没变化
# 通过位置显示标签
label1.place(x=10, y=10)
label2.place(x=50, y=50)
label3.place(x=100, y=100)


# 相对布局 #
# 窗体改变对控件有影响
label1.pack(fill=tk.Y, side=tk.LEFT)
label2.pack(fill=tk.X, side=tk.TOP)
label3.pack()


# 表格布局 #
# 布局,row行column列
label1.grid(row=0, column=0)
label2.grid(row=1, column=0)
label3.grid(row=0, column=1)
label4.grid(row=1, column=1)


win.mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值