Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.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中使用字符串变量显示路径?_Python_Windows_Tkinter - Fatal编程技术网

如何在python中使用字符串变量显示路径?

如何在python中使用字符串变量显示路径?,python,windows,tkinter,Python,Windows,Tkinter,所以我想制作一个应用程序,从一个目录中获取文件,然后用该文件替换另一个目录中的文件。我一直在尝试找出如何获取字符串变量并将其显示为路径字符串。我知道,os.replace()不接受字符串变量,所以我尝试创建一个变量,将字符串变量转换为字符串 将tkinter作为tk导入 从tkinter导入文件对话框,ttk 从tkinter进口* 导入操作系统 root=Tk() 根目录。可调整大小(False,False) 根部几何形状(“600x600”) def search1(): 全局文件1_路径

所以我想制作一个应用程序,从一个目录中获取文件,然后用该文件替换另一个目录中的文件。我一直在尝试找出如何获取字符串变量并将其显示为路径字符串。我知道,
os.replace()
不接受字符串变量,所以我尝试创建一个变量,将字符串变量转换为字符串

将tkinter作为tk导入
从tkinter导入文件对话框,ttk
从tkinter进口*
导入操作系统
root=Tk()
根目录。可调整大小(False,False)
根部几何形状(“600x600”)
def search1():
全局文件1_路径
filename1=filedialog.askopenfilename()
file1_path.set(filename1)
打印(文件名1)
def search2():
全局文件2_路径
filename2=filedialog.askopenfilename()
file2_path.set(filename2)
打印(文件名2)
def浏览按钮():
全局文件夹路径
filename=filedialog.askdirectory()
文件夹路径集(文件名)
打印(文件名)
def replacefile():
替换(f'{folder}/game/scripts.rpa',f'{file1}')
替换(f'{folder}/game/images.rpa',f'{file2}')
文件夹路径=StringVar()
file1_path=StringVar()
file2_path=StringVar()
file1=str(file1\u路径)
file2=str(file2\u路径)
folder=str(文件夹路径)
button1=ttk.Button(text=“Browse”,command=search1)
按钮1.位置(relx=1.1,rely=1.01)
按钮1.位置(relx=0.6,rely=0.5)
button3=ttk.Button(text=“Browse”,command=search2)
按钮3.位置(relx=1.1,rely=1.01)
按钮3.place(relx=.6,rely=.54)
button4=ttk.Button(text=“Browse”,command=Browse\u按钮)
按钮4.位置(relx=1.1,rely=1.01)
按钮4.位置(relx=.6,rely=.58)
button2=ttk.Button(text=“Replace”,command=replacefile)
按钮2.位置(relx=0.01,rely=1.01)
按钮2.位置(relx=0.01,rely=0.8)
mainloop()
当我尝试替换时,它在控制台中出现此错误:

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'PY_VAR0/game/scripts.rpa' -> 'PY_VAR1'
Python认为这只是一个变量,而不是字符串的一部分。我对python有点陌生,所以我不知道为什么会发生这种情况。如果有人知道我如何解决这个问题,我将不胜感激。

以下三点:

file1 = str(file1_path)
file2 = str(file2_path)
folder = str(folder_path)
StringVar
的名称分配给三个变量,而不是
StringVar
的内容

您应该在
replacefile()函数中获取
StringVar
的内容:

def replacefile():
    os.replace(f'{folder_path.get()}/game/scripts.rpa', f'{file1_path.get()}')
    os.replace(f'{folder_path.get()}/game/images.rpa', f'{file2_path.get()}')

如果您按下
replace
,它会在窗口后面的控制台命令提示符中显示。3个浏览按钮让您选择一个文件或文件夹,然后尝试在字符串中实现。一旦按下replace按钮,就会键入路径。感谢它帮助修复了我的错误,但是当我尝试替换文件时,它只是删除了文件,而没有替换文件,但是在我沮丧地回来发布另一个问题之前,我可能需要做更多的研究。