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自动化处理Excel数据:从基础到进阶####
本文旨在为读者提供一个全面的指南,通过Python编程语言实现Excel数据的自动化处理。无论你是初学者还是有经验的开发者,本文都将帮助你掌握Pandas和openpyxl这两个强大的库,从而提升数据处理的效率和准确性。我们将从环境设置开始,逐步深入到数据读取、清洗、分析和可视化等各个环节,最终实现一个实际的自动化项目案例。 ####
634 10
|
5月前
|
测试技术 开发者 Python
Python(GUI)之活动积分记录表
本文介绍了一套使用Python的Tkinter库构建的学生活动积分记录系统。该系统允许教师选择班级和学生,输入加分分数及原因,并将数据保存至文件,旨在简化学生积分管理流程,提升教学效率。
92 6
|
5月前
|
机器学习/深度学习 数据挖掘 Python
Python编程入门——从零开始构建你的第一个程序
【10月更文挑战第39天】本文将带你走进Python的世界,通过简单易懂的语言和实际的代码示例,让你快速掌握Python的基础语法。无论你是编程新手还是想学习新语言的老手,这篇文章都能为你提供有价值的信息。我们将从变量、数据类型、控制结构等基本概念入手,逐步过渡到函数、模块等高级特性,最后通过一个综合示例来巩固所学知识。让我们一起开启Python编程之旅吧!
101 1
|
4月前
|
Python
使用OpenPyXL库实现Excel单元格其他对齐方式设置
本文介绍了如何使用Python的`openpyxl`库设置Excel单元格中的文本对齐方式,包括文本旋转、换行、自动调整大小和缩进等,通过具体示例代码展示了每种对齐方式的应用方法,适合需要频繁操作Excel文件的用户学习参考。
245 85
使用OpenPyXL库实现Excel单元格其他对齐方式设置
|
1月前
|
Python
[oeasy]python074_ai辅助编程_水果程序_fruits_apple_banana_加法_python之禅
本文回顾了从模块导入变量和函数的方法,并通过一个求和程序实例,讲解了Python中输入处理、类型转换及异常处理的应用。重点分析了“明了胜于晦涩”(Explicit is better than implicit)的Python之禅理念,强调代码应清晰明确。最后总结了加法运算程序的实现过程,并预告后续内容将深入探讨变量类型的隐式与显式问题。附有相关资源链接供进一步学习。
39 4
|
3月前
|
存储 NoSQL 数据库连接
在Python程序中实现LevelDB的海量key的分批次扫描
通过本文的步骤,您可以在Python程序中实现对LevelDB海量key的分批次扫描。这样不仅能够有效地管理大规模数据,还可以避免一次性加载过多数据到内存中,提高程序的性能和稳定性。希望这篇指南能为您的开发工作提供实用的帮助。
104 28
|
3月前
|
数据可视化 数据挖掘 大数据
1.1 学习Python操作Excel的必要性
学习Python操作Excel在当今数据驱动的商业环境中至关重要。Python能处理大规模数据集,突破Excel行数限制;提供丰富的库实现复杂数据分析和自动化任务,显著提高效率。掌握这项技能不仅能提升个人能力,还能为企业带来价值,减少人为错误,提高决策效率。推荐从基础语法、Excel操作库开始学习,逐步进阶到数据可视化和自动化报表系统。通过实际项目巩固知识,关注新技术,为职业发展奠定坚实基础。
|
4月前
|
安全 API C语言
Python程序的安全逆向(关于我的OPENAI的APIkey是如何被盗的)
本文介绍了如何使用C语言编写一个简单的文件加解密程序,并讨论了如何为编译后的软件添加图标。此外,文章还探讨了Python的.pyc、.pyd等文件的原理,以及如何生成和使用.pyd文件来增强代码的安全性。通过视频和教程,作者详细讲解了生成.pyd文件的过程,并分享了逆向分析.pyd文件的方法。最后,文章提到可以通过定制Python解释器来进一步保护源代码。
123 6
|
4月前
|
IDE 程序员 开发工具
Python编程入门:打造你的第一个程序
迈出编程的第一步,就像在未知的海洋中航行。本文是你启航的指南针,带你了解Python这门语言的魅力所在,并手把手教你构建第一个属于自己的程序。从安装环境到编写代码,我们将一步步走过这段旅程。准备好了吗?让我们开始吧!
|
5月前
|
开发者 Python
使用Python实现自动化邮件通知:当长时程序运行结束时
本文介绍了如何使用Python实现自动化邮件通知功能,当长时间运行的程序完成后自动发送邮件通知。主要内容包括:项目背景、设置SMTP服务、编写邮件发送函数、连接SMTP服务器、发送邮件及异常处理等步骤。通过这些步骤,可以有效提高工作效率,避免长时间等待程序结果。
199 9

热门文章

最新文章

下一篇
oss创建bucket