设备管理系统-基于Tkinter的小案例

基于Tkinter,做了一个简易的设备管理系统。

文件格式

检验文件.txt 文件格式如下:
在这里插入图片描述

状态获取.txt 文件格式如下:

机器名称:测试4号	   220V	   2.5A

代码如下

import tkinter as tk
import tkinter.messagebox as msg
import re


Machines_list = ['机器名称', '额定电压', '额定电流', '故障原因', '故障名称', '故障地点']
# 检验文件路径
filepath = '.\检验文件.txt'
# 状态文件
infile = '.\状态获取.txt'

str_print = "{}\t {}V\t {}A\t {}\t {}\t {}\n"

# 字体设置
font = ('Arial', 14)


def clear():
    # 清空框架
    for widget in frame.winfo_children():
        widget.destroy()


def write_information(entrys):
    """
    信息写入
    Arguments:
        frame {[type]} -- [description]
        entrys {[type]} -- [description]
    """
    infos = []
    for entry in entrys:
        infos.append(entry.get())

    # 解包
    n_g, u_g, i_g, h_g, r_g, k_g = infos
    with open(filepath, 'a', encoding='utf-8') as file_object:
        file_object.write(str_print.format(n_g, u_g, i_g, h_g, r_g, k_g))

    # 提示保存成功
    msg.showinfo('success', '保存成功')


def input_information():
    """
    信息输入
    """
    clear()
    # 要保存的信息
    entrys = []
    # 录入信息
    for row, info in enumerate(Machines_list):
        tk.Label(frame, text=info, font=font).grid(row=row, column=0)

        entry = tk.Entry(frame, font=font)
        entry.grid(row=row, column=1)
        entrys.append(entry)

    # 确认按钮
    btn_comfirm = tk.Button(
        frame, text='录入', command=lambda: write_information(entrys))
    btn_comfirm.grid(row=6, column=2)


def show_information():
    """
    展示机器信息
    """
    clear()
    str_print = "机器名称:{}\t额定电压:{}\t额定电流:{}\t故障原因:{}\t故障名称:{}\t故障地点:{}"
    with open(filepath, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        lines = lines[1:]
        for row, line in enumerate(lines):
            # 跳过空行
            line = line.strip()
            if not line:
                continue
            infos = line.split('\t')
            tk.Label(frame, text=str_print.format(
                infos[0], infos[1], infos[2], infos[3], infos[4], infos[5]), font=font).grid(row=row, column=0)


def read_information(entry, res_frame):
    # 清空res_frame
    for widget in res_frame.winfo_children():
        widget.destroy()

    # 查询关键字
    key = entry.get()
    flag = False
    row = 2
    str_print = "{}\t {}\t {}\t {}\t {}\t {}"
    with open(filepath, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        lines = lines[1:]
        for line in lines:
            infos = line.split('\t')
            infos = list(map(lambda x: x.strip(), infos))
            if key in infos:
                tk.Label(res_frame, text=str_print.format(
                    infos[0], infos[1], infos[2], infos[3], infos[4], infos[5]), font=font).grid(row=row, column=0)
                flag = True
                row += 1
        if flag:
            # 提示查询成功
            msg.showinfo('success', '成功检索到您输入的机器信息')
        else:
            msg.showinfo('failed', '未检索到您输入的机器信息')


def search_information():
    clear()

    tk.Label(frame, text='请输入需要查询的关键字', font=font).grid(row=0, column=0)
    entry = tk.Entry(frame, font=font)
    entry.grid(row=0, column=1)

    # 查询结果显示框架
    res_frame = tk.Frame(frame, height=400, width=400)
    res_frame.grid(row=2, column=1)
    # 确认按钮
    btn_comfirm = tk.Button(
        frame, text='查询', command=lambda: read_information(entry, res_frame))
    btn_comfirm.grid(row=1, column=1)


def drop_information(entry, res_frame):
    # 清空res_frame
    for widget in res_frame.winfo_children():
        widget.destroy()

    # 删除关键字
    key = entry.get()
    flag = False
    row = 2

    with open(filepath, 'r', encoding='utf-8') as f:
        lines = f.readlines()

    with open(filepath, 'w', encoding='utf-8') as f:
        for i, line in enumerate(lines):
            # 写入表头
            if i < 1:
                f.write(line)
                continue

            infos = line.split('\t')
            if key in infos:
                tk.Label(res_frame, text=str_print.format(
                    infos[0], infos[1], infos[2], infos[3], infos[4], infos[5]), font=font).grid(row=row, column=0)
                flag = True
                row += 1
                continue
            # 写入
            f.write(line)

        if flag:
            # 提示查询成功
            msg.showinfo('success', '成功删除')
        else:
            msg.showinfo('failed', '删除失败,未检索到您输入的机器信息')


def delete_information():
    """
    删除信息
    """
    clear()

    tk.Label(frame, text='请输入需要删除的信息关键字', font=font).grid(row=0, column=0)
    entry = tk.Entry(frame, font=font)
    entry.grid(row=0, column=1)

    # 删除结果显示框架
    res_frame = tk.Frame(frame, height=400, width=400)
    res_frame.grid(row=2, column=1)
    # 确认按钮
    btn_comfirm = tk.Button(
        frame, text='删除', command=lambda: drop_information(entry, res_frame))
    btn_comfirm.grid(row=1, column=1)


def save_information(entrys, line_numbers):

    with open(filepath, 'r', encoding='utf-8') as f:
        lines = f.readlines()
    # 保存文件
    str_print = "{}\t {}\t {}\t {}\t {}\t {}"
    with open(filepath, 'w', encoding='utf-8') as f:
        # 表头写入
        f.write(lines[0])
        lines = lines[1:]
        for i, line in enumerate(lines):
            if i in line_numbers:
                index = line_numbers.index(i)
                row_entory = entrys[index]
                row = list(map(lambda x: x.get(), row_entory))
                n_g, u_g, i_g, h_g, r_g, k_g = row
                f.write(str_print.format(n_g, u_g, i_g, h_g, r_g, k_g))

            else:
                f.write(line)

    msg.showinfo('success', '修改已完成')
    # 展示信息
    show_information()


def modify(entry, res_frame):
    # 清空res_frame
    for widget in res_frame.winfo_children():
        widget.destroy()

    # 顶头
    for col, info in enumerate(Machines_list):
        tk.Label(res_frame, text=info, font=font).grid(row=0, column=col)

    # 修改关键字
    key = entry.get()
    flag = False
    row = 1

    # 要修改的内容
    entrys = []
    # 在文件中的行数
    line_numbers = []
    with open(filepath, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        lines = lines[1:]
        for i, line in enumerate(lines):
            infos = line.split('\t')
            if key in infos:
                text = tk.StringVar()
                text.set("在文件中的行数{}".format(i))
                line_numbers.append(i)
                entry = tk.Entry(
                    res_frame, textvariable=text, width=10, font=font)
                # 保存每行entory
                row_entory = []
                # 展示查询内容
                for col, info in enumerate(infos):
                    text = tk.StringVar()
                    text.set(info)
                    entry = tk.Entry(
                        res_frame, textvariable=text, width=10, font=font)
                    entry.grid(row=row, column=col)
                    row_entory.append(entry)

                flag = True
                row += 1
                entrys.append(row_entory)
        if not flag:
            # 提示没有信息
            msg.showinfo('failed', '未检索到您输入的机器信息')

   # 确认按钮
    btn_comfirm = tk.Button(
        res_frame, text='确认', command=lambda: save_information(entrys, line_numbers))
    btn_comfirm.grid(row=row, column=1)


def modify_information():
    """
    修改
    """
    clear()

    tk.Label(frame, text='请输入需要修改的机器信息', font=font).grid(row=0, column=0)
    entry = tk.Entry(frame, font=font)
    entry.grid(row=0, column=1)

    # 查询结果显示框架
    res_frame = tk.Frame(frame, height=400, width=400)
    res_frame.grid(row=2, column=0)

    # 按钮
    btn_comfirm = tk.Button(
        frame, text='修改', command=lambda: modify(entry, res_frame))
    btn_comfirm.grid(row=1, column=1)


def loadDatadet():
    # 加载故障文件
    f = open(infile, "r", encoding='utf-8')
    source_in_line = f.readlines()
    dataset = []
    for line in source_in_line:
        temp = re.split('\t|:', line)[1:]
        dataset.append(temp)
    return dataset


def trouble():
    """
    故障检索
    """
    clear()

    m_list = loadDatadet()
    n_g, u_g, i_g = m_list[0]
    u_g = re.findall('\d+', u_g)[0]
    i_g = re.findall('\d+', i_g)[0]
    u_g = float(u_g)
    i_g = float(i_g)

    str_print_1 = "机器名称:{}\t实时电压:{}V\t实时电流:{}A"
    tk.Label(frame, text='实时数据', font=font).grid(row=0, column=0)
    tk.Label(frame, text=str_print_1.format(n_g, u_g, i_g),
             font=font).grid(row=1, column=0)

    tk.Label(frame, text='额定数据', font=font).grid(row=2, column=0)
    tk.Label(frame, text='检索结果', font=font).grid(row=2, column=1)

    row = 3
    flag = False
    str_print_2 = "机器名称:{}\t额定电压:{}V\t额定电流:{}A"
    with open(filepath, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        lines = lines[1:]
        for line in lines:
            infos = line.split('\t')
            # 跳过空行或非对应机器
            if not line.strip() or n_g != infos[0]:
                continue
            u_1 = re.findall('\d+', infos[1])[0]
            i_1 = re.findall('\d+', infos[2])[0]
            u_1 = float(u_1)
            i_1 = float(i_1)
            # 校验
            text = ''
            if u_g * 0.95 <= u_1 <= u_g * 1.05:
                flag = True
                text = text+"\t电压状况良好"
            else:
                text = text+"\t电压出现故障"

            if i_g * 0.95 <= i_1 <= i_g * 1.05:
                flag = True
                text = text+"\t电流状况良好"
            else:
                text = text+"\t电流出现故障"

            tk.Label(frame, text=str_print_2.format(infos[0], u_1, i_1),
                     font=font).grid(row=row, column=0)
            tk.Label(frame, text=text, font=font).grid(row=row, column=1)
            row += 1

    if not flag:
        # 提示信息
        msg.showinfo('info', '没有检索到故障信息')
    else:
        msg.showinfo('info', '故障信息检索完毕')


# 实例化窗口
root = tk.Tk()
root.title("设备管理文件")
# 大小设置
root.geometry('1000x700')

lable = tk.Label(root, text="欢迎进入设备管理系统,2020年6月出品",
                 bg="blue", fg="red", font=("黑体", 20))
lable.pack()

# 框架
frame = tk.Frame(root, height=600, width=1000)
frame.pack()


# 创建顶级菜单
menu = tk.Menu(root)
# 基础操作
basic_menu = tk.Menu(menu)
menu.add_cascade(label='基础操作', menu=basic_menu)
basic_menu.add_command(label='录入本部机器信息', command=input_information)
basic_menu.add_command(label='显示全部机器信息', command=show_information)
basic_menu.add_command(label='查询录入机器信息', command=search_information)
basic_menu.add_command(label='删除录入机器信息', command=delete_information)
basic_menu.add_command(label='修改录入机器信息', command=modify_information)

# 故障检索
malfunction = tk.Menu(menu)
menu.add_cascade(label='故障检索', menu=malfunction)
malfunction.add_command(label='开始检索', command=trouble)


# 显示菜单
root.config(menu=menu)
# 主窗口循环
root.mainloop()

运行效果

在这里插入图片描述

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值