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天前
|
数据采集 JavaScript Android开发
【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
29 7
【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
|
28天前
|
测试技术 Python
【03】做一个精美的打飞机小游戏,规划游戏项目目录-分门别类所有的资源-库-类-逻辑-打包为可玩的exe-练习python打包为可执行exe-优雅草卓伊凡-持续更新-分享源代码和游戏包供游玩-1.0.2版本
【03】做一个精美的打飞机小游戏,规划游戏项目目录-分门别类所有的资源-库-类-逻辑-打包为可玩的exe-练习python打包为可执行exe-优雅草卓伊凡-持续更新-分享源代码和游戏包供游玩-1.0.2版本
106 31
【03】做一个精美的打飞机小游戏,规划游戏项目目录-分门别类所有的资源-库-类-逻辑-打包为可玩的exe-练习python打包为可执行exe-优雅草卓伊凡-持续更新-分享源代码和游戏包供游玩-1.0.2版本
|
1月前
|
机器学习/深度学习 存储 数据挖掘
Python图像处理实用指南:PIL库的多样化应用
本文介绍Python中PIL库在图像处理中的多样化应用,涵盖裁剪、调整大小、旋转、模糊、锐化、亮度和对比度调整、翻转、压缩及添加滤镜等操作。通过具体代码示例,展示如何轻松实现这些功能,帮助读者掌握高效图像处理技术,适用于图片美化、数据分析及机器学习等领域。
73 20
|
1月前
|
数据可视化 数据挖掘 大数据
1.1 学习Python操作Excel的必要性
学习Python操作Excel在当今数据驱动的商业环境中至关重要。Python能处理大规模数据集,突破Excel行数限制;提供丰富的库实现复杂数据分析和自动化任务,显著提高效率。掌握这项技能不仅能提升个人能力,还能为企业带来价值,减少人为错误,提高决策效率。推荐从基础语法、Excel操作库开始学习,逐步进阶到数据可视化和自动化报表系统。通过实际项目巩固知识,关注新技术,为职业发展奠定坚实基础。
|
2月前
|
XML JSON 数据库
Python的标准库
Python的标准库
185 77
|
2月前
|
XML JSON 数据库
Python的标准库
Python的标准库
71 11
|
4月前
|
Python
pip批量安装Python库 requirement.txt 离线环境无互联网环境下pip安装Python库
pip批量安装Python库 requirement.txt 离线环境无互联网环境下pip安装Python库
240 3
|
7月前
|
Python
确保你已经安装了`python-barcode`库。如果没有,可以通过pip来安装:
确保你已经安装了`python-barcode`库。如果没有,可以通过pip来安装:
|
8月前
|
开发工具 git Python
安装和使用`libnum`是一个用于数字理论函数的Python库
【6月更文挑战第19天】`libnum`是Python的数字理论函数库。安装可通过`git clone`,进入目录后运行`python setup.py install`,也可用`pip install libnum`。示例:使用`int_to_hex`将十进制数42转换为十六进制字符串&#39;2a&#39;。注意,信息可能已过时,应查最新文档以确保准确性。如遇问题,参考GitHub仓库或寻求社区帮助。
148 1
|
Linux Python
不可出外网的主机如何快速、方便、优雅的安装Python库?
不可出外网的主机如何快速、方便、优雅的安装Python库?
519 0
不可出外网的主机如何快速、方便、优雅的安装Python库?

热门文章

最新文章

推荐镜像

更多