【问题标题】:Stuck on a loop!卡在一个循环中!
【发布时间】:2010-12-17 14:32:19
【问题描述】:

我正在创建一个应用程序,用于将图像上传到指定的服务器。我已经在 Qt Designer 中创建了我的 GUI,一切正常我只是被困在我知道很简单的东西上。似乎无法绕过它。

这个想法是让脚本通过并查看有多少文本字段与图像路径一起归档 - 从那里获取每个路径,然后将每个路径上传到服务器。我可以让它只用一个盒子就可以了,但是当我尝试为这个过程创建一个循环时,它就会分崩离析。我基本上需要用每个不同的路径返回“全名”。这只是一个片段,但你明白了..

这个概念似乎很简单,我已经用我能找到和想到的许多方式重写了它。任何帮助都是极好的。我应该使用列表来代替吗?

        # count how many images there are going to be
    if not self.imgOnePathLabel.text().isEmpty():
        totalImages = 1
        # gets the path from IMAGE 1 box
        image1 = self.imgOnePathLabel.text()
        fullname = '%s' % image1
    if not self.imgTwoPathLabel.text().isEmpty():
        totalImages = 2
        image2 = self.img2PathLabel.text()
        fullname = '%s' % image2
    if not self.imgThreePathLabel.text().isEmpty():
        totalImages = 3
        imageThreePath = self.imgThreePathLabel.text()
        fullname = '%s' % imageThreePath
    try:
        for x in range(1,totalImages,1):
            # split end file from the file path
            name = os.path.split(fullname)[1]
            f = open(fullname, "rb")
            # store our selected file
            ftp.storbinary('STOR ' + name, f)
            msg = "Sent <font color=green>" + name + "</font>"
            self.logBrowser.append(msg)
            f.close()

    finally:
        msg = "<font color=green>" "Ok" "</font>"
        self.logBrowser.append(msg)

【问题讨论】:

  • 只是关于循环的评论...x 将具有值 1 和 2,但如果 totalImages 为 3,则永远不会为 3。

标签: python image ftp range qt-designer


【解决方案1】:

您遇到的问题是您分配给变量fullname 三次,每次都覆盖它。因此,当您进入 for 循环时,如果最后一个字段已设置,则只有最后一个文件名可用,否则您将一无所获。您需要一份全名列表而不是一个变量的说法是正确的。您需要以下内容:

    fullnames = []
    imageLabels = [self.imgOnePathLabel, self.imgTwoPathLabel,
            self.imgThreePathLabel]
    for imageLabel in imageLabels:
        if imageLabel.text():
            image = self.imgOnePathLabel.text()
            fullnames.append('%s' % image)
    try:
        for fullname in fullnames:
            # split end file from the file path
            name = os.path.split(fullname)[1]
            f = open(fullname, "rb")
            # store our selected file
            ftp.storbinary('STOR ' + name, f)
            msg = "Sent <font color=green>" + name + "</font>"
            self.logBrowser.append(msg)
            f.close()
    finally:
        msg = "<font color=green>" "Ok" "</font>"
        self.logBrowser.append(msg)

【讨论】:

  • 感谢 Tendayi- 就像一个魅力,我觉得我所有的“如果不是”陈述感觉有点笨重,但这是我需要的一个解决方案!
  • 您绝对可以将其减少一点,我最初提供的是一种解决您当前问题的方法。我对解决方案进行了小幅更新,使其阅读效果更好,并且更容易添加更多文本字段。
【解决方案2】:

除了范围需要 +1 才能达到 #3 的问题(参见 Vincent R 的评论),

(其中一个)问题是全名变量被非空标签的每个新案例覆盖。

很难评论代码来修复它,即我想建议并重新组织一下。例如通过引入一个函数来提取给定图像的名称/路径,给定 UI 对象;这将避免一些重复。这样的函数,或者它的调用者,会将每个新的全名添加到其中的列表中,然后上传循环可以使用该列表。

请参阅 Tendayi Mawushe 的解决方案,该解决方案尊重原始结构,但按照建议引入了列表。顺便说一句,这个列表可以作为循环的基础进行迭代,而不是依赖 range() 函数,并且 这更像是 pythonic (这消除了修复缺失问题的需要#3与范围)。虽然有时很方便,但对于 Python,这些数字范围驱动的循环通常会邀请您重新审视设计。

【讨论】:

  • 非常感谢!我打赌解决方案与创建列表有关,我只是不确定该方法。我明白你说它肯定更 Pythonic 是什么意思
【解决方案3】:

原始代码的另一个问题是,如果标签 1 和 3 不是空白,而标签 2 是空白,即使您只有两条路径,totalImages 也会设置为 3。

此外,此代码中有一个错字(“Two”与“2”):

if not self.imgTwoPathLabel.text().isEmpty():
    image2 = self.img2PathLabel.text()

而且我相信您不需要字符串替换 '%s' % image

您可以像这样稍微压缩您的代码(并解决您的问题):

# this is a list of references to your PyQt4 label objects
imagePathLabels = [self.imgOnePathLabel, 
                   self.imgTwoPathLabel, 
                   self.imgThreePathLabel]

try:
    for label in imagePathLabels:
        if not label.text().isEmpty():
            image_path = label.text()
            image_name = os.path.split(image_path)[1]
            f = open(image_path, "rb")
            ftp.storbinary('STOR ' + image_name, f)
            msg = "Sent <font color=green>" + name + "</font>"
            self.logBrowser.append(msg)
            f.close()
finally:
    msg = "<font color=green>" "Ok" "</font>"
    self.logBrowser.append(msg)

【讨论】:

    猜你喜欢
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 2015-08-26
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    相关资源
    最近更新 更多