用pyqt将B盘下的软件包解压代码
时间: 2024-05-12 20:19:23 浏览: 56
软件解包工具
以下是使用PyQt5实现在B盘下解压缩软件包的示例代码:
```python
import os
import zipfile
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QFileDialog, QMessageBox
class App(QWidget):
def __init__(self):
super().__init__()
self.title = '解压缩软件包'
self.left = 100
self.top = 100
self.width = 400
self.height = 150
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.lbl_path = QLabel('选择文件:', self)
self.lbl_path.move(20, 20)
self.txt_path = QLineEdit(self)
self.txt_path.move(100, 20)
self.txt_path.resize(200, 20)
self.btn_path = QPushButton('浏览', self)
self.btn_path.move(310, 20)
self.btn_path.clicked.connect(self.openFileDialog)
self.btn_unzip = QPushButton('解压缩', self)
self.btn_unzip.move(20, 60)
self.btn_unzip.clicked.connect(self.unzip)
self.show()
def openFileDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(self, '选择文件', 'B:\\', 'Zip Files (*.zip)', options=options)
if fileName:
self.txt_path.setText(fileName)
def unzip(self):
file_path = self.txt_path.text()
if not file_path.endswith('.zip'):
QMessageBox.warning(self, '错误', '请选择zip文件!')
return
if not os.path.exists(file_path):
QMessageBox.warning(self, '错误', '文件不存在!')
return
with zipfile.ZipFile(file_path, 'r') as zip_ref:
zip_ref.extractall('B:\\')
QMessageBox.information(self, '提示', '解压缩完成!')
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
```
运行代码后,窗口中有一个文本框和两个按钮,可以选择B盘下的zip文件并解压缩到B盘根目录。
阅读全文