python使用tkinter库,封装操作excel为GUI程序

简介: python使用tkinter库,封装操作excel为GUI程序

首先是完整程序

import xlwings as xws  # 操作excel库
import tkinter as tk  # Gui库
import random   # 随机数
def create():
    # 获取编辑框的数据
    if len(ebox_min.get()) == 0:
        temperature_min = 36.0
    else:
        temperature_min = float(ebox_min.get())
    if len(ebox_max.get()) == 0:
        temperature_max = 37.0
    else:
        temperature_max = float(ebox_max.get())
    if len(ebox_month.get()) == 0:
        month = "8月"
    else:
        month = str(ebox_month.get()) + "月"
    if len(ebox_start_day.get()) == 0:
        day_min = 1
    else:
        day_min = int(ebox_start_day.get())
    if len(ebox_stop_day.get()) == 0:
        day_max = 31
    else:
        day_max = int(ebox_stop_day.get())
    if len(ebox_save.get()) == 0:
        file_name = "demo.xlsx"
    else:
        file_name = str(ebox_save.get())
    app = xws.App(visible=True, add_book=False)
    wb = app.books.add()
    sht = wb.sheets["sheet1"]
    sht.range("a1").value = ["日期", "早", "中", "晚"]
    lst = []
    for it in range(day_min, day_max):
        temp = month + str(it) + "日"
        lst.append(temp)
    sht.range("a2").options(transpose=True).value = lst
    lst.clear()
    for it in range(day_min, day_max):
        temp = random.uniform(temperature_min, temperature_max)
        lst.append(round(temp, 1))
    sht.range("b2").options(transpose=True).value = lst
    lst.clear()
    for it in range(day_min, day_max):
        temp = random.uniform(temperature_min, temperature_max)
        lst.append(round(temp, 1))
    sht.range("c2").options(transpose=True).value = lst
    lst.clear()
    for it in range(day_min, day_max):
        temp = random.uniform(temperature_min, temperature_max)
        lst.append(round(temp, 1))
    sht.range("d2").options(transpose=True).value = lst
    wb.save(file_name + ".xlsx")
    wb.close()
    app.quit()
if __name__ == "__main__":
    # 主窗口名称
    root = tk.Tk()
    # 标题
    root.title('随机体温表生成工具')
    # 关于窗口的设置:窗口居中显示、大小不可调整
    root.resizable(0, 0)  # 阻止Python GUI的大小调整
    width, height = [400, 230]
    scr_width, scr_height = root.maxsize()
    align_str = '%dx%d+%d+%d' % (width, height,
                                 (scr_width-width)/2, (scr_height-height)/2)
    root.geometry(align_str)
    # 按钮的放置
    bt_start = tk.Button(text="生成", command=create, bd=3)
    bt_start.place(x=310, y=65, width=80, height=100)
    # 显示lable的放置
    month_label = tk.Label(root, text="月份", width=10, height=2)
    month_label.place(x=-5, y=20)
    day_label = tk.Label(root, text="日期", width=10, height=2)
    day_label.place(x=-5, y=70)
    file_label = tk.Label(root, text="名字", width=10, height=2)
    file_label.place(x=-5, y=120)
    _label_1 = tk.Label(root, text="<-->", width=10, height=2)
    _label_1.place(x=150, y=70)
    minmax_label = tk.Label(root, text="温度", width=10, height=2)
    minmax_label.place(x=-5, y=170)
    _label_2 = tk.Label(root, text="<-->", width=10, height=2)
    _label_2.place(x=150, y=170)
    # 月份按钮编辑框
    ebox_month = tk.StringVar()
    month_entry = tk.Entry(root, textvariable=ebox_month, bd=2, justify="center",
                           highlightcolor='red', highlightthickness=1)
    month_entry.place(x=80, y=20, width=220, height=40)
    # 日期按钮开始编辑框
    ebox_start_day = tk.StringVar()
    start_day_entry = tk.Entry(
        root, textvariable=ebox_start_day, bd=2, justify="center", highlightcolor='red', highlightthickness=1)
    start_day_entry.place(x=80, y=70, width=80, height=40)
    # 日期按钮结束编辑框
    ebox_stop_day = tk.StringVar()
    stop_day_entry = tk.Entry(
        root, textvariable=ebox_stop_day, bd=2, justify="center", highlightcolor='red', highlightthickness=1)
    stop_day_entry.place(x=220, y=70, width=80, height=40)
    # 保存名字编辑框
    ebox_save = tk.StringVar()
    save_entry = tk.Entry(root, textvariable=ebox_save, bd=2,
                          justify="center", highlightcolor='red', highlightthickness=1)
    save_entry.place(x=80, y=120, width=220, height=40)
    # 温度最小数值编辑框
    ebox_min = tk.StringVar()
    min_entry = tk.Entry(
        root, textvariable=ebox_min, bd=2, justify="center", highlightcolor='red', highlightthickness=1)
    min_entry.place(x=80, y=170, width=80, height=40)
    # 温度最大编辑框
    ebox_max = tk.StringVar()
    max_entry = tk.Entry(
        root, textvariable=ebox_max, bd=2, justify="center", highlightcolor='red', highlightthickness=1)
    max_entry.place(x=220, y=170, width=80, height=40)
    # 默认参数
    ebox_max.set("37.1")
    ebox_min.set("36.0")
    ebox_month.set("8")
    ebox_save.set("体温表")
    ebox_start_day.set("1")
    ebox_stop_day.set("31")
    # 窗体循环
    root.mainloop()

GUI部分:

import tkinter as tk  # Gui库
if __name__ == "__main__":
    # 主窗口名称
    root = tk.Tk()
    # 标题
    root.title('随机体温表生成工具')
    # 关于窗口的设置:窗口居中显示、大小不可调整
    root.resizable(0, 0)  # 阻止Python GUI的大小调整
    width, height = [400, 230]
    scr_width, scr_height = root.maxsize()
    align_str = '%dx%d+%d+%d' % (width, height,
                                 (scr_width-width)/2, (scr_height-height)/2)
    root.geometry(align_str)
    # 按钮的放置
    bt_start = tk.Button(text="生成", command=create, bd=3)
    bt_start.place(x=310, y=65, width=80, height=100)
    # 显示lable的放置
    month_label = tk.Label(root, text="月份", width=10, height=2)
    month_label.place(x=-5, y=20)
    day_label = tk.Label(root, text="日期", width=10, height=2)
    day_label.place(x=-5, y=70)
    file_label = tk.Label(root, text="名字", width=10, height=2)
    file_label.place(x=-5, y=120)
    _label_1 = tk.Label(root, text="<-->", width=10, height=2)
    _label_1.place(x=150, y=70)
    minmax_label = tk.Label(root, text="温度", width=10, height=2)
    minmax_label.place(x=-5, y=170)
    _label_2 = tk.Label(root, text="<-->", width=10, height=2)
    _label_2.place(x=150, y=170)
    # 月份按钮编辑框
    ebox_month = tk.StringVar()
    month_entry = tk.Entry(root, textvariable=ebox_month, bd=2, justify="center",
                           highlightcolor='red', highlightthickness=1)
    month_entry.place(x=80, y=20, width=220, height=40)
    # 日期按钮开始编辑框
    ebox_start_day = tk.StringVar()
    start_day_entry = tk.Entry(
        root, textvariable=ebox_start_day, bd=2, justify="center", highlightcolor='red', highlightthickness=1)
    start_day_entry.place(x=80, y=70, width=80, height=40)
    # 日期按钮结束编辑框
    ebox_stop_day = tk.StringVar()
    stop_day_entry = tk.Entry(
        root, textvariable=ebox_stop_day, bd=2, justify="center", highlightcolor='red', highlightthickness=1)
    stop_day_entry.place(x=220, y=70, width=80, height=40)
    # 保存名字编辑框
    ebox_save = tk.StringVar()
    save_entry = tk.Entry(root, textvariable=ebox_save, bd=2,
                          justify="center", highlightcolor='red', highlightthickness=1)
    save_entry.place(x=80, y=120, width=220, height=40)
    # 温度最小数值编辑框
    ebox_min = tk.StringVar()
    min_entry = tk.Entry(
        root, textvariable=ebox_min, bd=2, justify="center", highlightcolor='red', highlightthickness=1)
    min_entry.place(x=80, y=170, width=80, height=40)
    # 温度最大编辑框
    ebox_max = tk.StringVar()
    max_entry = tk.Entry(
        root, textvariable=ebox_max, bd=2, justify="center", highlightcolor='red', highlightthickness=1)
    max_entry.place(x=220, y=170, width=80, height=40)
    # 默认参数
    ebox_max.set("37.1")
    ebox_min.set("36.0")
    ebox_month.set("8")
    ebox_save.set("体温表")
    ebox_start_day.set("1")
    ebox_stop_day.set("31")
    # 窗体循环
    root.mainloop()

操作excel部分

import tkinter as tk  # Gui库
import random   # 随机数
def create():
    # 获取编辑框的数据
    if len(ebox_min.get()) == 0:
        temperature_min = 36.0
    else:
        temperature_min = float(ebox_min.get())
    if len(ebox_max.get()) == 0:
        temperature_max = 37.0
    else:
        temperature_max = float(ebox_max.get())
    if len(ebox_month.get()) == 0:
        month = "8月"
    else:
        month = str(ebox_month.get()) + "月"
    if len(ebox_start_day.get()) == 0:
        day_min = 1
    else:
        day_min = int(ebox_start_day.get())
    if len(ebox_stop_day.get()) == 0:
        day_max = 31
    else:
        day_max = int(ebox_stop_day.get())
    if len(ebox_save.get()) == 0:
        file_name = "demo.xlsx"
    else:
        file_name = str(ebox_save.get())
    app = xws.App(visible=True, add_book=False)
    wb = app.books.add()
    sht = wb.sheets["sheet1"]
    sht.range("a1").value = ["日期", "早", "中", "晚"]
    lst = []
    for it in range(day_min, day_max):
        temp = month + str(it) + "日"
        lst.append(temp)
    sht.range("a2").options(transpose=True).value = lst
    lst.clear()
    for it in range(day_min, day_max):
        temp = random.uniform(temperature_min, temperature_max)
        lst.append(round(temp, 1))
    sht.range("b2").options(transpose=True).value = lst
    lst.clear()
    for it in range(day_min, day_max):
        temp = random.uniform(temperature_min, temperature_max)
        lst.append(round(temp, 1))
    sht.range("c2").options(transpose=True).value = lst
    lst.clear()
    for it in range(day_min, day_max):
        temp = random.uniform(temperature_min, temperature_max)
        lst.append(round(temp, 1))
    sht.range("d2").options(transpose=True).value = lst
    wb.save(file_name + ".xlsx")
    wb.close()
    app.quit()

操作excel使用的是xlwings库,具体介绍请参考我的其他博文:链接直达

成品展示


相关文章
|
4天前
|
Python
使用Python的openpyxl库
【5月更文挑战第17天】使用Python的openpyxl库
14 2
|
23小时前
|
机器学习/深度学习 算法 前端开发
2024年8个可以提高数据科学工作效率并节省宝贵时间的Python库,2024年最新记得把每一次面试当做经验积累
2024年8个可以提高数据科学工作效率并节省宝贵时间的Python库,2024年最新记得把每一次面试当做经验积累
2024年8个可以提高数据科学工作效率并节省宝贵时间的Python库,2024年最新记得把每一次面试当做经验积累
|
1天前
|
数据采集 数据安全/隐私保护 Python
2024年最新【Python】如何用Python来操作PDF文件,收藏(2),2024年最新阿里p7Python面试题
2024年最新【Python】如何用Python来操作PDF文件,收藏(2),2024年最新阿里p7Python面试题
2024年最新【Python】如何用Python来操作PDF文件,收藏(2),2024年最新阿里p7Python面试题
|
3天前
|
关系型数据库 Java 分布式数据库
实时计算 Flink版操作报错合集之在使用 Python UDF 时遇到 requests 包的导入问题,提示 OpenSSL 版本不兼容如何解决
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
20 5
|
3天前
|
网络协议 数据处理 调度
深入探索Python异步编程:asyncio库的应用与实践
在现代软件开发中,异步编程已成为处理并发和I/O密集型任务的重要策略。本文将带您深入探索Python的asyncio库,解析其背后的设计原理,并通过实例展示如何在实际项目中应用asyncio实现高效的异步编程。我们不仅会探讨asyncio的基本用法,还会分析其性能优势,并探讨其与其他并发模型的比较。此外,文章还将涵盖asyncio在Web开发、网络编程和数据处理等场景中的应用案例,帮助您更好地理解并掌握这一强大的异步编程工具。
|
4天前
|
程序员 Python
tesseract库的安装与使用及在python中使用,Python程序员秋招三面蚂蚁金服
tesseract库的安装与使用及在python中使用,Python程序员秋招三面蚂蚁金服
|
4天前
|
Python
Python基础教程: math库常用函数(1),Python这些高端技术只有你还不知道
Python基础教程: math库常用函数(1),Python这些高端技术只有你还不知道
|
4天前
|
Java Apache 索引
POI操作大全(动态合并单元格,为单元格生成一个自定义的数据显示格式,自定义公式计算结果生成,读取excel,word文件在生成图片,word指定位置生成图片)
POI操作大全(动态合并单元格,为单元格生成一个自定义的数据显示格式,自定义公式计算结果生成,读取excel,word文件在生成图片,word指定位置生成图片)
|
4天前
|
数据可视化 Python
Python----matplotlib库
Python----matplotlib库
12 1
|
6天前
|
Java
java导出复杂excel
java导出复杂excel
http://www.vxiaotou.com