【问题标题】:Python and wxpython how to use a button to download a file from ftpPython 和 wxpython 如何使用按钮从 ftp 下载文件
【发布时间】:2014-04-04 18:25:24
【问题描述】:

好的,现在我正在使用 ftp 为我的应用程序下载文件,但问题是代码在我启动程序时会下载文件,而当我单击下载按钮时什么也不做

# Handlers for MainFrameBase events.
def DownloadButtonClick( ftp,directory,file ):
    ftp.cwd(directory)
    f = open(file,"wb")
    ftp.retrbinary("RETR " + file,f.write)
    f.close()

ftp = ftplib.FTP("ftp.apkmultitool.com")
ftp.login("adkesoapp@apkmultitool.com", "adkesoapp")

DownloadButtonClick(ftp, "", "testfile.txt")

我正在使用 wxbuilder 为我的应用程序设计 GUI,我是 python 的新手,我已经尝试用 Google 寻找我的答案,但没有任何结果 我还希望在按下按钮时它会检查文件,如果已经存在,它将附加到文档而不是覆盖。

我可能只是放弃这个想法并让它从文本文件的原始链接下载,因为你怀疑我可以将文件上传回服务器并让它检查并修改它 但我也不希望它导致重复的条目,所以任何防止这种情况的建议可能最好在它自己的问题中,但这是我最终想要完成的基础

如果你想查看完整的源代码

https://github.com/ADK-ESO-Project/ADK-ESO-Multi-Tool

【问题讨论】:

    标签: python-2.7 ftp download wxpython ftplib


    【解决方案1】:

    这是一个代码 sn-p,它创建一个带有按钮的 GUI。当您单击该按钮时,您会注意到它当前仅在控制台中打印Clicked。您可以将 FTP 代码放在那里,这样当您单击按钮时,FTP 内容就会启动。

    代码:

    import wx
    
    class Frame(wx.Frame):
        def __init__(self, parent, id, title):
            wx.Frame.__init__(self, parent, id, title, style=wx.DEFAULT_FRAME_STYLE)
            myPanel = wx.Panel(self,-1)
            myButton = wx.Button(myPanel, -1, 'Download', size=(100,50), pos=(20,20))
            myButton.Bind(wx.EVT_LEFT_DOWN, self.onClick)
            self.Show(True)
        def onClick(self, e):
            print 'Clicked'
            #Put your code here that you
            #want to get executed when ever
            #you click the button.
            #Eg: call your method from here
            #DownloadButtonClick(ftp, "", "testfile.txt")
    
    app = wx.App()
    frame = Frame(None, wx.ID_ANY, 'Image')
    app.MainLoop()
    

    这是一个简单的应用程序。如果你想并行做更多的事情,你可能喜欢使用线程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      • 2011-10-23
      • 1970-01-01
      相关资源
      最近更新 更多