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

成品展示

python使用tkinter库,封装操作excel为GUI程序_第1张图片

你可能感兴趣的:(笔记,Python,python,tkinter,excel)