| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- import sys
- import os
- from PyQt5.QtCore import Qt, QUrl, QDateTime
- from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QFileDialog, QMessageBox, QDateTimeEdit, QPushButton
- from PyQt5.QtGui import QDesktopServices
- from litter_helper import Ui_menu # 确保这个导入路径是正确的
- class ClickableLabel(QLabel):
- def __init__(self, parent=None, file_path=None):
- super().__init__(parent)
- self.file_path = file_path
- def mousePressEvent(self, event):
- if event.button() == Qt.LeftButton:
- self.emit_click_event()
- def emit_click_event(self):
- if self.file_path:
- self.parent().on_label_click(self.file_path)
- def get_resource_path(relative_path):
- """获取资源文件的绝对路径"""
- if getattr(sys, 'frozen', False): # 是否为PyInstaller打包的exe文件
- # 返回exe文件所在的绝对路径
- base_path = os.path.dirname(sys.executable)
- else: # 在开发环境下运行
- # 返回脚本文件所在的绝对路径
- base_path = os.path.dirname(__file__)
- return os.path.join(base_path, relative_path)
- class MainWindow(QMainWindow, Ui_menu):
- def __init__(self, param1, param2):
- super().__init__()
- self.setupUi(self)
- # 使用传递的参数
- self.param1 = param1
- self.param2 = param2
- # 设置标签的文本为传递的参数
- # self.label.setText(f"参数1: {self.param1}, 参数2: {self.param2}")
- # 登录账号
- self.loginPushButton.clicked.connect(self.on_login_button_click)
- # 开始下单
- self.startPushButton.clicked.connect(self.on_start_button_click)
- # 终止执行
- self.endPushButton.clicked.connect(self.on_stop_button_click)
- # 编辑账号
- # 获取 QLabel 控件并替换为 ClickableLabel
- self.accountsLabel = self.findChild(QLabel, "accountsLabel")
- if self.accountsLabel:
- # 替换为 ClickableLabel
- self.clickableAccountsLabel = ClickableLabel(self, file_path=get_resource_path("accounts.txt"))
- self.clickableAccountsLabel.setObjectName("accountsLabel")
- self.clickableAccountsLabel.setText("编辑账号")
- self.clickableAccountsLabel.setGeometry(self.accountsLabel.geometry())
- self.clickableAccountsLabel.show()
- self.accountsLabel.deleteLater() # 删除原来的 QLabel 控件
- # 编辑IP代理池
- # 获取 QLabel 控件并替换为 ClickableLabel
- self.ipPoolLabel = self.findChild(QLabel, "ipPoolLabel")
- if self.ipPoolLabel:
- # 替换为 ClickableLabel
- self.clickableIpPoolLabel = ClickableLabel(self, file_path=get_resource_path("proxy_list.txt"))
- self.clickableIpPoolLabel.setObjectName("ipPoolLabel")
- self.clickableIpPoolLabel.setText("编辑代理池")
- self.clickableIpPoolLabel.setGeometry(self.ipPoolLabel.geometry())
- self.clickableIpPoolLabel.show()
- self.ipPoolLabel.deleteLater() # 删除原来的 QLabel 控件
- # 获取 QDateTimeEdit 控件并设置显示格式
- self.startDateTimeEdit = self.findChild(QDateTimeEdit, "startDateTimeEdit")
- if self.startDateTimeEdit:
- self.startDateTimeEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
- # 设置当前时间
- self.startDateTimeEdit.setDateTime(QDateTime.currentDateTime())
- # 获取 QDateTimeEdit 控件并设置显示格式
- self.endDateTimeEdit = self.findChild(QDateTimeEdit, "endDateTimeEdit")
- if self.endDateTimeEdit:
- self.endDateTimeEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
- # 设置当前时间
- self.endDateTimeEdit.setDateTime(QDateTime.currentDateTime())
- # 连接 watchPushButton 点击事件到槽函数
- self.watchPushButton = self.findChild(QPushButton, "watchPushButton")
- if self.watchPushButton:
- self.watchPushButton.clicked.connect(self.on_watch_button_click)
- def on_label_click(self, file_path):
- if os.path.exists(file_path):
- try:
- QDesktopServices.openUrl(QUrl.fromLocalFile(file_path))
- except Exception as e:
- QMessageBox.critical(self, "Error", f"无法打开文件: {e}")
- else:
- QMessageBox.critical(self, "Error", f"文件不存在: {file_path}")
- def on_start_button_click(self):
- # 在按钮点击时执行的操作
- print(f"按钮被点击了!参数1: {self.param1}, 参数2: {self.param2}")
- # 获取 QDateTimeEdit 控件的值
- if self.startDateTimeEdit:
- date_time_value = self.startDateTimeEdit.dateTime()
- print(f"选中的日期和时间: {date_time_value.toString('yyyy-MM-dd HH:mm:ss')}")
- # 获取 QDateTimeEdit 控件的值
- if self.endDateTimeEdit:
- date_time_value = self.endDateTimeEdit.dateTime()
- print(f"选中的日期和时间: {date_time_value.toString('yyyy-MM-dd HH:mm:ss')}")
- # 获取并发数
- if self.concurrencyLineEdit:
- print(f"并发数为: {self.concurrencyLineEdit.text()}")
- # 登录账号
- def on_login_button_click(self):
- print(f"登录账号! {self.urlLineEdit.text()}")
- # 终止下单
- def on_stop_button_click(self):
- print(f"终止下单! 目标网址: {self.urlLineEdit.text()}")
- def edit_accounts(self):
- # 编辑账号
- print(f"编辑账号: {self.urlLineEdit.text()}")
- def on_watch_button_click(self):
- # 获取项目当前路径
- current_path = sys.path[0]
- # 拼接 txt 文件路径
- file_path = get_resource_path("results/2024-05-27.txt")
- try:
- QDesktopServices.openUrl(QUrl.fromLocalFile(file_path))
- except Exception as e:
- QMessageBox.critical(self, "Error", f"无法打开文件: {e}")
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- # 创建窗口并传递参数
- window = MainWindow("Hello", "World")
- window.show()
- sys.exit(app.exec_())
|