【发布时间】:2021-08-13 16:02:11
【问题描述】:
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 我能做什么请帮忙(我需要再写一点,所以我在写)
【问题讨论】:
-
command=deleteAllFiles(ftp)表示您将command设置为deleteAllFiles(ftp)的返回值。您正在该行中执行deleteAllFiles。您必须将command分配给函数本身,而不是您运行的函数的返回值。ftp设置后您永远不会更改它,因此无需将其作为参数传入。你可以让它成为一个全球性的或其他的。 -
我该怎么做?
-
@BooWalker 同样在代码的开头,就在该打印语句之前,您有
deleteAllFiles(ftp)。 -
@TheLizzard 逻辑是一样的,但我试过了,还是一样
-
在当前代码中,没有任何东西可以直接调用该函数(除了在单击时会调用它的按钮和函数本身,但不计算在内),所以这不应该发生,我有不知道为什么它会被调用,也许@TheLizzard 可以提供帮助
标签: python tkinter tkinter-button