Python 如何让zipfile使用UI包装进行实例化
我一直在尝试使用类库appJar,它是Python的一个很好的基本UI。我有可以执行我需要的功能的工作代码,但是,当我将UI包装在它周围时,它不会运行,并且UI对断点的反馈要少得多 相关代码如下:Python 如何让zipfile使用UI包装进行实例化,python,Python,我一直在尝试使用类库appJar,它是Python的一个很好的基本UI。我有可以执行我需要的功能的工作代码,但是,当我将UI包装在它周围时,它不会运行,并且UI对断点的反馈要少得多 相关代码如下: def zipdir(path, ziph): # ziph is zipfile handle for root, dirs, files in os.walk(fromDirectory): for file in files: with Archive.progressb
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(fromDirectory):
for file in files:
with Archive.progressbar.progressbar(max_value=10) as progress:
for i in range(1):
filePath = os.path.join(root, file)
ziph.write(filePath, relpath(filePath, ""))
time.sleep(0.1)
progress.update(i)
def beginBackup(btn):
return app.questionBox("Ready?", "Click OK ")
上面的设置设置了这些函数,下面的应该运行它,但是没有
try:
if (beginBackup == True):
print(beginBackup)
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
os.rename('Python.zip', "bak" + str(fileName) + ".zip")
shutil.move("bak" + str(fileName) + ".zip", str(toDirectory) + "/bak" + str(fileName) + ".zip")
else:
raise Exception("Something went wrong")
except Exception as e:
app.warningBox("Error", "Something went wrong: {}".format(str(e)))
我觉得这是因为我把代码放在了错误的地方,因为我觉得它从来没有实例化备份过程
以下是完整的代码:
import os
import zipfile
import shutil
import time
from os.path import relpath
from appJar import gui
# Global Variables
fromDirectory = ""
toDirectory = ""
fileName = time.time()
fileVersion = 1.2
# Setting up the mechanism
def backupsource(btn):
fromDirectory = app.directoryBox(title="Source")
def backupdest(btn):
toDirectory = app.directoryBox(title="Destination")
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(fromDirectory):
for file in files:
with Archive.progressbar.progressbar(max_value=10) as progress:
for i in range(1):
filePath = os.path.join(root, file)
ziph.write(filePath, relpath(filePath, ""))
time.sleep(0.1)
progress.update(i)
def beginBackup(btn):
return app.questionBox("Ready?", "Click OK ")
# Open the GUI
app = gui()
app.showSplash("Simple zip v. 1.2", fill="grey", stripe="#ADDFAD", fg="white", font=44)
# Setup the visual styles of the app
app.setTitle("Simple Zip")
app.setIcon("img/logo.ico")
app.setGeometry(400, 300)
app.setResizable(canResize=True)
# Items inside of the GUI
app.addLabel("title", "Welcome to the simple backup utility")
app.setLabelBg("title", "gray")
app.addLabel("Backup", "Label goes here")
# Setup source buttons
app.addButton("Source", backupsource)
app.addButton("Destination", backupdest)
# Begin Backup section
app.addButton("Backup!", beginBackup)
# start the GUI
app.go()
try:
if (beginBackup == True):
print(beginBackup)
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
os.rename('Python.zip', "bak" + str(fileName) + ".zip")
shutil.move("bak" + str(fileName) + ".zip", str(toDirectory) + "/bak" + str(fileName) + ".zip")
else:
raise Exception("Something went wrong")
except Exception as e:
app.warningBox("Error", "Something went wrong: {}".format(str(e)))
def beginBackup(btn):
if not app.questionBox("Ready?", "Click OK"):
app.warningBox("Error", "Cancelled")
else:
try:
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
shutil.move('Python.zip', str(toDirectory) + "/bak" + str(fileName) + ".zip")
except Exception as e:
app.warningBox("Error", "Something went wrong: {}".format(str(e)))
我发现的第一个错误是
os.renames
,它不存在。但我不能建议像这样轻率地发现错误。我会用异常包装器(exceptionwrapper)重写它,并短路无用的rename
命令,因为shutil.move
可以处理重命名和跨文件系统移动
然后您执行app.go()
但没有任何内容通过这一行。这是GUI主循环。从现在起,appJar
将分派事件。您不再控制主例程
因此,所有操作都必须在此处完成(按下按钮时激活代码):
将弹出一个窗口,不再执行任何操作。应将其替换为完整代码:
import os
import zipfile
import shutil
import time
from os.path import relpath
from appJar import gui
# Global Variables
fromDirectory = ""
toDirectory = ""
fileName = time.time()
fileVersion = 1.2
# Setting up the mechanism
def backupsource(btn):
fromDirectory = app.directoryBox(title="Source")
def backupdest(btn):
toDirectory = app.directoryBox(title="Destination")
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(fromDirectory):
for file in files:
with Archive.progressbar.progressbar(max_value=10) as progress:
for i in range(1):
filePath = os.path.join(root, file)
ziph.write(filePath, relpath(filePath, ""))
time.sleep(0.1)
progress.update(i)
def beginBackup(btn):
return app.questionBox("Ready?", "Click OK ")
# Open the GUI
app = gui()
app.showSplash("Simple zip v. 1.2", fill="grey", stripe="#ADDFAD", fg="white", font=44)
# Setup the visual styles of the app
app.setTitle("Simple Zip")
app.setIcon("img/logo.ico")
app.setGeometry(400, 300)
app.setResizable(canResize=True)
# Items inside of the GUI
app.addLabel("title", "Welcome to the simple backup utility")
app.setLabelBg("title", "gray")
app.addLabel("Backup", "Label goes here")
# Setup source buttons
app.addButton("Source", backupsource)
app.addButton("Destination", backupdest)
# Begin Backup section
app.addButton("Backup!", beginBackup)
# start the GUI
app.go()
try:
if (beginBackup == True):
print(beginBackup)
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
os.rename('Python.zip', "bak" + str(fileName) + ".zip")
shutil.move("bak" + str(fileName) + ".zip", str(toDirectory) + "/bak" + str(fileName) + ".zip")
else:
raise Exception("Something went wrong")
except Exception as e:
app.warningBox("Error", "Something went wrong: {}".format(str(e)))
def beginBackup(btn):
if not app.questionBox("Ready?", "Click OK"):
app.warningBox("Error", "Cancelled")
else:
try:
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
shutil.move('Python.zip', str(toDirectory) + "/bak" + str(fileName) + ".zip")
except Exception as e:
app.warningBox("Error", "Something went wrong: {}".format(str(e)))
所以现在,如果在回调中发生任何事情,您会得到一个很好的警告框,告诉您它到底是什么(无法打开文件、语法错误等等)
os.rename
=>os.rename
。当你可以用shutil.move
完成所有操作时,为什么要重命名+移动?我对os.rename进行了调整,我可能可以用shutil.move
完成所有操作,这是我作为控制台应用程序编写时的旧版本。我的想法是创建文件,然后使用shutil移动它,这可能是多余的。我没有使用try/except包装进行测试,但我在获取任何要打印到控制台的内容时遇到了问题。我将进行测试并给出反馈。我编辑了您在这里提到的代码(并更新了上面的帖子)实际上,控制台上没有发布任何内容。我对此还是新手,因此调试或输入详细代码以提供给控制台对我来说仍然是新鲜事。之前有几个错误,但概念上有一个很大的错误。你应该阅读GUI系统。同时,我的回答应该会起到作用。太棒了,谢谢你ou!是的,我在这方面还是很新的,所以你的建议帮助很大,现在我可以看到它阻塞的地方,尽管我现在不知道如何修复它。它看起来几乎像是挂着的,或者我没有给它足够的时间来完成。可能需要添加一个状态栏来监控进度?我不确定按照你的指示,除此之外,其他一切都可以正常工作是一行:shutil.move('Python.zip',str(toDirectory)+“/bak”+str(fileName)+“.zip”)
,它移动文件(我不确定到哪里),但不移动到目标toDirectory