python tkinter窗口的定义和基本相关参数Lable,Button,Entry

直接看代码吧

窗口类的相关设置
import tkinter


# 主窗口的类,使用root接收一下
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)
# 如果是true代表可以拉伸 false 不可拉伸
root.resizable(width=True, height=False)
# 主窗体一直循环
root.mainloop()

Lable组件的相关应用
import tkinter

# 主窗口的类,使用root接收一下
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)
# 如果是true代表可以拉伸 false 不可拉伸
root.resizable(width=True, height=False)
# 定义lable
'''
    Label参数:
        root:绑定主窗口,
        text:显示的文本内容,
        width:字的宽度是60,
        height:字高度为4,
        bg:背景色,
        fg:前景色-文字颜色,
        font:字体设置(字体,字体大小),
        anchot:方向
        anchor控制你文本的在文本组件中位置N,NE,,S,SW,W,NW,或CENTER来定位(EWSN代表东西南北,上北下南左西右东)默认值是CENTER
'''
# 设置第一个lable
l1 = tkinter.Label(root, text='我是一个可以显示文本内容的组件', width=60, height=4, bg='yellow', fg='blue',
                   font=('宋体', 12), anchor=tkinter.W)
# 第一个lable显示
l1.pack()
# 字符串类型变量设置
var = tkinter.StringVar()
# 给字符串变量赋值
var.set("大家好,我是一个可变的变量,是关于文本类型")
# 设置第二个lable
l2 = tkinter.Label(root, textvariable=var, width=60, height=4, bg='blue', fg='white', font=('宋体', 12))
# 设置第二个lable显示
l2.pack()
# lable图片的显示
# 先定义图片对象指向他
image1 = tkinter.PhotoImage(file='../src/xiaolan.gif')
# 设置图片lable
l3 = tkinter.Label(root, image=image1)
# 设置第三个lable显示
l3.pack()
# 设置第三个lable显示
l3.pack()
# 主窗体一直循环
root.mainloop()

button组件运用
import tkinter

# 主窗口的类,使用root接收一下
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)
# 如果是true代表可以拉伸 false 不可拉伸
root.resizable(width=True, height=False)
# 定义lable
'''
    Label参数:
        root:绑定主窗口,
        text:显示的文本内容,
        width:字的宽度是60,
        height:字高度为4,
        bg:背景色,
        fg:前景色-文字颜色,
        font:字体设置(字体,字体大小),
        anchot:方向
        anchor控制你文本的在文本组件中位置N,NE,,S,SW,W,NW,或CENTER来定位(EWSN代表东西南北,上北下南左西右东)默认值是CENTER
'''
# 设置第一个lable
l1 = tkinter.Label(root, text='我是一个可以显示文本内容的组件', width=60, height=4, bg='yellow', fg='blue',
                   font=('宋体', 12), anchor=tkinter.W)
# 第一个lable显示
l1.pack()
# 字符串类型变量设置
var = tkinter.StringVar()
# 给字符串变量赋值
var.set("大家好,我是一个可变的变量,是关于文本类型")
# 设置第二个lable
l2 = tkinter.Label(root, textvariable=var, width=60, height=4, bg='blue', fg='white', font=('宋体', 12))
# 设置第二个lable显示
l2.pack()
# lable图片的显示
# 先定义图片对象指向他
image1 = tkinter.PhotoImage(file='../src/xiaolan.gif')
# 设置图片lable
l3 = tkinter.Label(root, image=image1)
# 设置第三个lable显示
l3.pack()
# 设置第三个lable显示
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))
# 窗口布局管理,试组件放到指定的位置,相对于左上角(0,0)的坐标
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)

# show定义输入内容显示替换,这里替换成*
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('登录失败')


# b2 = tkinter.Button(root, text='登录', width=10, command=login)
# b2.place(x=100, y=120)

# insert 代表光标的位置
def insert():
    e_name.insert('insert', 'abc')


# b2 = tkinter.Button(root, text='在光标处插入', width=10, command=insert)
# b2.place(x=100, y=100)


# end 表示末尾
def end_insert():
    e_name.insert('end', 'ABC')


# b2 = tkinter.Button(root, text='在末尾插入', width=10, command=end_insert)
# b2.place(x=100, y=100)


# 从头开始删除-删除全部
def del_all():
    e_name.delete(0, 'end')


# b2 = tkinter.Button(root, text='删除全部', width=10, command=del_all)
# b2.place(x=100, y=100)


# 删除第一个
def del_first():
    e_name.delete(0)


# b2 = tkinter.Button(root, text='删除第一个', width=10, command=del_first)
# b2.place(x=100, y=100)


# 从指定位置开始删除 从第几个开始删到第几个,不包括地一个
def del_other():
    e_name.delete(1, 4)


# b2 = tkinter.Button(root, text='删除指定位置', width=10, command=del_other)
# b2.place(x=100, y=100)


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))
# 窗口布局管理,试组件放到指定的位置,相对于左上角(0,0)的坐标
l_name.place(x=20, y=20)

l_pwd = tkinter.Label(root, text='密 码', font=('宋体', 14))
l_pwd.place(x=20, y=60)


# 鼠标失去焦点的时候验证 返回True 或 False
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()

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lanlnan抱抱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值