Python 3.2 Tkinter-键入目录作为文件复制的源 [英] Python 3.2 Tkinter - typed in directory to be source for file copying
问题描述
我正在尝试制作一个简单的工具,该工具会将具有特定扩展名的文件从一个地方复制/移动到另一个地方.一切工作都很好,但是我正在尝试键入要复制的目录,然后选择要复制到的目录.
I am trying to make a simple tool which will copy/move files with certain extension from one place to another. Everything is working out pretty much fine, but I am trying to make it possible to type in the directory where you want to copy from, and choose the directory where you want to copy to.
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"
也不正确.
如果我将整个路径放在代码中,则复制按钮可以很好地工作,但是我希望工具的用户可以在窗口中写入路径.请帮忙.
But if I click on my button, the error message says that windows can't find directory named /Source or something like that. So I understand source = os.listdir("Source")
is the problem. And I guess destination = "Destination"
is also incorect.
If I put the whole path in the code the copy button works well, but I want it to be possible to write the path in the window by the user of the tool. Please help.
整个代码(如果需要)
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()
推荐答案
我进行了一些修改:
- 使用了
class
(更好的编码技术) - 使用了
filedialog
(用于获取路径) - 创建了
Browse
按钮 - 修改了
copyy
&的代码movee
.
- Used
class
(better coding technique) - Used
filedialog
(for getting the path) - Created
Browse
button - Modified your code for
copyy
&movee
.
这是:
import shutil
import os
from tkinter import *
import tkinter.filedialog as fdialog
class MainClass():
def __init__(self,master):
self.parent=master
self.gui()
def gui(self):
self.Source=StringVar()
self.Destination=StringVar()
MySource=Entry(myGUI, textvariable=self.Source).grid(row=9, column=2)
browse=Button(myGUI,text="Browse",command=lambda:self.Source.set(fdialog.askopenfilename(filetypes=[("JPEG File",'.jpg')]))).grid(row=9, column=3)
MyDestination=Entry(myGUI, textvariable=self.Destination).grid(row=10, column=2)
browse1=Button(myGUI,text="Browse",command=lambda:self.Destination.set(fdialog.askdirectory())).grid(row=10, column=3)
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=self.copyy).grid(row=3, column=0)
button2=Button(myGUI, text=" Move ", command=self.movee).grid(row=5, column=0)
def copyy(self):
source_file=self.Source.get()
if source_file.endswith(".jpg"):
shutil.copy(source_file, self.Destination.get())
def movee(self):
source_file=self.Source.get()
if source_file.endswith(".jpg"):
shutil.move(source_file, self.Destination.get())
if __name__ == '__main__':
myGUI=Tk()
app=MainClass(myGUI)
myGUI.geometry("400x200+100+200")
myGUI.title('Copy dat')
myGUI.mainloop()
如果您有任何疑问,我很乐意为您提供帮助:)
If you have any doubts,I would be happy to help :)
这篇关于Python 3.2 Tkinter-键入目录作为文件复制的源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!