我一直在尝试使用类库appJar这是一个很好的Python基本用户界面。我有工作的代码执行,我需要,但是,当我环绕它的用户界面,它不运行的功能,UI的对破发点我怎样才能得到zipfile实例与UI包装
远不及反馈相关的代码是在这里:
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)))
' os.renames' =>'os.rename'。为什么在用'shutil.move'完成所有的重命名+移动? –
我对'os.rename'进行了调整,我可能可以用'shutil.move'完成所有工作,这是我在写作控制台应用程序时的一个老版本。这个想法是创建文件,然后_then_使用shutil将其移动,这可能是多余的 – DSMTurboAWD