我一直在尝试使用类库 appJar ,这是一个很好的Python基本用户界面。我有工作代码来执行我需要的功能,但是,当我围绕它运行UI时,它不会运行,并且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)))
答案 0 :(得分:1)
第一个错误我发现的是os.renames
,它并不存在。但我不能建议像这样发现错误。我用异常包装器重写了那个(并且短路是无用的rename
命令,因为shutil.move
可以处理重命名+在文件系统之间移动。
然后你做app.go()
但没有任何东西通过这一行。它是GUI主循环。从现在起,appJar
将调度事件。你无法控制主程序了。
所以一切都必须在这里完成(按下按钮时激活代码):
def beginBackup(btn):
return app.questionBox("Ready?", "Click OK ")
会弹出一个窗口而不再执行任何操作。它应该被完整的代码替换:
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)))
所以现在如果在该回调中发生任何事情,你会得到一个很好的警告框,告诉你它究竟是什么(无法打开文件,语法错误等......)