import tkinter as tk from tkinter import messagebox import os import zipfile window = tk.Tk() window.title("Homework Packer 1.0 Beta") window.geometry("800x600") # 窗口设置 window.resizable(0, 0) # 固定窗口大小 var = tk.StringVar() # 输入框内容 num = 1 # 已生成的作业数量+1 source = str() # 单个c源代码的内容 path = str() # 打包相对路径 #__Labels head = tk.Label(window, text="请将第{num}题源代码复制到框中:".format(num=num), font=('Arial', 15), width=20, height=1) # 这里的长宽以字符为单位 head.place(x=20, y=100, anchor='nw') name = tk.Label(window, text="请输入姓名:",font=('Arial', 15), ) name.place(x=2, y=5, anchor='nw') xuehao = tk.Label(window, text="请输入学号:",font=('Arial', 15), ) xuehao.place(x=2, y=35, anchor='nw') ok_txt = tk.Label(window, text="点击确定即可\n以框中内容\n生成一个.py文件:", font=('Arial', 15)) ok_txt.place(x=600, y=20, anchor='nw') #__Entrys name_en = tk.Entry(window) name_en.place(x=122, y=8, anchor='nw') xuehao_en = tk.Entry(window) xuehao_en.place(x=122, y=38, anchor='nw') #__Buttons def make_zip(floder, backupfilename): # floder:要打包的文件夹;backupfilename:指定文件名 backupfilename = backupfilename + '.zip' f = zipfile.ZipFile(backupfilename, 'w') # c创建一个zip对象 for floderName, subFolders, fileNames in os.walk(floder): f.write(floderName) for subFolder in subFolders: f.write(os.path.join(floderName, subFolder)) for fileName in fileNames: f.write(os.path.join(floderName, fileName)) f.close() def ok_hit(): ##确认 键的功能 global path path = "./{xuehao}_{name}".format(xuehao=xuehao_en.get(),name=name_en.get()) folder = os.path.exists(path) if not folder: os.makedirs(path) source = content.get("0.0", "end") global num with open(path + '/' + "第{num}题.py".format(num=num), 'w') as f: f.write(source) num += 1 head.config(text="请将第{num}题源代码复制到框中:".format(num=num)) content.delete("0.0", 'end') pass def zip_them_hit(): ##打包键功能 make_zip(path, "{xuehao}_{name}".format (xuehao=xuehao_en.get(), name=name_en.get())) messagebox.showinfo(message="你的 {num} 个c语言源代码\n已经打包入zip压缩包中\n\ 在当前目录即可找到".format( num=num - 1), title="Packing has been complished!") os.system("explorer " + os.getcwd()) window.destroy() ##结束程序 pass def about_file(): ##文件介绍 win = tk.Tk() win.title("关于本软件") win.geometry("400x500") # 窗口设置 win.resizable(0, 0) # 固定窗口大小 tk.Label(win, text="========================================\n\ 使用方法很简单:\n\ 1.输入姓名和学号\n\ 2.每完成一题编程作业就将源码复制到框里。\n\ 3.点击<确定>。\n\ 4.重复2,3步直到作业做完\n\ 5.点击<打包成zip> 即可在程序所在的目录\n\ 下找到压缩包。\n\ \n\ (包名字都是按本班 C语言作业命\n\ 名要求设置的,所以这个软件基本上就给自\n\ 己人使用。)\n\ ========================================\n\ ========================================", anchor='ne').pack() pass # # # # # # # # # # # ## 创建Buttons ok = tk.Button(window, text="确 定", command=ok_hit, width=10, height=2, font=40) ok.place(x=650, y=120, anchor='nw') zip_them = tk.Button(window, text="打包成zip", command=zip_them_hit, width=10, height=2, font=40) zip_them.place(x=650, y=250, anchor='nw') # about HMPacker1.0Beta about = tk.Button(window, text="关于本软件", command=about_file, font=5) about.place(x=600, y=550, anchor='nw') #__Texts content = tk.Text(window, width=80, height=30) content.place(x=20, y=130, anchor='nw') about_file() # 运行的时候打开以下about_file 因为里面有操作方法 window.mainloop()