2009-11-17 60 views
0

我正在创建一个应用程序,用于将图像上传到指定的服务器。我在Qt Designer中创建了我的图形用户界面,一切正常,我只是卡在了我所知道的很简单的东西上。似乎不能把我的头包裹起来。卡在循环中!

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

这个概念似乎很简单,我已经改写了很多方法,我可以找到和想到。任何帮助都是极好的。我应该使用列表来做到这一点,或者什么?

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

只是有关环路评论... X的值将是1和2,但从来没有3如果totalImages为3 。 – 2009-11-17 20:09:21

回答

2

您遇到的问题是,您每次都将变量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) 
+0

感谢Tendayi-作品像一个魅力,我觉得它有点笨重与我所有的'如果不是'的声明,但这是一个我需要的解决方案! – 2009-11-17 20:36:41

+0

你肯定可以修改这一点,我最初给出的是一种克服你眼前的问题的方法。我对该解决方案进行了小幅更新,使其读取更好一些,并使添加更多文本字段变得更加容易。 – 2009-11-17 20:48:36

0

除了范围需要为1作为这样的问题,以达到到#3(由文森特řCF备注)

(之一)的问题(s)为使全名变量会被每个新的非空标签情况覆盖。

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

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

+0

谢谢!我打赌解决方案与创建列表有关,我只是不确定这个方法。我明白你的意思是说它会更加pythonic – 2009-11-17 20:34:34

0

与原代码的另一个问题是,如果标签1和3不为空,但标签2,totalImages将被设置为3,即使你只有两条路。

另外,在此代码一个错字(“二”与“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)