前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 弹出框 压缩文件 解压文件

python 弹出框 压缩文件 解压文件

作者头像
用户5760343
发布2022-05-13 10:34:41
8950
发布2022-05-13 10:34:41
举报
文章被收录于专栏:sktjsktj

from tkinter import * # widgets and presets from tkinter.filedialog import askopenfilename # file selector dialog

def makeFormRow(parent, label, width=15, browse=True, extend=False): var = StringVar() row = Frame(parent) lab = Label(row, text=label + '?', relief=RIDGE, width=width) ent = Entry(row, relief=SUNKEN, textvariable=var) row.pack(fill=X) # uses packed row frames lab.pack(side=LEFT) # and fixed-width labels ent.pack(side=LEFT, expand=YES, fill=X) # or use grid(row, col) if browse: btn = Button(row, text='browse...') btn.pack(side=RIGHT) if not extend: btn.config(command= lambda: var.set(askopenfilename() or var.get()) ) else: btn.config(command= lambda: var.set(var.get() + ' ' + askopenfilename()) ) return var

pack text files into a single file with separator lines (simple archive)

import sys, glob marker = ':' * 20 + 'textpak=>' # hopefully unique separator

def pack(ofile, ifiles): output = open(ofile, 'w') for name in ifiles: print('packing:', name) input = open(name, 'r').read() # open the next input file if input[-1] != '\n': input += '\n' # make sure it has endline output.write(marker + name + '\n') # write a separator line output.write(input) # and write the file's contents

mlen = len(marker) # filenames after markers

def unpack(ifile, prefix='new-'): for line in open(ifile): # for all input lines if line[:mlen] != marker: output.write(line) # write real lines else: name = prefix + line[mlen:-1] # or make new output print('creating:', name) output = open(name, 'w')

popup a GUI dialog for packer script arguments, and run it

from glob import glob # filename expansion from tkinter import * # GUI widget stuff

def packDialog(): # a new top-level window win = Toplevel() # with 2 row frames + ok button win.title('Enter Pack Parameters') var1 = makeFormRow(win, label='Output file') var2 = makeFormRow(win, label='Files to pack', extend=True) Button(win, text='OK', command=win.destroy).pack() win.grab_set() win.focus_set() # go modal: mouse grab, keyboard focus, wait win.wait_window() # wait till destroy; else returns now return var1.get(), var2.get() # fetch linked var values

def runPackDialog(): output, patterns = packDialog() # pop-up GUI dialog if output != "" and patterns != "": # till ok or wm-destroy patterns = patterns.split() # do non-GUI part now filenames = [] for sublist in map(glob, patterns): # do expansion manually filenames += sublist # Unix shells do this auto print('Packer:', output, filenames) pack(ofile=output, ifiles=filenames) # should show msgs in GUI too

if name == 'main': root = Tk() Button(root, text='popup', command=runPackDialog).pack(fill=X) Button(root, text='bye', command=root.quit).pack(fill=X) root.mainloop()

image.png


//解压文件

popup a GUI dialog for unpacker script arguments, and run it

from tkinter import * # widget classes

def unpackDialog(): win = Toplevel() win.title('Enter Unpack Parameters') var = makeFormRow(win, label='Input file', width=11) win.bind('<Key-Return>', lambda event: win.destroy()) win.grab_set() win.focus_set() # make myself modal win.wait_window() # till I'm destroyed on return return var.get() # or closed by wm action

def runUnpackDialog(): input = unpackDialog() # get input from GUI if input != '': # do non-GUI file stuff print('Unpacker:', input) # run with input from dialog unpack(ifile=input, prefix='')

if name == "main": Button(None, text='popup', command=runUnpackDialog).pack() mainloop()

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • pack text files into a single file with separator lines (simple archive)
  • popup a GUI dialog for packer script arguments, and run it
  • popup a GUI dialog for unpacker script arguments, and run it
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档