【问题标题】:Python 3.2 Tkinter - typed in directory to be source for file copyingPython 3.2 Tkinter - 输入目录作为文件复制的源
【发布时间】: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


【解决方案1】:

我做了一些修改:

  • 使用class(更好的编码技术)
  • 使用filedialog(用于获取路径)
  • 已创建Browse 按钮
  • 修改了copyymovee 的代码。

这里是:

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()

如果您有任何疑问,我很乐意提供帮助:)

【讨论】:

  • 非常漂亮,谢谢!虽然移动按钮在我刚刚复制了复制按钮的代码并将其更改为 shutil.copyshutil.move 之前不起作用,但不知道为什么。现在它复制而不是目录中的所有 .jpgs,只是一个特定的,它有点改变了工具的用途。除此之外-您帮助我在这件事上取得了进步,并向我展示了我必须做更多研究的东西:)所以非常感谢,我会继续学习并尝试新事物!再次感谢:)
  • 是的,我忘记更改了..抱歉
  • 如果你想获取目录中的所有文件你想查看os.walk()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多