• 使用Python Tkinter创建文件生成工具


    我们将使用Python的Tkinter模块创建一个简单的文件生成工具。这个工具可以用来创建Excel、文本、Python脚本和Word文档等不同类型的文件。

    1、准备工作

    首先,确保你已经安装了Python和所需的第三方库:Tkinter、openpyxl和python-docx。你可以使用pip来安装它们:

    pip install tkinter openpyxl python-docx
    
    
    • 1
    • 2

    2、创建GUI界面

    我们将使用Tkinter模块来创建用户界面。以下是创建主窗口和调用文件类型选择对话框的代码:

    import tkinter as tk
    from tkinter import simpledialog
    from tkinter import messagebox
    from openpyxl import Workbook
    from docx import Document
    
    # 定义创建文件的函数...
    
    def show_file_dialog():
        root = tk.Tk()
        root.withdraw()
        
        # 创建文件类型选择对话框的代码...
        
        window.mainloop()
    
    show_file_dialog()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3、创建不同类型的文件

    接下来,我们将编写函数来创建Excel、文本、Python脚本和Word文档等不同类型的文件。

    # 定义创建文件的函数...
    
    def create_excel_file():
        wb = Workbook()
        wb.save('new_document.xlsx')
        messagebox.showinfo("成功", "新的 Excel 文件创建成功。")
    
    def create_text_file():
        with open('new_document.txt', 'w') as f:
            f.write("这是一个新的文本文档。")
        messagebox.showinfo("成功", "新的文本文件创建成功。")
    
    def create_python_file():
        with open('new_document.py', 'w') as f:
            f.write("# 这是一个新的 Python 脚本。\n\nprint('你好,世界!')")
        messagebox.showinfo("成功", "新的 Python 脚本文件创建成功。")
    
    def create_word_file():
        doc = Document()
        doc.save('new_document.docx')
        messagebox.showinfo("成功", "新的 Word 文档创建成功。")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    4、显示文件类型选择对话框

    最后,我们将完善show_file_dialog函数,以便用户可以选择要创建的文件类型。

    # 创建文件类型选择对话框的代码...
    
    def show_file_dialog():
        root = tk.Tk()
        root.withdraw()
        
        file_types = ['xlsx', 'txt', 'py', 'docx']
        window = tk.Toplevel(root)
        window.geometry("300x100")
        
        file_type_var = tk.StringVar(window)
        file_type_var.set(file_types[0])
        file_type_menu = tk.OptionMenu(window, file_type_var, *file_types)
        file_type_menu.pack()
        
        def on_ok():
            file_type = file_type_var.get()
            
            if file_type:
                if file_type == 'xlsx':
                    create_excel_file()
                elif file_type == 'txt':
                    create_text_file()
                elif file_type == 'py':
                    create_python_file()
                elif file_type == 'docx':
                    create_word_file()
                window.destroy()
                root.destroy()
    
        ok_button = tk.Button(window, text="确定", command=on_ok)
        ok_button.pack()
        
        window.mainloop()
    
    show_file_dialog()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    现在,当用户运行程序时,将会弹出一个窗口,用户可以选择要创建的文件类型,并点击“确定”按钮,然后相应的文件将会被创建。

    5、完整代码

    import tkinter as tk  # 导入Tkinter模块,用于创建GUI界面
    from tkinter import simpledialog  # 导入simpledialog模块,用于创建简单的对话框
    from tkinter import messagebox  # 导入messagebox模块,用于显示消息框
    from openpyxl import Workbook  # 导入openpyxl模块,用于处理Excel文件
    from docx import Document  # 导入docx模块,用于处理Word文档
    
    # 定义函数:创建一个新的Excel文件
    def create_excel_file():
        wb = Workbook()  # 创建一个Workbook对象
        wb.save('new_document.xlsx')  # 将Workbook对象保存为Excel文件
        messagebox.showinfo("成功", "新的 Excel 文件创建成功。")  # 显示消息框,提示文件创建成功
    
    # 定义函数:创建一个新的文本文件
    def create_text_file():
        with open('new_document.txt', 'w') as f:
            f.write("这是一个新的文本文档。")  # 向文件中写入内容
        messagebox.showinfo("成功", "新的文本文件创建成功。")  # 显示消息框,提示文件创建成功
    
    # 定义函数:创建一个新的Python脚本文件
    def create_python_file():
        with open('new_document.py', 'w') as f:
            f.write("# 这是一个新的 Python 脚本。\n\nprint('你好,世界!')")  # 向文件中写入内容
        messagebox.showinfo("成功", "新的 Python 脚本文件创建成功。")  # 显示消息框,提示文件创建成功
    
    # 定义函数:创建一个新的Word文档
    def create_word_file():
        doc = Document()  # 创建一个Document对象
        doc.save('new_document.docx')  # 将Document对象保存为Word文档
        messagebox.showinfo("成功", "新的 Word 文档创建成功。")  # 显示消息框,提示文件创建成功
    
    # 定义函数:显示文件类型选择对话框
    def show_file_dialog():
        root = tk.Tk()  # 创建一个Tk对象
        root.withdraw()  # 隐藏Tk窗口
    
        # 定义文件类型选项
        file_types = ['xlsx', 'txt', 'py', 'docx']
    
        # 创建一个新窗口
        window = tk.Toplevel(root)
    
        # 设置窗口大小
        window.geometry("300x100")
    
        # 创建下拉菜单以选择文件类型
        file_type_var = tk.StringVar(window)
        file_type_var.set(file_types[0])  # 默认选择第一个文件类型
        file_type_menu = tk.OptionMenu(window, file_type_var, *file_types)
        file_type_menu.pack()
    
        # 定义确认按钮的事件处理函数
        def on_ok():
            # 获取用户选择的文件类型
            file_type = file_type_var.get()
    
            # 根据用户选择的文件类型调用相应的函数创建文件,并显示消息框提示用户文件创建成功
            if file_type:
                if file_type == 'xlsx':
                    create_excel_file()
                elif file_type == 'txt':
                    create_text_file()
                elif file_type == 'py':
                    create_python_file()
                elif file_type == 'docx':
                    create_word_file()
                window.destroy()  # 销毁窗口
                root.destroy()  # 销毁主窗口
    
        # 创建确认按钮,点击按钮时触发on_ok函数
        ok_button = tk.Button(window, text="确定", command=on_ok)
        ok_button.pack()
    
        # 显示对话框
        window.mainloop()
    
    # 调用show_file_dialog函数,运行程序
    show_file_dialog()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
  • 相关阅读:
    记账时,花费如何按类别分类
    数据预处理与模型评估【机器学习、人工智能、实际事例】
    Java设计模式-创建型模式-原型模式
    接口测试[PostMan]
    前端开发学习之【Vue】-下
    C++常成员函数 - const 关键字
    【C++】C++ 引用详解 ③ ( 函数返回值不能是 “ 局部变量 “ 的引用或指针 | 函数内的 “ 局部变量 “ 的引用或指针做函数返回值无意义 )
    如何写一篇吊炸天的竞品分析
    商汤&上海AI实验室联合发布:自动驾驶全栈式高精度标定工具箱(含车、IMU、相机、激光雷达等的标定)
    基于python的毕业设计电脑硬件配置推荐系统
  • 原文地址:https://blog.csdn.net/lcl17779740668/article/details/138031120