直接看代码吧
窗口类的相关设置
import tkinter
root = tkinter.Tk()
root.title("My table")
width, height = 300, 400
width_max, height_max = root.maxsize()
s_center = '%dx%d+%d+%d' % (width, height, (width_max - width) / 2, (height_max - height) / 2)
print(s_center)
root.geometry(s_center)
root.resizable(width=True, height=False)
root.mainloop()
Lable组件的相关应用
import tkinter
root = tkinter.Tk()
root.title("My table")
width, height = 600, 400
width_max, height_max = root.maxsize()
s_center = '%dx%d+%d+%d' % (width, height, (width_max - width) / 2, (height_max - height) / 2)
root.geometry(s_center)
root.resizable(width=True, height=False)
'''
Label参数:
root:绑定主窗口,
text:显示的文本内容,
width:字的宽度是60,
height:字高度为4,
bg:背景色,
fg:前景色-文字颜色,
font:字体设置(字体,字体大小),
anchot:方向
anchor控制你文本的在文本组件中位置N,NE,,S,SW,W,NW,或CENTER来定位(EWSN代表东西南北,上北下南左西右东)默认值是CENTER
'''
l1 = tkinter.Label(root, text='我是一个可以显示文本内容的组件', width=60, height=4, bg='yellow', fg='blue',
font=('宋体', 12), anchor=tkinter.W)
l1.pack()
var = tkinter.StringVar()
var.set("大家好,我是一个可变的变量,是关于文本类型")
l2 = tkinter.Label(root, textvariable=var, width=60, height=4, bg='blue', fg='white', font=('宋体', 12))
l2.pack()
image1 = tkinter.PhotoImage(file='../src/xiaolan.gif')
l3 = tkinter.Label(root, image=image1)
l3.pack()
l3.pack()
root.mainloop()
button组件运用
import tkinter
root = tkinter.Tk()
root.title("My table")
width, height = 600, 400
width_max, height_max = root.maxsize()
s_center = '%dx%d+%d+%d' % (width, height, (width_max - width) / 2, (height_max - height) / 2)
root.geometry(s_center)
root.resizable(width=True, height=False)
'''
Label参数:
root:绑定主窗口,
text:显示的文本内容,
width:字的宽度是60,
height:字高度为4,
bg:背景色,
fg:前景色-文字颜色,
font:字体设置(字体,字体大小),
anchot:方向
anchor控制你文本的在文本组件中位置N,NE,,S,SW,W,NW,或CENTER来定位(EWSN代表东西南北,上北下南左西右东)默认值是CENTER
'''
l1 = tkinter.Label(root, text='我是一个可以显示文本内容的组件', width=60, height=4, bg='yellow', fg='blue',
font=('宋体', 12), anchor=tkinter.W)
l1.pack()
var = tkinter.StringVar()
var.set("大家好,我是一个可变的变量,是关于文本类型")
l2 = tkinter.Label(root, textvariable=var, width=60, height=4, bg='blue', fg='white', font=('宋体', 12))
l2.pack()
image1 = tkinter.PhotoImage(file='../src/xiaolan.gif')
l3 = tkinter.Label(root, image=image1)
l3.pack()
l3.pack()
root.mainloop()
Entry组件
import tkinter
root = tkinter.Tk()
root.title('登录窗口')
width, height = 300, 150
width_max, height_max = root.maxsize()
s_center = '%dx%d+%d+%d' % (width, height, (width_max - width)/2, (height_max - height)/2)
root.geometry(s_center)
root.resizable(width=False, height=False)
l_name = tkinter.Label(root, text='用户名', font=('宋体', 14))
l_name.place(x=20, y=20)
l_pwd = tkinter.Label(root, text='密 码', font=('宋体', 14))
l_pwd.place(x=20, y=60)
e_name = tkinter.Entry(root, width=20)
e_name.place(x=100, y=20)
e_pwd = tkinter.Entry(root, width=20, show='*')
e_pwd.place(x=100, y=60)
def get_password():
print(type(e_name.get()))
print(e_pwd.get())
b1 = tkinter.Button(root, text='获取密码', width=10, command=get_password)
b1.place(x=100, y=100)
def login():
print(e_name.get())
print(e_pwd.get())
if e_name.get() == str('admin') and e_pwd().get() == '123456':
print('登录成功')
else:
print('登录失败')
def insert():
e_name.insert('insert', 'abc')
def end_insert():
e_name.insert('end', 'ABC')
def del_all():
e_name.delete(0, 'end')
def del_first():
e_name.delete(0)
def del_other():
e_name.delete(1, 4)
root.mainloop()
一些Entry的验证
import tkinter
root = tkinter.Tk()
root.title('登录窗口')
width, height = 500, 150
width_max, height_max = root.maxsize()
s_center = '%dx%d+%d+%d' % (width, height, (width_max - width) / 2, (height_max - height) / 2)
root.geometry(s_center)
root.resizable(width=False, height=False)
l_name = tkinter.Label(root, text='用户名', font=('宋体', 14))
l_name.place(x=20, y=20)
l_pwd = tkinter.Label(root, text='密 码', font=('宋体', 14))
l_pwd.place(x=20, y=60)
def is_name():
if e_name.get() == 'admin':
print('输入正确')
return True
else:
print('输入错误,已删除')
e_name.delete(0, 'end')
return False
'''
validate
值:含义
focus:当Entry组件获得或失去焦点的时候验证
focusin 获得组件焦点的时候验证
focusout 失去组件焦点的时候验证
key 当输入框被编辑的时候验证
all 出现上述任何一种情况的时候验证
none 1 关闭验证功能
'''
e_name = tkinter.Entry(root, validate='focusout', validatecommand=is_name, width=20)
e_name.place(x=100, y=20)
var = tkinter.StringVar()
e_pd = tkinter.Entry(root, textvariable=var, width=20, show='*')
e_pd.place(x=100, y=60)
root.mainloop()