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库,具体介绍请参考我的其他博文:链接直达

成品展示


相关文章
|
1天前
|
SQL 并行计算 API
Dask是一个用于并行计算的Python库,它提供了类似于Pandas和NumPy的API,但能够在大型数据集上进行并行计算。
Dask是一个用于并行计算的Python库,它提供了类似于Pandas和NumPy的API,但能够在大型数据集上进行并行计算。
19 9
|
1天前
|
机器学习/深度学习 人工智能 数据挖掘
Numba是一个Python库,用于对Python代码进行即时(JIT)编译,以便在硬件上高效执行。
Numba是一个Python库,用于对Python代码进行即时(JIT)编译,以便在硬件上高效执行。
20 9
|
1天前
|
网络协议 安全 Shell
`nmap`是一个开源的网络扫描工具,用于发现网络上的设备和服务。Python的`python-nmap`库允许我们在Python脚本中直接使用`nmap`的功能。
`nmap`是一个开源的网络扫描工具,用于发现网络上的设备和服务。Python的`python-nmap`库允许我们在Python脚本中直接使用`nmap`的功能。
22 7
|
1天前
|
机器人 Shell 开发者
`roslibpy`是一个Python库,它允许非ROS(Robot Operating System)环境(如Web浏览器、移动应用等)与ROS环境进行交互。通过使用`roslibpy`,开发者可以编写Python代码来远程控制ROS节点,发布和订阅话题,以及调用服务。
`roslibpy`是一个Python库,它允许非ROS(Robot Operating System)环境(如Web浏览器、移动应用等)与ROS环境进行交互。通过使用`roslibpy`,开发者可以编写Python代码来远程控制ROS节点,发布和订阅话题,以及调用服务。
18 8
|
1天前
|
自然语言处理 程序员 编译器
`pylatex`是一个Python库,用于生成LaTeX文档。LaTeX是一种用于高质量排版和打印的文档准备系统,特别适用于科学、技术和数学文档。
`pylatex`是一个Python库,用于生成LaTeX文档。LaTeX是一种用于高质量排版和打印的文档准备系统,特别适用于科学、技术和数学文档。
11 2
|
1天前
|
存储 搜索推荐 算法
`surprise`是一个用于构建和分析推荐系统的Python库。
`surprise`是一个用于构建和分析推荐系统的Python库。
11 0
|
1天前
|
机器学习/深度学习 PyTorch TensorFlow
在深度学习中,数据增强是一种常用的技术,用于通过增加训练数据的多样性来提高模型的泛化能力。`albumentations`是一个强大的Python库,用于图像增强,支持多种图像变换操作,并且可以与深度学习框架(如PyTorch、TensorFlow等)无缝集成。
在深度学习中,数据增强是一种常用的技术,用于通过增加训练数据的多样性来提高模型的泛化能力。`albumentations`是一个强大的Python库,用于图像增强,支持多种图像变换操作,并且可以与深度学习框架(如PyTorch、TensorFlow等)无缝集成。
8 0
|
1天前
|
Python
确保你已经安装了`python-barcode`库。如果没有,可以通过pip来安装:
确保你已经安装了`python-barcode`库。如果没有,可以通过pip来安装:
6 0
|
26天前
|
开发工具 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仓库或寻求社区帮助。
24 1
|
Linux Python
不可出外网的主机如何快速、方便、优雅的安装Python库?
不可出外网的主机如何快速、方便、优雅的安装Python库?
479 0
不可出外网的主机如何快速、方便、优雅的安装Python库?