1.Label控件
语法格式:label=tk.Label(master,option,…)
其中master是其父控件,用来放置这个控件的。
Tkinter容器控件没有背景属性,通常利用Label图片属性来给容器控件增加背景图片
import tkinter as tk
import time
from PIL import Image,ImageTk
root=tk.Tk()
root.title('最新时间')
root.geometry('400x400')
imgg=Image.open('D://anaconda3//1.jpg')
img=ImageTk.PhotoImage(imgg)
label_img=tk.Label(root,image=img)
label_img.place(x=0,y=0)
time1=tk.StringVar()
time2=tk.StringVar()
L1=tk.Label(root,textvariable=time1,bg='#1E288F',fg='red')
L2=tk.Label(root,textvariable=time2,bg='#1E288F',fg='blue')
L1.pack()
L2.pack()
def settime():
today=str('现在日期:'+time.strftime('%Y-%m-%d',time.localtime(time.time())))
time3=str('现在时间:'+time.strftime('%H:%M:%S',time.localtime(time.time())))
time1.set(today)
time2.set(time3)
settime()
root.mainloop()
2.Frame控件
Frame控件负责安排其他控件的位置。
窗口画面布局过程:
首先建立布局:主窗口布局,用不同颜色和大小的Frame类进行填充,调整位置直到自己满意。
其次设计不同功能的子窗口,就是把Frame类放置Frame中
最后根据需要可以程序控制不同功能的Frame类显示到对应的主窗口中
import tkinter as tk
root=tk.Tk()
root.title('Frame演示')
labels=[]
reliefs=[tk.FLAT,tk.SUNKEN,tk.RAISED,tk.GROOVE,tk.RIDGE]
i=0
for color in ['blue','red','yellow','purple','pink']:
f=tk.Frame(root,borderwidth=1,bg=color,relief=reliefs[i],bd=20,width=100,height=100).pack(side=tk.LEFT)
i+=1
root.mainloop()
#图片三
3.Button控件
用于实现各种包含文本和图形的按钮。
import tkinter as tk
def hello():
print('hello!')
root=tk.Tk()
root.title('Button测试')
tk.Button(root,text='左边',compound='left',bitmap='error',cursor='heart').pack()
tk.Button(root,text='调用命令',width=23,fg='red',bd=2,command=hello,cursor='icon',underline=4).pack()
tk.Button(root,text='外观修饰',width=19,relief=tk.GROOVE,bg='yellow',cursor='mouse').pack()
tk.Button(root,text='设置高宽度以及文字显示位置',anchor=tk.SW,width=32,height=4,cursor='man').pack()
root.mainloop()
#图片4,5
4.Entry控件
该控件是Tkinter的文本框,用来接收字符串等输入的控件,允许用户查看和修改一行文本。
import tkinter as tk
root=tk.Tk()
tk.Label(root,text='作品:').grid(row=0)
tk.Label(root,text='作者:').grid(row=1)
a1=tk.Entry(root)
a2=tk.Entry(root)
a1.grid(row=0,column=1,padx=10,pady=5)
a2.grid(row=1,column=1,padx=10,pady=5)
def show():
print('作品:《%s》'%a1.get())
print('作者:%s'%a2.get())
a1.delete(0,"end")
a2.delete(0,"end")
tk.Button(root,text='显示',command=show,width=10).grid(row=3,column=0)
tk.Button(root,text='退出',command=root.quit,width=10).grid(row=3,column=1)
root.mainloop()
#图片6,7
5.Text控件
主要作用是显示多行文本,常常被当做简单的文本处理器、文本编辑器来使用。
import tkinter as tk
root=tk.Tk()
text=tk.Text(root,width=30,height=10)
#INSERT索引表示在光标处插入
text.insert(tk.INSERT,'中间')
text.insert(tk.END,'最后')
text.pack()
#清除text控件的所有内容
#text.delete(1.0,tk.END)
#获取控件的所有内容
t1=text.get(1.0,tk.END)
#插入图片
#photo=PhotoImage(file='cat.gif')
#text.image_create(END,image=photo)
#插入button按钮
def show():
text.insert(tk.END,'哎呀,我被点了一下')
b1=tk.Button(text,text='点我',command=show).pack()
#在text中创建控件的命令
text.window_create(tk.INSERT,window=b1)
root.mainloop()
6.Scrollbar控件
用于滚动一些控件的可见范围,通常与text,canvas,listbox控件一起使用。
分为垂直滚动条和水平滚动条。
import tkinter as tk
root=tk.Tk()
root.title('scrollbar控件')
s=tk.Scrollbar(root).pack(side=tk.RIGHT,fil=tk.Y)
t=tk.Text(root,height=4,width=50).pack(side=tk.LEFT,fill=tk.Y)
s.config(command=t.yview)
t.config(yscrollcommand=s.set)
quote="""Nothing is impossible to a willing heart"""
t.insert(tk.END,quote)
root.mainloop()
7.Canvas控件
通常用于显示和编辑图形,即绘制线段、圆形和多边形甚至其他控件。
import tkinter as tk
root=tk.Tk()
canvas_width=300
canvas_height=200
def check(canvas,line_distance):
for x in range(line_distance,canvas_width,line_distance):
canvas.create_line(x,0,x,canvas_height,fill='red')
for y in range(line_distance,canvas_height,line_distance):
canvas.create_line(0,y,x,canvas_width,fill='red')
def paint(event):
a='black'
x1,y1=(event.x-1),(event.y-1)
x2,y2=(event.x+1),(event.y+1)
canvas.create_oval(x1,y1,x2,y2,fill=a)
root.title('绘图')
canvas=tk.Canvas(root,width=canvas_width,height=canvas_height)
canvas.pack(expand=tk.YES,fill=tk.BOTH)
#绑定鼠标左键
canvas.bind("<B1-Motion>",paint)
check(canvas,20)
message=tk.Label(root,text='按下左键绘图').pack(side=tk.BOTTOM)
root.mainloop()
import tkinter as tk
from tkinter import messagebox,filedialog,simpledialog,colorchooser
import tkinter.tix as tix
#将窗口移动到中央
def setplace(window,x,y):
width=window.winfo_width()
height=window.winfo_height()
window.geometry('{}x{}+{}+{}'.format(width,height,x,y))
def run(w):
top=tix.Label(w,padx=20,pady=20,text='tixbuttonbox的使用\n')
box=tix.ButtonBox(w)
def quit():
result=messagebox.askyesno('提示','真的要结束吗?')
if result==True:
w.destroy()
box.add('ok',text='OK',underline=1,width=5,command=w.destroy)
box.add('quit',text='QUIT',underline=1,width=5,command=quit)
box.pack(side=tix.BOTTOM,fill=tix.X)
top.pack(side=tix.TOP,fill=tix.BOTH,expand=1)
def run2(w):
top=tix.Label(w,padx=20,pady=20,text='tixbuttonbox的使用\n')
box=tix.ButtonBox(w)
def quit():
result=messagebox.askyesno('提示','真的要结束吗?')
if result==True:
w.destroy()
box.add('ok',text='确定',underline=0,width=5,command=w.destroy)
box.add('quit',text='取消',underline=0,width=5,command=quit)
box.pack(side=tix.BOTTOM,fill=tix.X)
top.pack(side=tix.TOP,fill=tix.BOTH,expand=1)
if __name__=='__main__':
root=tix.Tk()
run(root)
top=tix.Toplevel(root)
run2(top)
root.update()
root.title('tix.ButtonBox演示')
setplace(root,100,100)
top.title('tix演示')
setplace(top,200, 200)
root.mainloop()