【Python】实验四 Python面向对象与图形界面编程

实验四 Python面向对象与图形界面编程

一、实验目的:
1、初步理解面向对象的思想,掌握python中类的定义
2、掌握tkinter中各常用组件的使用及其布局方式
3、掌握tkinter面向对象编程中的技术
二、实验要求:
1、要求有任务独立在计算独立完成实验任务
2、每题前面要有注释说明班级、学号姓名、以及题目说明
3、所有编写程序要求编程界面友好,互动性好。
4、课程结束前在学习通提交课内完成任务的代码,课后补充完成本实验报告,并且按照如下格式命名(专业班级学号姓名)
实验四Python面向对象与图形界面编程实验报告-软工20202-01-01姓名.docx
三、实验内容:
编写程序:读取指EXCEL
students.xlsx
中的学生信息,按python中面向对象的思想在tkinter中实现查询所有学生和按班级查询学生。
四、编程实现
设计思路(各方法中的设计步骤):
1.导入Tkinter模块
2. 创建一个窗口window
3. 生成一个标签label
4. 生成一个按钮button
5. 将label,button放在窗口上
6.使用open_workbook读取指定excle

实验代码:

import xlrd
import tkinter as tk
from tkinter import ttk

window = tk.Tk()
window.title("学生信息查询")
window.geometry("700x700")
columns = tuple(['学号', '班级', '姓名', '密码', '性别', '出生日期'])

tree = ttk.Treeview(window, height=23, show="headings", selectmode='browse', columns=columns)
class_name = tk.StringVar()
entry_class_name = tk.Entry(window, width=12, textvariable=class_name)
entry_class_name.place(x=270, y=40)

def getall():
    List = []
    with xlrd.open_workbook('students.xlsx') as workbook:
        table = workbook.sheets()[0]
        for i in range(1, table.nrows):
            rowList = table.row_values(i)
            List.append(rowList)
    return List

def classgetall():
    List = []
    with xlrd.open_workbook('students.xlsx') as workbook:
        table = workbook.sheets()[0]
        for i in range(1, table.nrows):
            rowList = table.row_values(i)
            # print(rowList)
            # str = '2020-2'
            str = entry_class_name.get()
            print(str)
            if (rowList[1] == str):
                List.append(rowList)
    return List

def showall():
    info = getall()
    print(info)
    global tree
    for i in range(len(info)):
        tree.insert('', 'end', values=info[i])

def showclass():
    global tree
    info = classgetall()
    print(info)
    for i in range(len(info)):
        tree.insert('', 'end', values=info[i])

tk.Label(window, text='周心怡-2020211001000104').place(x=120, y=5)

tk.Label(window, text='输入班级').place(x=200, y=40)

btn_login = tk.Button(window, text='点击查询', width=6, command=showall())
btn_login.place(x=380, y=40)

btn_login = tk.Button(window, text='查询所有', width=6, command=showclass())
btn_login.place(x=600, y=40)

tree.column(columns[0], width=130)
tree.column(columns[1], width=110)
tree.column(columns[2], width=110)
tree.column(columns[3], width=110)
tree.column(columns[4], width=50)
tree.column(columns[5], width=130)

for i in columns:
    tree.heading(i, text=i)
tree.grid(row=0, column=0, padx=10, pady=70, sticky='nsew')

window.mainloop()

运行截图:
在这里插入图片描述

五、实验总结(不足与收获)
收获:1.能够更加熟练地运用Tkinter模块
2.学习到表格树的部分用法

不足:1.对各类函数的调用仍然不够熟悉
2.函数中有部分累赘的地方,后期得对齐进行优化

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

almostspring

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

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

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

打赏作者

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

抵扣说明:

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

余额充值