[英]Executing a function on button press (tkinter)
I'm trying to write a small program that allows the user to click a button to select files, type in a stock number, and then click a button to rename those files. 我正在尝试编写一个小程序,该程序允许用户单击按钮以选择文件,键入股票编号,然后单击按钮以重命名那些文件。
This is what I've come up with so far: 到目前为止,这是我想出的:
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()
I can select the files, and type in the stock number, but when I click the rename button, it gives me this error in the console: 我可以选择文件,然后输入库存编号,但是当我单击“重命名”按钮时,它将在控制台中显示以下错误:
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()
for i in range(len(array)):
fileNames.append(os.path.basename(i))
In your above code, i
is an integer and os.path.basename(path) expects a pathname like string. 在上面的代码中,
i
是一个整数,并且os.path.basename(path)需要一个类似于字符串的路径名。
When iterating over a list, you can direcly iterate through items. 遍历列表时,可以直接遍历项目。
for file in array:
fileNames.append(os.path.basename(file))
or if you want to use indexes, you can do 或者如果您想使用索引,则可以
for i in range(len(array)):
fileNames.append(os.path.basename(array[i]))
^^^^^ notice accessing an item using index
list.append() requires a string - you're giving an integer. list.append()需要一个字符串-您要提供一个整数。 Change
更改
fileNames.append(os.path.basename(int))
to 至
fileNames.append(os.path.basename(str))
The error was thrown within the Python code ( os
module, see the docs over here ( Python 2 )) when it tried to get the len(int)
, which is impossible (since int
doesn't have the method __len__
) 当尝试获取
len(int)
,该错误在Python代码( os
模块, 请参阅此处的文档 ( Python 2 ))中引发,这是不可能的(因为int
没有方法__len__
)
Try this: 尝试这个:
for i in range(array):
Instead of 代替
for i in range(len(array)):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.