陷入循环!

发布于 2024-08-11 07:03:58 字数 1404 浏览 4 评论 0原文

我正在创建一个应用程序,用于将图像上传到指定的服务器。我已经在 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)

I am creating an app which will be used to upload images to a specified server. I have created my GUI in Qt Designer, everything works fine I just am stuck on something that I know is simple. Cant seem to wrap my head around it.

The idea is for the script to go through and see how many text fields are filed in with paths of images - from there get each path and in order upload each one to server. I can make it work with just one box just fine but when i try to create a loop for this process it falls apart. I basically need to return 'fullname' with each different path. This is just a snippit but you get the idea..

The concept seems simple enough and I have rewritten this as many ways and I could find and think of. Any help would be awesome. Should I be using lists to do this instead or something?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梅窗月明清似水 2024-08-18 07:03:59

原始代码的另一个问题是,如果标签 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)

Another issue with your original code is that if labels 1 and 3 are not blank, but label 2 is, totalImages would be set to 3 even though you only have two paths.

Also, a typo in this code ("Two" vs "2"):

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

And I believe you don't need the string substitution '%s' % image.

You could condense your code a little (and fix your issues) like this:

# 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)
撩动你心 2024-08-18 07:03:58

您遇到的问题是,您为变量 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)

The issue you have is that you are assigning to the variable fullname three times overwriting it each time. So by the time you get to the for loop only have the last file name available if the last field has been set otherwise you get nothing at all. Your statement that you need a list of full names rather than one variable is correct. You need something like below:

    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)
握住你手 2024-08-18 07:03:58

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

问题之一是 fullname 变量被每个新的情况覆盖非空标签。

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

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

Aside from the issue of the range needing to be +1 as so to reach to #3 (cf Remark by Vincent R),

(one of) the problem(s) is that the fullname variable is overwritten with each of the new case of non empty label.

It's hard to comment about the code to just fix it, i.e. I'd like to suggest and reorganize it a bit. For example by introducing a function to extract the name / path of a given image, given the UI object; this would avoid some of the repetition. Such a function, or the the caller of it, would then add each new fullname to a list thereof, which can then be used by the upload loop.

See Tendayi Mawushe's solution, which is respectful of the original structure but introduces a list as suggested. BTW this list then can be iterated over as the basis of the loop, instead of relying on the range() function, and this is much more pythonic (and this removes the need of fixing the problem with missing #3 with the range). While being handy on occasion, with Python, these numeric range driven loops are often an invitation to revisit the design.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文