首页 > 技术文章 > tkinter学习04

Mengchangxin 2018-11-01 09:34 原文

 演示示例

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")


#进入消息循环



root.mainloop()
View Code

标签 

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("标签控件")

#设置窗体大小和位置
root.geometry("400x400+200+20")
"Label:标签控件,可以显示文本"
label=Label(root,
            text="可以显示文", #显示文本内容
            bg="pink",#背景色
            fg="blue",#字体颜色
            font=("黑体",20),#文本字体,大小,样式(加粗,斜体等)
            width=20,#文本宽度
            height=4,#文本高度
            wraplength=100,#指定text中多宽后,进行换行
            justify="left",#设置换行后的对其方式。
            anchor="w",#放置位置 按照东南西北must be n, ne, e, se, s, sw, w, nw, or center
            )
label.pack()

root.mainloop()
View Code

 按钮

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")
def callback():
    print("调用函数")
button=Button(root,text="按钮",command=callback,
              width=10,height=10)
button.pack()
button2=Button(root,text="按钮",command=lambda  :print("调用匿名函数"))
button2.pack()
button3=Button(root,text="退出",command=root.quit)
button3.pack()

root.mainloop()
View Code

 输入框

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")
#为了能取到输入框的值,我们需要给它绑定变量
e=Variable()#实例化一个变量

entry=Entry(root,textvariable=e)
entry.pack()

#e就代表输入框这个对象
#设置值
e.set("我来设置输入框的值")

#取到值.两个都可以取到值
print(e.get())
print(entry.get())

entry0=Entry(root)
entry0.pack()

#e就代表输入框这个对象
#设置值
entry0.insert(0, "我也可以设置输入框的值")
#取到值.两个都可以取到值

print(entry0.get())

#显示密码框
entry1=Entry(root,show="*")
entry1.pack()

root.mainloop()
View Code

"D:\Program Files (x86)\python36\python.exe" F:/python从入门到放弃/tkinter/11.1/输入框.py
我来设置输入框的值
我来设置输入框的值
我也可以设置输入框的值

小练习:输出输入框的内容 

from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")

entry=Entry(root)
entry.pack()
def callback():
    print(entry.get())

button=Button(root,text="按钮",command=callback)
button.pack()
View Code

 

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")
e=Variable()#实例化一个变量
entry=Entry(root,textvariable=e)
entry.pack()
def callback():
    print(e.get())

button=Button(root,text="按钮",command=callback)
button.pack()
View Code

 

"D:\Program Files (x86)\python36\python.exe" F:/python从入门到放弃/tkinter/11.1/小练习.py
你好
你好
我是
取到输入框内容

 文本

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")

#文本控件,用于显示多行文本
text=Text(root,width=30,height=4)#30指30个英文字母的长度
text.pack()

root.mainloop()
View Code

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")
string="There are plenty of things to note in this example." \
       " First, no position is specified for the label widgets." \
       " In this case, the column defaults to 0, and the row to the " \
       "first unused row in the grid. Next, the entry widgets are " \
       "positioned as usual, but the checkbutton widget is placed on " \
       "the next empty row (row 2, in this case), "
#文本控件,用于显示多行文本
text=Text(root,width=30,height=4)#30指30个英文字母的长度
text.pack()
text.insert(INSERT,string)

root.mainloop()
View Code

 滚动条

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
#root.geometry("400x400+200+20")
string="There are plenty of things to note in this example." \
       " First, no position is specified for the label widgets." \
       " In this case, the column defaults to 0, and the row to the " \
       "first unused row in the grid. Next, the entry widgets are " \
       "positioned as usual, but the checkbutton widget is placed on " \
       "the next empty row (row 2, in this case), "
#创建一个滚动条
scroll=Scrollbar()

#文本控件,用于显示多行文本
text=Text(root,width=60,height=4)#30指30个英文字母的长度
scroll.pack(side=RIGHT,fill=Y)#把滚动条放到窗体的右侧,把Y轴方向填充满
text.pack(side=LEFT,fill=Y)#把文本框放到窗体的左侧,把Y轴方向填充满
#配置文件上进行关联
scroll.config(command=text.yview)#给滚动条配置。让文本框的y方向视图关联
text.config(yscrollcommand=scroll.set)#给文本框配置。让滚动条的位置随之文本的上下移动


text.insert(INSERT,string)
View Code

 复选框

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *


root=Tk()
root.title("植物大战僵尸")
root.geometry("400x400+200+20")
def updata():
    message=""
    if hobby1.get()==True:
        message+="pingpang\n"
    if hobby2.get()==True:
        message+="basketball\n"
    if hobby3.get()==True:
        message+="football\n"
    text.delete(0.0,END)#清楚文本框中所有数据。从第一行第一个字符开始到最后
    text.insert(INSERT,message)

hobby1 = BooleanVar()
check1 = Checkbutton(root, text="pingpang",variable=hobby1,command=updata)
check1.pack()
hobby2 = BooleanVar()
check2 = Checkbutton(root, text="basketball",variable=hobby2,command=updata)
check2.pack()
hobby3 = BooleanVar()
check3 = Checkbutton(root, text="football",variable=hobby3,command=updata)
check3.pack()

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

button=Button(root,text="按钮",command=updata)
button.pack()



root.mainloop()
View Code

 单选框

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")
def updata():
    print(r.get())
    text.delete(0.0, END)  # 清楚文本框中所有数据。从第一行第一个字符开始到最后
    text.insert(INSERT, r.get())

r=IntVar()

radio1=Radiobutton(root,text="one",value=1,variable=r,command=updata)
radio1.pack()
radio2=Radiobutton(root,text="two",value=2,variable=r,command=updata)
radio2.pack()
text=Text(root,width=50,height=5)
text.pack()
button=Button(root,text="按钮",command=updata)
button.pack()


root.mainloop()
View Code

 一组单选框要绑定同一个变量

 列表框控件

在Listbox窗口小部件是用来显示一个字符串列表给用户

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")
'''
列表框控件,可以包含一个或是多个文本框
列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户'''
lb=Listbox(root,selectmode=BROWSE)
lb.pack()
for item in ["good","nice","handsome","vg","vn"]:
    lb.insert(END,item)
lb.insert(ACTIVE,"cool")
lb.insert(END,["verygood","verynice"])
#删除一些数据。参数1为开始的索引,参数2为结束的索引。如果不指定参数2,只删除第一个索引出的内容
lb.delete(1,3)
lb.delete(3)


root.mainloop()
View Code

 

# lb.delete(1,3)
# lb.delete(3)
#选中某项
# lb.select_set(2,4)
lb.select_set(2)
root.mainloop()

 

1 #选中某项
2 lb.select_set(2,4)
3 # lb.select_set(2)
4 #取消选中
5 lb.select_clear(3)
6 
7 
8 root.mainloop()

 

lb.select_set(2,4)
# lb.select_set(2)
#取消选中
lb.select_clear(3)

#获取到列表中的元素个数
print(lb.size())

root.mainloop()

结果:

"D:\Program Files (x86)\python36\python.exe" F:/python从入门到放弃/tkinter/11.1/listbox控件.py
7

Process finished with exit code 0
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")
'''
列表框控件,可以包含一个或是多个文本框
列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户'''
lb=Listbox(root,selectmode=BROWSE)
lb.pack()
for item in ["good","nice","handsome","vg","vn"]:
    lb.insert(END,item)
lb.insert(ACTIVE,"cool")
lb.insert(END,["verygood","verynice"])
#删除一些数据。参数1为开始的索引,参数2为结束的索引。如果不指定参数2,只删除第一个索引出的内容
# lb.delete(1,3)
# lb.delete(3)
#选中某项
lb.select_set(2,4)
# lb.select_set(2)
#取消选中
lb.select_clear(3)
#lb.select_clear(2,4)

#获取到列表中的元素个数
print(lb.size())

#取值
print(lb.get(2,4))
print(lb.get(2))

root.mainloop()
View Code
"D:\Program Files (x86)\python36\python.exe" F:/python从入门到放弃/tkinter/11.1/listbox控件.py
7
('nice', 'handsome', 'vg')
nice

Process finished with exit code 0
#获取选择的值的索引
print(lb.curselection())

  

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")
'''
列表框控件,可以包含一个或是多个文本框
列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户'''
lb=Listbox(root,selectmode=BROWSE)
lb.pack()
for item in ["good","nice","handsome","vg","vn"]:
    lb.insert(END,item)
lb.insert(ACTIVE,"cool")
lb.insert(END,["verygood","verynice"])
#删除一些数据。参数1为开始的索引,参数2为结束的索引。如果不指定参数2,只删除第一个索引出的内容
# lb.delete(1,3)
# lb.delete(3)
#选中某项
lb.select_set(2,4)
#lb.select_set(2)
# #取消选中
# lb.select_clear(3)
# #lb.select_clear(2,4)
#
# #获取到列表中的元素个数
# print(lb.size())
#
# #取值
# print(lb.get(2,4))
# print(lb.get(2))
#获取选择的值
print(lb.curselection())


root.mainloop()
View Code

 

"D:\Program Files (x86)\python36\python.exe" F:/python从入门到放弃/tkinter/11.1/listbox控件.py
(2, 3, 4)

 

#判断一个选项是否被选中
print(lb.select_includes(1))
print(lb.select_includes(3))
"D:\Program Files (x86)\python36\python.exe" F:/python从入门到放弃/tkinter/11.1/listbox控件.py
(2, 3, 4)
False
True
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")
'''
列表框控件,可以包含一个或是多个文本框
列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户'''
lb=Listbox(root,selectmode=BROWSE)
lb.pack()
for item in ["good","nice","handsome","vg","vn"]:
    lb.insert(END,item)
lb.insert(ACTIVE,"cool")
lb.insert(END,["verygood","verynice"])
#删除一些数据。参数1为开始的索引,参数2为结束的索引。如果不指定参数2,只删除第一个索引出的内容
# lb.delete(1,3)
# lb.delete(3)
#选中某项
lb.select_set(2,4)
#lb.select_set(2)
# #取消选中
# lb.select_clear(3)
# #lb.select_clear(2,4)
#
# #获取到列表中的元素个数
# print(lb.size())
#
# #取值
# print(lb.get(2,4))
# print(lb.get(2))
#获取选择的值
print(lb.curselection())
#判断一个选项是否被选中
print(lb.select_includes(1))
print(lb.select_includes(3))

root.mainloop()
View Code

 

The listbox offers four different selection modes through the selectmode option. These are SINGLE (just a single choice), BROWSE (same, but the selection can be moved using the mouse), MULTIPLE (multiple item can be choosen, by clicking at them one at a time), or EXTENDED (multiple ranges of items can be chosen, using the Shift and Control keyboard modifiers). The default is BROWSE. Use MULTIPLE to get “checklist” behavior, and EXTENDED when the user would usually pick only one item, but sometimes would like to select one or more ranges of items.
View Code
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")
'''
列表框控件,可以包含一个或是多个文本框
列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户'''
lb=Listbox(root,selectmode=SINGLE)
lb.pack()
for item in ["good","nice","handsome","vg","vn"]:
    lb.insert(END,item)

root.mainloop()
single模式

 These are SINGLE (just a single choice), BROWSE (same, but the selection can be moved using the mouse), MULTIPLE (multiple item can be choosen, by clicking at them one at a time), or EXTENDED (multiple ranges of items can be chosen, using the Shift and Control keyboard modifiers). The default is BROWSE

 

#设置窗体大小和位置
root.geometry("400x400+200+20")
'''
列表框控件,可以包含一个或是多个文本框
列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户'''
lbv=StringVar()#绑定变量
lb=Listbox(root,selectmode=SINGLE,listvariable=lbv)
lb.pack()
for item in ["good","nice","handsome","vg","vn"]:
    lb.insert(END,item)
#打印当前列表中的值
print(lbv.get())
#设置选项
lbv.set(("1","2","3"))
#绑定事件
def my_print(evnet):
    print(lb.curselection())
lb.bind("<Double-Button-1>",my_print)


root.mainloop()
View Code
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/1 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")
'''
列表框控件,可以包含一个或是多个文本框
列表框控件;在Listbox窗口小部件是用来显示一个字符串列表给用户'''
lbv=StringVar()#绑定变量
lb=Listbox(root,selectmode=SINGLE,listvariable=lbv)
lb.pack()
for item in ["good","nice","handsome","vg","vn"]:
    lb.insert(END,item)
#打印当前列表中的值
print(lbv.get())
#设置选项
#lbv.set(("1","2","3"))
#绑定事件
def my_print(evnet):
    print(lb.curselection())
    print(lb.get(lb.curselection()))
lb.bind("<Double-Button-1>",my_print)


root.mainloop()
View Code

 

"D:\Program Files (x86)\python36\python.exe" F:/python从入门到放弃/tkinter/11.1/listbox控件2.py
('good', 'nice', 'handsome', 'vg', 'vn')
(2,)
handsome
(3,)
vg

Process finished with exit code 0

 

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator
2018/11/1
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
# root.geometry("400x400+200+20")

lb=Listbox(root,selectmode=EXTENDED)

for item in ["good","nice","handsome","vg","vn",
             "good", "nice", "handsome", "vg", "vn",
             "good", "nice", "handsome", "vg", "vn",
             "good", "nice", "handsome", "vg", "vn",
             "good", "nice", "handsome", "vg", "vn",
             "good", "nice", "handsome", "vg", "vn",]:
    lb.insert(END,item)

#滚动条
sc=Scrollbar(root)
sc.pack(side=RIGHT,fill=Y)
lb.configure(yscrollcommand=sc.set)
lb.pack(side=LEFT,fill=BOTH)
#给属性赋值。的另一种方法。等同于sc.config(command=lb.yview)
sc['command']=lb.yview

root.mainloop()
带滚动条

 

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator
2018/11/1
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")

lb=Listbox(root,selectmode=MULTIPLE )

for item in ["good","nice","handsome","vg","vn",
             "good", "nice", "handsome", "vg", "vn",
             "good", "nice", "handsome", "vg", "vn",
             "good", "nice", "handsome", "vg", "vn",
             "good", "nice", "handsome", "vg", "vn",
             "good", "nice", "handsome", "vg", "vn",]:
    lb.insert(END,item)

lb.pack()
root.mainloop()
View Code

Scale滑块

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator
2018/11/1
'''
from tkinter import *

#创建主窗口
root=Tk()
#设置窗体大小和位置
root.geometry("400x400+200+20")
root.title("植物大战僵尸")
'''
供用户通过拖拽指示器的改变变量的值,可以水平,也可以竖直
orient=HORIZONTAL  水平
orient=VERTICAL    竖直
length=200 长度。滑块长度
tickinterval=10  选择值将会为该值得倍数  标记刻度
'''
scale=Scale(root,from_=0,to=100,orient=HORIZONTAL,tickinterval=20,length=200)
scale.pack()

#设置值
scale.set(20)


#print(scale.get()) #取值
def showNum():
    print(scale.get())
buttom=Button(root,text="按钮",command=showNum).pack()


scale1=Scale(root,from_=0,to=100,orient=VERTICAL,length=200)
scale1.pack()
root.mainloop()
View Code

 

Spinbox数值范围控件

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator
2018/11/1
'''
from tkinter import *

#创建主窗口
root=Tk()
#设置窗体大小和位置
root.geometry("400x400+200+20")
root.title("植物大战僵尸")
'''
数值范围控件
increment=5   步长  默认为1
values=(0,2,4,6,8)不要与from_=0,to=100 一起使用
'''
def updata():
    print(v.get())
v=StringVar()
sp=Spinbox(root,from_=0,to=100,increment=5,textvariable=v,command=updata#只要值改变就会触发函数
           #values=(0,2,4,6,8)
           )
sp.pack()
#设置和取值
v.set(20)




root.mainloop()
View Code

 顶层菜单

 

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator
2018/11/1
'''
from tkinter import *

#创建主窗口
root=Tk()
#设置窗体大小和位置
root.geometry("400x400+200+20")
root.title("植物大战僵尸")

menubar=Menu(root)#创建菜单条的框。留着放整行的菜单
root.config(menu=menubar)#配置菜单。把菜单框架放到root 上

def func():
    print("菜单演示")


#创建一个菜单选项
menu1=Menu(menubar,tearoff=False)
#给菜单选项添加内容
for item in ["python","C","C++","OC","Swift",
             "C#","shell","Java","JS","PHP","汇编","NodeJS","退出"]:
    if item=="退出":
        menu1.add_separator()
        menu1.add_command(label=item,command=root.quit)
    else:
        menu1.add_command(label=item,command=func)
menubar.add_cascade(label="语言",menu=menu1)#向菜单条上添加菜单


#创建一个菜单选项
menu2=Menu(menubar,tearoff=False)
#给菜单选项添加内容
for item in ["python","C","C++","OC","Swift",
             "C#","shell","Java","JS","PHP","汇编","NodeJS","退出"]:
    if item=="退出":
        menu2.add_command(label=item,command=root.quit)
    else:
        menu2.add_command(label=item,command=func)
menubar.add_cascade(label="编辑",menu=menu2)#向菜单条上添加菜单


root.mainloop()
View Code

 鼠标右键菜单

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator
2018/11/1
'''
from tkinter import *

#创建主窗口
root=Tk()
#设置窗体大小和位置
root.geometry("400x400+200+20")
root.title("植物大战僵尸")

menubar=Menu(root)#创建菜单条的框。留着放整行的菜单


def func():
    print("菜单演示")


#创建一个菜单选项
menu1=Menu(menubar,tearoff=True)
#给菜单选项添加内容
for item in ["python","C","C++","OC","Swift",
             "C#","shell","Java","JS","PHP","汇编","NodeJS","退出"]:
    menu1.add_command(label=item,command=func)
menubar.add_cascade(label="语言",menu=menu1)#向菜单条上添加菜单

#创建一个菜单选项
menu2=Menu(menubar,tearoff=False)
#给菜单选项添加内容
for item in ["python","C","C++","OC","Swift",
             "C#","shell","Java","JS","PHP","汇编","NodeJS","退出"]:
    if item=="退出":
        menu2.add_command(label=item,command=root.quit)
    else:
        menu2.add_command(label=item,command=func)
menubar.add_cascade(label="编辑",menu=menu2)#向菜单条上添加菜单



def show_menu(event):
    menubar.post(event.x_root,event.y_root)
root.bind("<Button-3>",show_menu)




root.mainloop()
View Code

 下来框

from tkinter import *
from tkinter import ttk
#创建主窗口
root=Tk()
#设置窗体大小和位置
root.geometry("400x400+200+20")
root.title("植物大战僵尸")

comb=ttk.Combobox(root)



root.mainloop()
View Code

 

from tkinter import *
from tkinter import ttk
#创建主窗口
root=Tk()
#设置窗体大小和位置
root.geometry("400x400+200+20")
root.title("植物大战僵尸")
cv=StringVar()
comb=ttk.Combobox(root,textvariable=cv)
comb.pack()
#设置下拉数据
comb["value"]=("黑龙江","吉林","辽宁")
#设置默认值
# comb.current(0)
cv.set(0)
#绑定事件
def func(event):
    # print(comb.get())
    print(cv.get())

comb.bind("<<ComboboxSelected>>",func)
root.mainloop()
View Code

fram框架布局 

from tkinter import *

#创建主窗口
root=Tk()
#设置窗体大小和位置
root.geometry("400x400+200+20")
root.title("植物大战僵尸")
'''
框架控件
在屏幕上显示一个矩形区域,多作为容器控件
'''
frm=Frame(root)
frm.pack()

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

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

root.mainloop()
View Code

表格数据 

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/2 
'''
from tkinter import *
from tkinter import ttk
#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("600x400+200+20")


tree=ttk.Treeview(root)
tree.pack()

#定义列
tree["columns"]=("姓名","年龄","身高","体重")
#设置列,列还不显示
tree.column("姓名",width=100)
tree.column("年龄",width=100)
tree.column("身高",width=100)
tree.column("体重",width=100)
#设置头用来显示
tree.heading("姓名",text="姓名-name")
tree.heading("年龄",text="年龄-age")
tree.heading("身高",text="身高-height")
tree.heading("体重",text="体重-weight")
#添加数据
tree.insert("",0,text="line1",values=("牛有道","28","165","80"))
tree.insert("",1,text="line2",values=("邵凌波","29","185","90"))




root.mainloop()
View Code

树状数据

 

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/2 
'''
from tkinter import *
from tkinter import ttk
#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")


tree=ttk.Treeview(root)
tree.pack()

#添加一级树枝
treeFirst=tree.insert("",0,"中国",text="中国CHN",values=("F1",))
treeSecond=tree.insert("",1,"美国",text="美国USA",values=("F2",))
treeThird=tree.insert("",2,"英国",text="英国UK",values=("F3",))

#添加二级树枝
treeFirst_1=tree.insert(treeFirst,0,"南京1",text="江苏南京",values=("F1_1"))
treeFirst_2=tree.insert(treeFirst,1,"南京2",text="江苏苏州",values=("F1_2"))
treeFirst_3=tree.insert(treeFirst,2,"南京3",text="江苏常州",values=("F1_3"))

treeSecond_1=tree.insert(treeSecond,0,"济南1",text="山东济南",values=("F1_1"))
treeSecond_1=tree.insert(treeSecond,1,"济南2",text="山东德州",values=("F1_2"))
treeSecond_1=tree.insert(treeSecond,2,"济南3",text="山东青岛",values=("F1_3"))

#三级树枝
treeFirst_1_1=tree.insert(treeFirst_1,0,"刘欢",text="刘欢呀",values=("ooo"))
treeFirst_1_2=tree.insert(treeFirst_1,1,"刘欢1",text="刘欢呀111",values=("ooo"))

root.mainloop()
View Code

绝对布局

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/2 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")

label1=Label(root,text="good",bg="blue")
label2=Label(root,text="好的",bg="red")
label3=Label(root,text="你好",bg="blue")

#绝对布局
label1.place(x=10,y=10)
label2.place(x=50,y=50)
label3.place(x=100,y=100)


root.mainloop()
View Code

          

 

相对布局

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/2 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")

label1=Label(root,text="good",bg="blue")
label2=Label(root,text="好的",bg="red")
label3=Label(root,text="你好",bg="blue")

#相对布局
label1.pack(fill=Y,side=LEFT)
label2.pack(fill=X,side=TOP)
label3.pack()


root.mainloop()
View Code

表格布局

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/2 
'''
from tkinter import *

#创建主窗口
root=Tk()
root.title("植物大战僵尸")

#设置窗体大小和位置
root.geometry("400x400+200+20")

label1=Label(root,text="good",bg="blue")
label2=Label(root,text="好的",bg="red")
label3=Label(root,text="你好",bg="blue")

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


root.mainloop()
View Code

 


 

 

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
Administrator 
2018/11/2
'''
from tkinter import *
from tkinter import ttk
import tkinter.filedialog as dir

class AppUI():

    def __init__(self):
        root = Tk()
        self.create_menu(root)
        self.create_content(root)
        self.path = 'C:'
        root.title("磁盘文件搜索工具")
        root.update()
        # root.resizable(False, False) 调用方法会禁止根窗体改变大小
        #以下方法用来计算并设置窗体显示时,在屏幕中心居中
        curWidth = root.winfo_width()  # get current width
        curHeight = root.winfo_height()  # get current height
        scnWidth, scnHeight = root.maxsize()  # get screen width and height
        tmpcnf = '800x600+%d+%d' % ((scnWidth - curWidth) / 2, (scnHeight - curHeight) / 2)
        root.geometry(tmpcnf)
        root.mainloop()


    def create_menu(self,root):
        #创建菜单栏
        menu = Menu(root)

        #创建二级菜单
        file_menu = Menu(menu,tearoff=0)
        file_menu.add_command(label="设置路径",command=self.open_dir)
        file_menu.add_separator()

        scan_menu = Menu(menu)
        file_menu.add_command(label="扫描")

        about_menu = Menu(menu,tearoff=0)
        about_menu.add_command(label="version:1.0")

        #在菜单栏中添加以下一级菜单
        menu.add_cascade(label="文件",menu=file_menu)
        menu.add_cascade(label="关于",menu=about_menu)
        root['menu'] = menu

    def create_content(self, root):
        mainf = Frame(root)
        mainf.pack()

        frm_l=ttk.LabelFrame(mainf, text="文件搜索")
        fram=Frame(frm_l)
        tree = ttk.Treeview(fram)
        tree.pack(side=LEFT)
        treeFirst = tree.insert("", 0, "中国", text="中国CHN", values=("F1",))
        treeSecond = tree.insert("", 1, "美国", text="美国USA", values=("F2",))
        treeThird = tree.insert("", 2, "英国", text="英国UK", values=("F3",))
        # 添加二级树枝
        treeFirst_1 = tree.insert(treeFirst, 0, "南京1", text="江苏南京", values=("F1_1"))
        treeFirst_2 = tree.insert(treeFirst, 1, "南京2", text="江苏苏州", values=("F1_2"))
        treeFirst_3 = tree.insert(treeFirst, 2, "南京3", text="江苏常州", values=("F1_3"))

        treeSecond_1 = tree.insert(treeSecond, 0, "济南1", text="山东济南", values=("F1_1"))
        treeSecond_1 = tree.insert(treeSecond, 1, "济南2", text="山东德州", values=("F1_2"))
        treeSecond_1 = tree.insert(treeSecond, 2, "济南3", text="山东青岛", values=("F1_3"))

        # 三级树枝
        treeFirst_1_1 = tree.insert(treeFirst_1, 0, "刘欢", text="刘欢呀", values=("ooo"))
        treeFirst_1_2 = tree.insert(treeFirst_1, 1, "刘欢1", text="刘欢呀111", values=("ooo"))

        frm_l.pack(fill=X, padx=15, pady=8,side=LEFT)
        fram.pack()



        frm_r = ttk.LabelFrame(mainf, text="文件搜索")
        subfram=Frame(frm_r)
        tree_cont = ttk.Treeview(subfram)
        tree_cont.pack()
        # 定义列
        tree_cont["columns"] = ("姓名", "年龄", "身高", "体重")
        # 设置列,列还不显示
        tree_cont.column("姓名", width=100)
        tree_cont.column("年龄", width=100)
        tree_cont.column("身高", width=100)
        tree_cont.column("体重", width=100)

        # 设置头用来显示
        tree_cont.heading("姓名", text="姓名-name")
        tree_cont.heading("年龄", text="年龄-age")
        tree_cont.heading("身高", text="身高-height")
        tree_cont.heading("体重", text="体重-weight")
        # 添加数据
        tree_cont.insert("", 0, text="line1", values=("牛有道", "28", "165", "80"))
        tree_cont.insert("", 1, text="line2", values=("邵凌波", "29", "185", "90"))
        tree_cont.insert("", 2, text="line3", values=("牛有道", "28", "165", "80"))
        tree_cont.insert("", 3, text="line4", values=("邵凌波", "29", "185", "90"))
        tree_cont.insert("", 4, text="line5", values=("牛有道", "28", "165", "80"))
        tree_cont.insert("", 5, text="line6", values=("邵凌波", "29", "185", "90"))
        tree_cont.insert("",6, text="line7", values=("牛有道", "28", "165", "80"))
        tree_cont.insert("", 7, text="line8", values=("邵凌波", "29", "185", "90"))
        tree_cont.insert("", 8, text="line9", values=("牛有道", "28", "165", "80"))
        tree_cont.insert("", 9, text="line0", values=("邵凌波", "29", "185", "90"))

        frm_r.pack(fill=X, padx=10, pady=8,side=RIGHT)
        subfram.pack()









    def search_file(self):
        pass

    def open_dir(self):
        d = dir.Directory()
        self.path = d.show(initialdir=self.path)



if __name__ == "__main__":
    AppUI()
View Code

 

推荐阅读