GUI界面实现文件拷贝
时间: 2023-09-01 15:10:58 浏览: 49
好的,您可以使用Python自带的tkinter模块来实现一个简单的GUI界面,然后使用shutil模块来完成文件拷贝的功能。
以下是一个简单的实现示例:
```python
import tkinter as tk
from tkinter import filedialog
import shutil
import os
class CopyFileGUI:
def __init__(self, master):
self.master = master
master.title("文件拷贝")
# 创建选择源文件按钮
self.src_button = tk.Button(master, text="选择源文件", command=self.choose_src_file)
self.src_button.pack()
# 创建选择目标文件夹按钮
self.dest_button = tk.Button(master, text="选择目标文件夹", command=self.choose_dest_folder)
self.dest_button.pack()
# 创建拷贝按钮
self.copy_button = tk.Button(master, text="开始拷贝", command=self.copy_file)
self.copy_button.pack()
# 创建状态标签
self.status_label = tk.Label(master, text="")
self.status_label.pack()
# 初始化源文件和目标文件夹路径
self.src_path = ""
self.dest_path = ""
def choose_src_file(self):
# 弹出选择文件对话框
self.src_path = filedialog.askopenfilename()
print(self.src_path)
def choose_dest_folder(self):
# 弹出选择目标文件夹对话框
self.dest_path = filedialog.askdirectory()
print(self.dest_path)
def copy_file(self):
if not self.src_path:
self.status_label.config(text="请选择源文件!")
return
if not self.dest_path:
self.status_label.config(text="请选择目标文件夹!")
return
try:
# 拷贝文件
shutil.copy2(self.src_path, os.path.join(self.dest_path, os.path.basename(self.src_path)))
self.status_label.config(text="文件拷贝成功!")
except Exception as e:
self.status_label.config(text="文件拷贝失败:" + str(e))
```
运行以上代码,即可看到一个简单的文件拷贝GUI界面。