-1
import tkinter as tk
from ftplib import FTP

pencere = tk.Tk()

pencere.title("Title")
pencere.geometry("800x300")


def deleteAllFiles(ftp):
    for n in ftp.nlst():
        try:
            if n not in ('.','..'):
                print('Working on..'+n)
                try:
                    ftp.delete(n)
                    print('Deleted...'+n)
                except Exception:
                    print(n+' Not deleted, we suspect its a directory, changing to '+n)
                    ftp.cwd(n)
                    deleteAllFiles(ftp)
                    ftp.cwd('..')
                    print('Trying to remove directory ..'+n)
                    ftp.rmd(n)
                    print('Directory, '+n+' Removed')
        except Exception:
            print( 'Trying to remove directory ..'+n)
            ftp.rmd(n)
            print('Directory, '+n+' Removed')

ftp = FTP('***')
username="**"
pwd="**"
ftp.login(username, pwd)
ftp.cwd('htdocs') 
deleteAllFiles(ftp)
print('Done deleting all Files and Directories')

etiket = tk.Label(text="LABEL" ,font = "Verdana 22 bold")
etiket.pack()

button1=tk.Button(pencere, text="Button", command=deleteAllFiles(ftp))
button1.pack()

button2=tk.Button(pencere, text="Quit", command=pencere.quit)
button2.pack()

pencere.mainloop()

此代码有助于删除我通过 ftp 指定的文件夹中的文件,但我启动应用程序并应用命令而不按下按钮。

在不单击按钮的情况下运行 deleteAllFiles(ftp) 命令我该如何解决这个问题

当前代码;

import tkinter as tk
from ftplib import FTP

pencere = tk.Tk()

pencere.title("Title")
pencere.geometry("800x300")

def deleteAllFiles(ftp):
    for n in ftp.nlst():
        try:
            if n not in ('.','..'):
                print('Working on..'+n)
                try:
                    ftp.delete(n)
                    print('Deleted...'+n)
                except Exception:
                    print(n+' Not deleted, we suspect its a directory, changing to '+n)
                    ftp.cwd(n)
                    deleteAllFiles(ftp)
                    ftp.cwd('..')
                    print('Trying to remove directory ..'+n)
                    ftp.rmd(n)
                    print('Directory, '+n+' Removed')
        except Exception:
            print( 'Trying to remove directory ..'+n)
            ftp.rmd(n)
            print('Directory, '+n+' Removed')

ftp = FTP('@@')
username="@@"
pwd="@@"
ftp.login(username, pwd)
ftp.cwd('htdocs') 
#deleteAllFiles(ftp)
print('Done deleting all Files and Directories')

button1=tk.Button(pencere, text="Button", command=lambda: deleteAllFiles(ftp))
button1.pack()

etiket = tk.Label(text="LABEL" ,font = "Verdana 22 bold")
etiket.pack()


button2=tk.Button(pencere, text="Button", command=pencere.quit)
button2.pack()

pencere.mainloop()

这是当前代码仍然自动运行的相同命令@Matiiss我能做什么请帮忙(我需要多写一点,所以我正在写)

4

1 回答 1

1

改变

command=deleteAllFiles(ftp)

command=lambda: deleteAllFiles(ftp)

所以你不是马上调用这个函数,而是把它包装在一个 lambda 中。

于 2021-08-13T16:09:01.950 回答