# -*- coding: utf-8 -*-
# Creator: zhu
# Time: 2023-12-13
import xlwt
import sys, base64, xlrd, re, datetime
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QFileDialog, QLabel, QMessageBox, QRadioButton, QComboBox
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply
from PyQt5.QtCore import QUrl
import time
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'CFS文件处理'
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
# 创建布局盒子
layout = QVBoxLayout()
self.setLayout(layout)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
? ? ? ? ? ? ? ? 点击QPushButton, 下滑菜单QComboBox, 单选按钮QRadioButton
def initUI(self):
self.setWindowTitle(self.title)
# QBoxLayout 盒子布局 layout虚拟容器
layout = QVBoxLayout()
background_label = QLabel(self)
background_label.setPixmap(QPixmap("C:\\Users\\VSPN\\Desktop\\xml\\cd.png"))
layout.addWidget(background_label)
self.resize(400, 300) # Set the window size to 600x900
# Create a button to open the file dialog
# 自定义button 大小 调用绑定方法 openFileNameDialog
self.button = QPushButton('上传文件', self)
self.button.setFixedWidth(465)
self.button.setFixedHeight(50)
self.button.clicked.connect(self.openFileNameDialog)
# 创建下拉菜单并添加选项
self.combo_box = QComboBox()
self.combo_box.addItem('txt')
self.combo_box.addItem('excl')
# ---------------
self.download_btn = QPushButton('下载文件', self)
self.download_btn .clicked.connect(self.downloadFile)
self.download_btn.setFixedWidth(465)
self.download_btn.setFixedHeight(50)
# 创建两个单选按钮
self.radio_button1 = QRadioButton("Option 1")
self.radio_button2 = QRadioButton("Option 2")
# 选中选项变化时触发的方法
self.radio_button1.toggled.connect(self.onRadioButtonToggled)
self.radio_button2.toggled.connect(self.onRadioButtonToggled)
# Layout 虚拟容器 通过addWidget(QWidget) / RemoveWidget(QWidget) 往里面添加 / 移除组件并实现多组件自动布局
# 横向布局的是QHboxLayout(), 纵向布局的就是QVboxLayout(),
layout.addWidget(self.button)
layout.addWidget(self.combo_box)
layout.addWidget(self.download_btn)
self.setLayout(layout)
self.show()
def openFileNameDialog(self):
# Open file dialog and get the selected file path
# 设置文件对话框的选项
options = QFileDialog.Options()
fileName, _ = QFileDialog.getOpenFileName(self, "QFileDialog.getOpenFileName()", "", "All Files (*);;Python Files (*.py)", options=options)
if fileName:
try:
# 此处文件处理逻辑 入mysql或者其他操作
# xls_to_sql(fileName)
print(fileName)
self.fileName = fileName
# QMessageBox弹窗提示
QMessageBox.information(self, 'success', "上传文件成功", QMessageBox.Ok)
except Exception as e:
QMessageBox.information(self, 'Error', "上传文件失败", QMessageBox.Ok)
????????????????????????此为远程连接下载,亦可改成sql查询写文件等
def downloadFiles(self):
# Handle download operation here
url = 'http://cfsraffle.vspnit.com/raffle/download' # 文件 URL
filename = 'raffle.xls' # 保存的文件名
url = QUrl(url)
# 创建 QNetworkAccessManager 对象
manager = QNetworkAccessManager(self)
# 创建请求并发送到服务器获取文件内容
try:
# QUrl类型的url,不接受str类型的url
request = QNetworkRequest(url)
reply = manager.get(request)
except Exception as e:
print(e)
# 等待文件下载完成
# while not reply.isFinished():
# QMessageBox.information(self, 'success', "开始下载文件", QMessageBox.Ok)
# pass
# 检查是否下载成功
QMessageBox.information(self, 'success', "CFS中奖名单下载中", QMessageBox.Ok)
time.sleep(5)
if reply.error() != QNetworkReply.NoError:
print(f"Error: {reply.error()}")
QMessageBox.information(self, 'errr', "CFS中奖名单下载文件失败", QMessageBox.Ok)
else:
print("File downloaded successfully!")
QMessageBox.information(self, 'success', "CFS中奖名单下载文件成功", QMessageBox.Ok)
# 将文件保存到本地
with open(filename, 'wb') as f:
while not reply.isReadable():
pass
device = reply.readAll()
f.write(device)
print(f"File saved as {filename}")
def qComboBoxToggled(self):
# 获取下拉菜单选择的选项
selected_item = self.combo_box.currentText()
if selected_item == 'txt':
QMessageBox.information(self, 'success', "下载txt成功", QMessageBox.Ok)
elif selected_item == 'excl':
QMessageBox.information(self, 'success', "下载excl成功", QMessageBox.Ok)
else:
return # 处理无效选项的情况
def onRadioButtonToggled(self, checked):
# 获取下拉菜单选择的选项
if checked:
if self.sender() == self.radio_button1:
self.selected_value = "Option 1"
print("Option 1")
elif self.sender() == self.radio_button2:
self.selected_value = "Option 2"
print("Option 2")