import tkinter as tk
def add_student():
result.set('增加学生信息')
def delete_student():
result.set('删除学生信息')
def change_student():
result.set('修改学生信息')
def show_student():
result.set('查看学生信息')
if __name__ == '__main__':
# 以下代码全部为gui界面的初始化
# 第1步,实例化对象,建立窗口window
window = tk.Tk()
# 第2步,给窗口的可视化起名字
window.title('学生信息管理系统 V2.1')
# 第3步,设定窗口的大小(长 * 宽)
window.geometry('500x650')
# tk.StringVar()用于接收用户输入
result = tk.StringVar()
result.set(" ")
# ①关于姓名的 label 和 entry
textName = tk.StringVar()
textName.set("")
labelLine1 = tk.Label(window, text="姓 名:", font=('Arial', 15), width=10).place(x=75, y=50, anchor='nw')
entryLine1 = tk.Entry(window, show=None, font=('宋体', 15), textvariable=textName, width=20)
entryLine1.place(x=190, y=50, anchor='nw') # 显示成明文形式
# ②关于性别的 label 和 entry
textSex = tk.StringVar()
textSex.set("")
labelLine2 = tk.Label(window, text="性 别:", font=('Arial', 15), width=10)
labelLine2.place(x=75, y=100, anchor='nw')
entryLine2 = tk.Entry(window, show=None, font=('Arial', 15), textvariable=textSex, width=18)
entryLine2.place(x=190, y=100, anchor='nw')
# ③关于电话的 label 和 entry
textPhone = tk.StringVar()
textPhone.set("")
labelLine3 = tk.Label(window, text="电 话:", font=('Arial', 15), width=10).place(x=75, y=150, anchor='nw')
entryLine3 = tk.Entry(window, show=None, font=('Arial', 15), textvariable=textPhone, width=18)
entryLine3.place(x=190, y=150, anchor='nw')
# 关于"添加"组件,此处绑定函数add_student用于添加学生信息
button1_add = tk.Button(window, text='添 加', bg='silver', font=('Arial', 12), command=add_student, width=8)
button1_add.place(x=40, y=220, anchor='nw')
# 关于"删除"组件,此处绑定函数delete_student用于删除学生信息
button2_delete = tk.Button(window, text='删 除', bg='silver', font=('Arial', 12), command=delete_student, width=8)
button2_delete.place(x=150, y=220, anchor='nw')
# 关于"修改"组件,此处绑定函数change_student用于修改学生信息
button3_change = tk.Button(window, text='修 改', bg='silver', font=('Arial', 12), command=change_student, width=8)
button3_change.place(x=260, y=220, anchor='nw')
# 关于"显示"组件,此处绑定函数show_student用于显示学生信息
button4_show = tk.Button(window, text='显 示', bg='silver', font=('Arial', 12), command=show_student, width=8)
button4_show.place(x=370, y=220, anchor='nw')
# 下边两行代码是在gui界面显示我的作者信息
labelLine_Name = tk.Label(window, text="@Author:", font=('宋体', 13), width=10).place(x=130, y=260, anchor='nw')
labelLine_myID = tk.Label(window, text="飞飞", font=('宋体', 13), width=10).place(x=250, y=260, anchor='nw')
show_result = tk.Label(window, bg="white", fg="black", font=("宋体", 12), bd='0', anchor='nw', textvariable=result)
show_result.place(x="25", y="300", width="450", height="300")
window.protocol('WM_DELETE_WINDOW', custom_over) # 重写关闭
window.mainloop() # 显示主窗口
窗口创建和设置方法 | 注释 |
---|
window = tk.Tk() | 生成窗口 |
window.title('标题名') | 修改窗口标题名字 |
window.geometry('500x650') | 改变窗体大小,注意:(‘宽x高’)中x不是* |
window.mainloop() | 显示窗口 |
place()组件参数 | 注释 |
---|
x | 组件左上角的x坐标 |
y | 组件左上角的y坐标 |
anchor | 组件对齐方式 |
width | 组件的宽度 |
height | 组件的高度 |
anchor参数值 | 注释 |
---|
n | 北 |
s | 南 |
w | 西 |
e | 东 |
center | 中心 |
nw | 西北 |
ne | 东北 |
sw | 西南 |
se | 东南 |
tk核心组件 | 注释 | 说明 |
---|
Label | 标签 | 用来显示文字或图片 |
Entry | 单行文字域 | 用来收集键盘输入 |
Button | 按钮 | 类似标签,但提供额外功能,比如鼠标/键盘事件 |
Label(根对象,[属性]) | 注释 |
---|
text | 标签名称 |
font | 字体(样式,大小) |
bg | 背景颜色(标签颜色) |
fg | 前景颜色(字体颜色) |
width | 标签宽度 |
height | 标签高度 |
anchor | 锚选项,控制标签文本位置,参数同上 |