【发布时间】:2014-04-19 09:20:20
【问题描述】:
我正在尝试制作一个简单的工具,它将具有特定扩展名的文件从一个地方复制/移动到另一个地方。一切都很好,但我正在尝试输入您要从中复制的目录,然后选择您要复制到的目录。
myGUI=Tk()
myGUI.geometry("400x200+100+200")
myGUI.title('Copy dat')
Source=StringVar()
Destination=StringVar()
MySource=Entry(myGUI, textvariable=Source).grid(row=9, column=2)
MyDestination=Entry(myGUI, textvariable=Destination).grid(row=10, column=2)
def copyy():
source = os.listdir("Source")
destination = "Destination"
for files in source:
if files.endswith(".jpg"):
shutil.copy(files, destination)
button1=Button(myGUI, text=" Copy ", command=copyy).grid(row=3, column=0)
但是如果我点击我的按钮,错误消息会说 windows 找不到名为 /Source 的目录或类似的东西。所以我明白source = os.listdir("Source")是问题所在。而且我猜destination = "Destination" 也是不正确的。
如果我将整个路径放在代码中,则复制按钮效果很好,但我希望工具的用户可以在窗口中写入路径。请帮忙。
编辑:如果需要,整个代码:
import shutil
import os
from tkinter import *
myGUI=Tk()
myGUI.geometry("400x200+100+200")
myGUI.title('Copy dat')
Source=StringVar()
Destination=StringVar()
MySource=Entry(myGUI, textvariable=Source).grid(row=9, column=2)
MyDestination=Entry(myGUI, textvariable=Destination).grid(row=10, column=2)
def copyy():
source = os.listdir('Source')
destination = "Destination"
for files in source:
if files.endswith(".jpg"):
shutil.copy(files, destination)
def movee():
source = os.listdir("C:/Users/PC/Desktop/python testing/")
destination = "C:/Users/PC/Desktop/python testing/destination"
for files in source:
if files.endswith(".jpg"):
shutil.move(files, destination)
label1=Label(myGUI, text='Welcome to the copy utility', fg='Blue').grid(row=0,column=2)
label2=Label(myGUI, text='Ultimate JPG mover', fg='Black').grid(row=1,column=0)
label3=Label(myGUI, text='(Thing\'s actually pretty useless)', fg='Black').grid(row=2,column=0)
button1=Button(myGUI, text=" Copy ", command=copyy).grid(row=3, column=0)
button2=Button(myGUI, text=" Move ", command=movee).grid(row=5, column=0)
myGUI.mainloop()
【问题讨论】:
-
为此目的使用
filedialog,也请浏览按钮 -
如果你想这样继续,你想用
binding&get()来获取Entry中输入的文字
标签: python-3.x path tkinter copy move