2017-01-09 42 views
0

我一直在尝试使用类库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))) 
+1

' os.renames' =>'os.rename'。为什么在用'shutil.move'完成所有的重命名+移动? –

+0

我对'os.rename'进行了调整,我可能可以用'shutil.move'完成所有工作,这是我在写作控制台应用程序时的一个老版本。这个想法是创建文件,然后_then_使用shutil将其移动,这可能是多余的 – DSMTurboAWD

回答

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))) 

所以现在如果有什么在回调发生,你会得到一个很好的警告框,告诉你它到底是什么(无法​​打开文件,语法错误等)

+0

我还没有使用try/except包装器进行测试,但是我遇到了任何问题,无法打印到控制台。我会测试和回复 – DSMTurboAWD

+0

我编辑了代码,就像你在这里提到的(并且更新了上面的文章),没有任何东西被发布到控制台。我仍然是新来的,所以调试或者输入详细的代码来提供给控制台对我来说仍然是新的 – DSMTurboAWD

+0

我错过了之前的观点。有几个错误,但是一个非常大的概念。你应该阅读一下GUI系统。与此同时,我的答案应该做到这一点。 –