Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 接收python类型错误:应为str、bytes或os.PathLike对象,而不是StringVar_Python 3.x_Tkinter_Tk - Fatal编程技术网

Python 3.x 接收python类型错误:应为str、bytes或os.PathLike对象,而不是StringVar

Python 3.x 接收python类型错误:应为str、bytes或os.PathLike对象,而不是StringVar,python-3.x,tkinter,tk,Python 3.x,Tkinter,Tk,我试图为python脚本创建一组tkinter按钮,该脚本读取包含文件名列表的文本文件,然后在用户选择的目录中搜索这些名称,并将找到的任何文件复制到用户指定的其他位置 但我现在收到了这个错误,因为我已经插入了按钮 with open(filePath, "r") as fh: TypeError: expected str, bytes or os.PathLike object, not StringVar 下面是代码: import os import sh

我试图为python脚本创建一组tkinter按钮,该脚本读取包含文件名列表的文本文件,然后在用户选择的目录中搜索这些名称,并将找到的任何文件复制到用户指定的其他位置

但我现在收到了这个错误,因为我已经插入了按钮


    with open(filePath, "r") as fh:
TypeError: expected str, bytes or os.PathLike object, not StringVar

下面是代码:

import os
import shutil
from tkinter import filedialog
from tkinter import *

def browse_button():
    filePath = filedialog.askopenfilename()
    
def browse_button():
    global folder_path
    folderPath = filedialog.askdirectory()

def browse_button():
    global destination
    destination = filedialog.askdirectory()

root = Tk()

filePath = StringVar()
lbl1 = Label(master=root,textvariable=filePath)
lbl1.grid(row=0, column=1)
button2 = Button(text="Browse Text File", command=browse_button)
button2.grid(row=0, column=3)


folderPath = StringVar()
lbl1 = Label(master=root,textvariable=folderPath)
lbl1.grid(row=0, column=1)
button2 = Button(text="Browse Source Folder", command=browse_button)
button2.grid(row=0, column=6)


destination = StringVar()
lbl1 = Label(master=root,textvariable=destination)
lbl1.grid(row=0, column=1)
button2 = Button(text="Browse Destination Folder", command=browse_button)
button2.grid(row=0, column=9)
 
filesToFind = []
with open(filePath, "r") as fh:
    for row in fh:
        filesToFind.append(row.strip())


for filename in os.listdir(folderPath):
    if filename in filesToFind:
        filename = os.path.join(folderPath, filename)
        shutil.copy(filename, destination)

mainloop()

更新代码如下:


import os
import shutil
from tkinter import filedialog
from tkinter import *

def browse_button_1():
    global filePath
    filePath = filedialog.askopenfilename()

def browse_button_2():
    global folder_path
    folderPath = filedialog.askdirectory()

def browse_button_3():
    global destination
    destination = filedialog.askdirectory()

def browse_button_4():
    global run
    run = runScript()

root = Tk()

def runScript():

    filesToFind = []
    with open(filePath.get(), "r") as fh:
        for row in fh:
            filesToFind.append(row.strip())

    for filename in os.listdir(folderPath.get()):
        if filename in filesToFind:
            filename = os.path.join(folderPath.get(), filename)
            shutil.copy(filename, destination.get())

filePath = StringVar()
lbl1 = Label(master=root,textvariable=filePath)
lbl1.grid(row=0, column=1)
button2 = Button(text="Browse Text File", command=browse_button_1)
button2.grid(row=0, column=3)


folderPath = StringVar()
lbl1 = Label(master=root,textvariable=folderPath)
lbl1.grid(row=0, column=1)
button3 = Button(text="Browse Source Folder", command=browse_button_2)
button3.grid(row=0, column=6)


destination = StringVar()
lbl1 = Label(master=root,textvariable=destination)
lbl1.grid(row=0, column=1)
button4 = Button(text="Browse Destination Folder", command=browse_button_3)
button4.grid(row=0, column=9)


run = StringVar()
lbl1 = Label(master=root,textvariable=run)
lbl1.grid(row=0, column=1)
button5 = Button(text="Run Script", command=runScript)
button5.grid(row=0, column=12)

mainloop()


是更新的错误代码:

稍有不同的问题,我收到此错误:


Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/Users/********/Documents/test.py", line 30, in runScript
    with open(filePath.get(), "r") as fh:
AttributeError: 'str' object has no attribute 'get'


正如错误所说,
filePath
StringVar
,因此使用
filePath.get()
。但是,
filePath
此时应为空。您应该将代码块放入函数中,然后通过单击按钮来调用它。另外,为什么定义了三个
browse_button()
函数?感谢您的回复,非常感谢,我应该将filePath.get()放在哪里?我想有三个按钮一个用于文本文件,另一个用于源目录,最后一个用于目标目录,这样做是否错误?您应该创建另一个按钮来执行文件复制。将从
filesToFind=[]
mainloop()
之前的行的块放到函数中(当然,将所有
filePath
更改为
filePath.get(),
folderPath
folderPath.get(),
destination
destination.get())并将该功能分配给新按钮的
命令
参数。另外,您需要重命名三个
浏览器按钮()
函数。您好,acw1668,很抱歉收音机静音,感谢您迄今为止的帮助!我已经更新了上面的代码,现在我收到了一个稍微不同的错误,您可以在上面看到。我已经应用了您的更改,但是否遗漏了什么?我将filestofind/for循环定义为一个函数,但不希望从按钮调用它。我没有正确定义函数吗?
StringVar
在那些
browse\u button\u x()
函数中被覆盖。