按下按钮执行功能(tkinter)

时间:2017-02-08 10:27:20

标签: python tkinter

我正在尝试编写一个小程序,允许用户单击按钮选择文件,输入库存号,然后单击按钮重命名这些文件。

这是我到目前为止所提出的:

import os
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilenames

root = Tk()
root.title("Photo Renamer")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

directory = ''
arrayOfFiles = []
stockNum = 0

def getStockNumber():
    stockNum = stockNumber.get()

def selectFiles():
    selectedFiles = askopenfilenames()
    # Switch our files from a tuple to an array.
    # arrayOfFiles = []
    for i in selectedFiles:
        arrayOfFiles.append(i)
    return arrayOfFiles

def rename(array): # array will be arrayOfFiles
    # Separate file names from the whole file path.
    fileNames = []
    for i in range(len(array)):
        fileNames.append(os.path.basename(i))

    count = 1
    directory = os.path.dirname(array[1])
    for file in directory:
        oldFileName = '%s/%s' % (directory, file)
        newFileName = '%s/gma%d_%d.jpg' % (directory, stockNum, count)
        os.rename(oldFileName, newFileName)
        count += 1

# "Stock Number" label
ttk.Label(mainframe, text="Stock Number: ").grid(column=1, row=1, sticky=W)

# Entry box
stockNumber = ttk.Entry(mainframe, width=7)
stockNumber.grid(column=2, row=1, sticky=(W, E))

# "Select Files" button
ttk.Button(mainframe, text="Select Files", command=selectFiles).grid(column=1, row=2, sticky=(W, E))

# "Number of Files" label
ttk.Label(mainframe, text="Number of Files: ").grid(column=1, row=3, sticky=W)

# "Rename" button
ttk.Button(mainframe, text="Rename", command= lambda: rename(arrayOfFiles)).grid(column=3, row=3, sticky=(W, E)) # command=rename

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
stockNumber.focus()

root.mainloop()

我可以选择文件,然后输入库存号,但是当我点击重命名按钮时,它会在控制台中显示此错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
    return self.func(*args)
  File "C:/Users/.../Desktop/Python/gui.py", line 56, in <lambda>
    ttk.Button(mainframe, text="Rename", command= lambda: rename(arrayOfFiles)).grid(column=3, row=3, sticky=(W, E)) # command=rename
  File "C:/Users/.../Desktop/Python/gui.py", line 32, in rename
    fileNames.append(os.path.basename(i))
  File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\ntpath.py", line 232, in basename
    return split(p)[1]
  File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\ntpath.py", line 204, in split
    d, p = splitdrive(p)
  File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\ntpath.py", line 139, in splitdrive
    if len(p) >= 2:
TypeError: object of type 'int' has no len()

3 个答案:

答案 0 :(得分:1)

for i in range(len(array)):
    fileNames.append(os.path.basename(i))

在上面的代码中,i是一个整数,os.path.basename(path)需要一个类似字符串的路径名。

迭代列表时,您可以直接遍历项目。

for file in array:
    fileNames.append(os.path.basename(file))

或者如果你想使用索引,你可以做

for i in range(len(array)):
    fileNames.append(os.path.basename(array[i]))
                                      ^^^^^ notice accessing an item using index

答案 1 :(得分:1)

list.append()需要一个字符串 - 你给出一个整数。 变化

fileNames.append(os.path.basename(int))

fileNames.append(os.path.basename(str))

在尝试获取os时,Python代码(len(int)模块see the docs over herePython 2))中出现错误,这是不可能的(因为{ {1}}没有方法int

答案 2 :(得分:0)

试试这个:

for i in range(array):

而不是

for i in range(len(array)):