tkinter Button实例没有调用方法

时间:2017-01-09 15:12:31

标签: python tkinter

我正在获得一个没有__call__方法的 Button 实例。我已经检查并尝试过所有事情(至少我是这么认为的)。有人可以解释为什么会这样吗?

#! python2
#find_file.py  - program will find every file with file format
#which user will input and then will copy all files to new folder

from Tkinter import *
import tkMessageBox, os, shutil

def launch_start():
    #Input folder path to check
    check_folder_path = cfp_string.get()
    selective_copy(check_folder_path)

#screen buttons
def selective_copy(folder):
    #walk through folder tree
    for folderName, subfolders, filenames in os.walk(folder):
        #print("Enter the new folder path: ")
        new_folder_path = nfp_string.get()
        os.mkdir(new_folder_path)
        #print("Enter file extension:")
        file_extension = fe_string.get()
        for filename in filenames:
            #for file nam need to add full path
            full_file_name = os.path.join(folder, filename)
            if filename.endswith(file_extension):
                #print("This is png file in this folder.")
                shutil.copy(full_file_name, new_folder_path)
                #print(filename, "\n")
        break

#main window
window = Tk()
window.title("_Find your files_")
window.geometry("580x210")
frame = Frame(window)
frame.pack()

我是Python新手,但这几天困扰着我。

# first text and entry box
fe_string = StringVar()
file_extension_message = Label(frame, text="Enter file extension:",    fg="black")
file_extension_message.grid(row=0, column=0,padx=20, pady=10)
file_extension_E = Entry(frame, textvariable=fe_string)
file_extension_E.grid(row=0, column=1, padx=20, pady=20) 
cfp_string = StringVar()
check_folder_message = Label(frame, text="Enter the path,\nwhere you want to    check for files:", fg="black")
check_folder_message.grid(row=50, column=0, padx=20, pady=20)
check_folder_path_E = Entry(frame, textvariable=cfp_string)
check_folder_path_E.grid(row=50, column=1, padx=20, pady=20)
nfp_string = StringVar()
new_folder_message = Label(frame, text="Enter the path of new folder, \nwhere you want to collect your files:", fg="black")
new_folder_message.grid(row=100, column=0, padx=20, pady=20)
new_folder_path_E = Entry(frame, textvariable=nfp_string)
new_folder_path_E.grid(row=100, column=1, padx=20, pady=20)

#Button
selective_copy = Button(frame, text="Find and copy files", command=launch_start, width=20, height=1)
selective_copy.grid(row=100, column=2, padx=20, pady=20)

window.mainloop()

1 个答案:

答案 0 :(得分:1)

您首先定义方法selective_copy,然后通过定义具有相同名称的新Button来覆盖它。确保为不同的对象使用不同的变量名称。

您收到异常是因为无法像函数一样调用Button对象。