【问题标题】:tkinter runs code without pressing button [duplicate]tkinter 无需按下按钮即可运行代码[重复]
【发布时间】: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


【解决方案1】:

改变

command=deleteAllFiles(ftp)

command=lambda: deleteAllFiles(ftp)

所以您不会立即调用该函数,而是将其包装在 lambda 中。

【讨论】:

  • 启动时仍然执行命令
  • @BooWalker 这个解决方案不是原因,您只需在代码中间调用它(ftp.cwd('htdocs') 之后)
  • @Matiiss 在这种情况下我该怎么办
  • @BooWalker 非常明显,删除或注释掉调用该函数的行
  • @Matiiss 仍然是同样的问题,我无法理解它在哪里,这不是你在说什么吗? #deleteAllFiles(ftp)
猜你喜欢
  • 2011-02-26
  • 2015-07-19
  • 2018-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多