English 中文(简体)
翻新回响功能中的差价——试图更新优惠制的进展
原标题:ValueError in Thread callback function - Trying to update FTP progress

I m New to threads and with some problems ting my workers function to Update the GUI. 我有两种变数,即我试图转回全球倡议,但当我跑时,我拿到的是价值错误:太多的数值无法包装。

import time
import os, sys, wx
from ftplib import FTP_TLS

from threading import Thread
from wx.lib.pubsub import Publisher

########################################################################
class TestThread(Thread):
    """Test Worker Thread Class."""

    #----------------------------------------------------------------------
    def __init__(self):
        """Init Worker Thread Class."""
        Thread.__init__(self)
        self.start()    # start the thread

    #----------------------------------------------------------------------
    def run(self):
        """Run Worker Thread."""
        # This is the code executing in the new thread.
        HOST =  127.0.0.1 
        USERID =  User 
        PASSWD =  Passwd 
        FILE = r C:Myfile.zip 


        BLOCKSIZE = 57344
        try:
            ftp = FTP_TLS(HOST)
            ftp.login(USERID, PASSWD)
            ftp.prot_p()
            ftp.voidcmd("TYPE I")
            f = open(FILE,  rb )
            datasock, esize = ftp.ntransfercmd(
                     STOR %s  % os.path.basename(FILE))
            size = os.stat(FILE)[6]
            bytes_so_far = 0
            while 1:
                buf = f.read(BLOCKSIZE)
                if not buf:
                    break
                datasock.sendall(buf)
                bytes_so_far += len(buf)
                msg = [bytes_so_far, size]
                Publisher().sendMessage("update", msg)
        except: raise
        finally:
            try:
                datasock.close()
                f.close()
                ftp.voidresp()
                ftp.quit()
                print  Complete... 
            except: pass

        wx.CallAfter(Publisher().sendMessage, "update", "Thread finished!")

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        self.displayLbl = wx.StaticText(panel, label="Amount of time since thread started goes here")
        self.btn = btn = wx.Button(panel, label="Start Thread")
        self.gauge = wx.Gauge(panel, -1, 100, size=(370, 24))

        btn.Bind(wx.EVT_BUTTON, self.onButton)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.displayLbl, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(self.gauge, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

        # create a pubsub receiver
        Publisher().subscribe(self.updateDisplay, "update")

    #----------------------------------------------------------------------
    def onButton(self, event):
        """
        Runs the thread
        """
        TestThread()
        self.displayLbl.SetLabel("Thread started!")
        btn = event.GetEventObject()
        btn.Disable()

    #----------------------------------------------------------------------
    def updateDisplay(self, msg):
        """
        Receives data from thread and updates the display
        """
        print msg.data
        bytes_so_far, size = msg.data
        k = 100 * bytes_so_far / size
        self.displayLbl.SetLabel("
Sent %d of %d bytes %.1f%%
" % (bytes_so_far, size, 100 * bytes_so_far / size))
        self.gauge.SetValue(k)
        self.btn.Enable()

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()

The error is as follows:

Traceback (most recent call last):
  File "C:Python27libsite-packageswx-2.8-msw-unicodewx\_core.py", line 14640, in <lambda>
    lambda event: event.callable(*event.args, **event.kw) )
  File "C:Python27libsite-packageswx-2.8-msw-unicodewxlibpubsubpubsub1pub.py", line 750, in sendMessage
    self.__topicTree.sendMessage(aTopic, message, onTopicNeverCreated)
  File "C:Python27libsite-packageswx-2.8-msw-unicodewxlibpubsubpubsub1pub.py", line 423, in sendMessage
    deliveryCount += node.sendMessage(message)
  File "C:Python27libsite-packageswx-2.8-msw-unicodewxlibpubsubpubsub1pub.py", line 261, in sendMessage
    listener(message)
  File "F:ProgrammingTestswxThread_FTP_Funtion.py", line 98, in updateDisplay
    bytes_so_far, size = msg.data
ValueError: too many values to unpack
最佳回答

通告

  File "G:ProgrammingTestswxThread_FTP_Funtion.py", line 126, in updateDisplay
    bytes_so_far, size = list

指出,如果将变数<代码>>>>>><>>>>>>>>>>> 代码---------------------------------->-----------------/--------------------------------------------------------------------------------------------------------- <代码>对许多数值表示无包装<>/代码>错误,表示<代码><>>>> > > >中有两个以上要素。

但是,你的代码似乎与这一错误不符,因为你的样本中没有一条线<编码>-bytes_so_far, volume = list,因此,在有些地方,似乎还有其他一些版本的代码可能会使错误的原因更加清楚。

EDIT:

Thanks for the update. The error should be due to this call:

wx.CallAfter(Publisher().sendMessage, "update", "Thread finished!")

更新《刑法》时,

bytes_so_far, size = msg.data

载于Thread ±/code>,python试图将体格分解成其个人特性,以便通过字母_so_far和大小将体格分解成每个配量变量,从而导致<代码>将许多数值移至未包装的电文中,尽管在电文表面上只有一种价值,但你可能期望有

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签