此代码可以从电脑磁盘中提取指定的文件(可多选),并将其存入桌面的一个制定文件夹中,做一个收纳功能。下面是一个例子
import sys
import os
import shutil
import tkinter as tk
from tkinter import filedialog
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QLabel, \
QGraphicsView, QFileDialog, QGraphicsScene
from PyQt5.QtCore import Qt, QRectF, QTimer, QTime, QDate
from PyQt5.QtGui import QPalette, QColor, QIcon, QPixmap, QImage, QPainter
class TitleBar(QWidget):
def __init__(self, parent):
super().__init__()
self.parent = parent
self.setFixedHeight(30)
layout = QHBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
self.setLayout(layout)
# 添加更换背景按钮
self.change_background_button = QPushButton(self)
self.change_background_button.setText("更换背景")
self.change_background_button.clicked.connect(self.change_background)
layout.addWidget(self.change_background_button, alignment=Qt.AlignRight | Qt.AlignTop)
# 设置标题栏样式
self.setStyleSheet("""
background-color: #333333;
color: white;
font-size: 14px;
padding-left: 5px;
""")
def change_background(self):
# 创建文件对话框
file_dialog = QFileDialog()
file_path, _ = file_dialog.getOpenFileName()
if file_path:
# 设置页面背景图片
self.parent.set_background(file_path)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drag_position = event.globalPos() - self.frameGeometry().topLeft()
event.accept()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.parent.move(event.globalPos() - self.drag_position)
event.accept()
class AppWindow(QMainWindow):
def __init__(self):
super().__init__()
# 设置窗口大小为屏幕宽度的一半
screen_width = QApplication.desktop().screenGeometry().width()
self.resize(screen_width // 2, self.height())
# 设置页面背景色为蓝色
palette = self.palette()
palette.setColor(QPalette.Window, QColor(0, 0, 255))
self.setPalette(palette)
# 创建标题栏
self.title_bar = TitleBar(self)
self.setMenuWidget(self.title_bar)
layout = QVBoxLayout()
upload_button = QPushButton("上传文件", self)
upload_button.clicked.connect(self.upload_file)
layout.addWidget(upload_button, alignment=Qt.AlignCenter)
self.background_view = QGraphicsView()
layout.addWidget(self.background_view)
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
# 创建时间标签
self.time_label = QLabel(self)
self.time_label.setStyleSheet("""
font-size: 16px;
color: white;
background-color: rgba(0, 0, 0, 0.5);
padding: 5px;
""")
layout.addWidget(self.time_label, alignment=Qt.AlignTop | Qt.AlignLeft)
# 创建定时器,每秒更新一次时间
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_time)
self.timer.start(1000)
def set_background(self, image_path):
# 加载图片
pixmap = QPixmap(image_path)
# 调整图片大小为窗口大小
pixmap = pixmap.scaled(self.size(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
# 创建 QGraphicsScene 和 QGraphicsView
scene = QGraphicsScene(self)
self.background_view.setScene(scene)
self.background_view.setAlignment(Qt.AlignLeft | Qt.AlignTop)
self.background_view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.background_view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
# 在 QGraphicsScene 中添加背景图片
scene.addPixmap(pixmap)
# 设置场景大小与窗口大小一致
scene.setSceneRect(QRectF(self.rect()))
def upload_file(self):
# 创建目标文件夹路径
destination_folder = os.path.expanduser("~/Desktop/文件夹1")
if not os.path.exists(destination_folder):
os.mkdir(destination_folder)
# 创建文件对话框
file_dialog = QFileDialog()
file_paths, _ = file_dialog.getOpenFileNames()
if file_paths:
for file_path in file_paths:
# 获取选中文件的文件名
file_name = os.path.basename(file_path)
# 拷贝文件到目标文件夹
destination_path = os.path.join(destination_folder, file_name)
shutil.copy2(file_path, destination_path)
print("已将文件拷贝到:", destination_path)
def update_time(self):
current_date = QDate.currentDate().year()
current_time = QTime.currentTime().toString("hh:mm")
time_string = f"{current_date}年 {current_time}"
self.time_label.setText(time_string)
if __name__ == '__main__':
# 创建根窗口
root = tk.Tk()
root.withdraw()
# 创建Qt应用程序
app = QApplication(sys.argv)
# 创建AppWindow实例
window = AppWindow()
window.show()
# 运行Qt应用程序
sys.exit(app.exec_())