我正在为一个为不同客户端自动输入数据的软件开发 GUI,现在我正在实施第一个名为 Galicia 的客户端,但我无法使用浏览按钮,该按钮应该允许用户浏览文件数据存储的位置。目前它甚至没有像按钮一样做出反应。 (默认情况下,在 PyQt 中,当鼠标悬停在按钮上时,按钮会覆盖蓝色边框,甚至没有响应。)
这是我的代码
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QComboBox, QPushButton
from PyQt5.QtGui import QIcon, QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget, QLabel, QLineEdit,
QPushButton, QFileDialog, QProgressBar
)
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QSize
from analysis import analyze
class WorkerThread(QThread):
progress_changed = pyqtSignal(int)
def run(self):
# Here you can put your own code to be executed in the main loop
for i in range(101):
self.progress_changed.emit(i)
self.msleep(50)
class GaliciaGUI(QWidget):
def __init__(self):
super().__init__()
central_widget = QWidget(self)
self.search_label = QLabel("Search for a file:", central_widget)
self.search_label.setGeometry(50, 50, 170, 30)
self.search_button = QPushButton("Browse", central_widget)
self.search_button.setGeometry(50, 80, 80, 30)
self.search_button.clicked.connect(self.browse_file)
self.date_start = QLabel("Fecha:", central_widget)
self.date_start.setGeometry(350, 50, 170, 30)
self.date_start_input = QLineEdit(central_widget)
self.date_start_input.setGeometry(350, 80, 250, 30)
self.date_start_input = QLineEdit(central_widget)
self.date_start_input.setGeometry(50, 150, 200, 30)
self.date_start_input.setEchoMode(QLineEdit.Password)
self.selected_file_label = QLabel("", central_widget)
self.selected_file_label.setGeometry(50, 80, 300, 30)
self.password_label = QLabel("Enter password:", central_widget)
self.password_label.setGeometry(50, 120, 150, 30)
self.password_input = QLineEdit(central_widget)
self.password_input.setGeometry(50, 150, 200, 30)
self.password_input.setEchoMode(QLineEdit.Password)
self.start_button = QPushButton("Start", central_widget)
self.start_button.setGeometry(50, 190, 80, 30)
self.start_button.clicked.connect(self.start_main_loop)
self.progress_bar = QProgressBar(central_widget)
self.progress_bar.setGeometry(50, 230, 300, 30)
self.progress_bar.setValue(0)
self.status_label = QLabel("", central_widget)
self.status_label.setGeometry(50, 250, 150, 30)
self.file_path = None
self.worker_thread = WorkerThread()
self.worker_thread.progress_changed.connect(self.update_progress_bar)
def browse_file(self):
file_path, _ = QFileDialog.getOpenFileName(
self, "Select a file", "", "All Files (*);;Text Files (*.txt)"
)
if file_path:
self.selected_file_label.setText(f"Selected file: {file_path}")
self.file_path = file_path
def start_main_loop(self):
password = self.password_input.text() or None
if self.file_path:
self.worker_thread.start()
analyze(self.file_path, password)
def update_progress_bar(self, value):
self.progress_bar.setValue(value)
if value == 100:
self.status_label.setText("Success")
class LandingPage(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Client Selection')
self.setGeometry(100, 100, 400, 300)
self.client_label = QLabel('Select a client:', self)
self.client_label.move(50, 50)
self.client_dropdown = QComboBox(self)
self.client_dropdown.addItems(['Galicia', 'Client 2', 'Client 3']) # Add more clients here
self.client_dropdown.move(150, 50)
self.submit_button = QPushButton('Submit', self)
self.submit_button.move(150, 100)
self.submit_button.clicked.connect(self.open_gui)
self.galicia_gui = None # Initialize instance variable for GaliciaGUI
self.show()
def open_gui(self):
client = self.client_dropdown.currentText()
if client == 'Galicia':
self.galicia_gui = GaliciaGUI()
self.galicia_gui.setGeometry(100, 100, 700, 600)
self.galicia_gui.activateWindow()
self.galicia_gui.raise_()
self.galicia_gui.show()
app = QApplication.instance()
app.setActiveWindow(self.galicia_gui)
elif client == 'Client 2':
# Open Client 2 GUI
pass # Replace client
elif client == 'Client 3':
# Open Client 3 GUI
pass # Replace with client
if __name__ == '__main__':
app = QApplication(sys.argv)
landing_page = LandingPage()
app.exec_()
我尝试实现一个按钮,它可以调出文件资源管理器(该软件适用于 windows),可以在其中浏览所需的文件,然后通过分析功能将所述文件输入到数据处理中。
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QComboBox, QPushButton
from PyQt5.QtGui import QIcon, QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget, QLabel, QLineEdit,
QPushButton, QFileDialog, QProgressBar
)
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QSize
# from analysis import analyze
class WorkerThread(QThread):
progress_changed = pyqtSignal(int)
def run(self):
# Here you can put your own code to be executed in the main loop
for i in range(101):
self.progress_changed.emit(i)
self.msleep(50)
class GaliciaGUI(QWidget):
def __init__(self):
super().__init__()
central_widget = QWidget(self)
self.search_label = QLabel("Search for a file:", central_widget)
self.search_label.setGeometry(50, 50, 170, 30)
# self.browse_file()
self.date_start = QLabel("Fecha:", central_widget)
self.date_start.setGeometry(350, 50, 170, 30)
self.date_start_input = QLineEdit(central_widget)
self.date_start_input.setGeometry(350, 80, 250, 30)
self.date_start_input = QLineEdit(central_widget)
self.date_start_input.setGeometry(50, 150, 200, 30)
self.date_start_input.setEchoMode(QLineEdit.Password)
self.selected_file_label = QLabel("", central_widget)
self.selected_file_label.setGeometry(50, 80, 300, 30)
self.password_label = QLabel("Enter password:", central_widget)
self.password_label.setGeometry(50, 120, 150, 30)
self.password_input = QLineEdit(central_widget)
self.password_input.setGeometry(50, 150, 200, 30)
self.password_input.setEchoMode(QLineEdit.Password)
self.start_button = QPushButton("Start", central_widget)
self.start_button.setGeometry(50, 190, 80, 30)
self.start_button.clicked.connect(self.start_main_loop)
self.progress_bar = QProgressBar(central_widget)
self.progress_bar.setGeometry(50, 230, 300, 30)
self.progress_bar.setValue(0)
self.status_label = QLabel("", central_widget)
self.status_label.setGeometry(50, 250, 150, 30)
self.search_button = QPushButton("Browse", central_widget)
self.search_button.setGeometry(50, 80, 80, 30)
self.search_button.pressed.connect(lambda: self.browse_file())
self.file_path = None
self.worker_thread = WorkerThread()
self.worker_thread.progress_changed.connect(self.update_progress_bar)
def browse_file(self):
file_path, _ = QFileDialog.getOpenFileName(
self, "Select a file", "", "All Files (*);;Text Files (*.txt)"
)
if file_path:
self.selected_file_label.setText(f"Selected file: {file_path}")
self.file_path = file_path
def start_main_loop(self):
password = self.password_input.text() or None
if self.file_path:
self.worker_thread.start()
analyze(self.file_path, password)
def update_progress_bar(self, value):
self.progress_bar.setValue(value)
if value == 100:
self.status_label.setText("Success")
class LandingPage(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Client Selection')
self.setGeometry(100, 100, 400, 300)
self.client_label = QLabel('Select a client:', self)
self.client_label.move(50, 50)
self.client_dropdown = QComboBox(self)
self.client_dropdown.addItems(['Galicia', 'Client 2', 'Client 3']) # Add more clients here
self.client_dropdown.move(150, 50)
self.submit_button = QPushButton('Submit', self)
self.submit_button.move(150, 100)
self.submit_button.clicked.connect(self.open_gui)
self.galicia_gui = None # Initialize instance variable for GaliciaGUI
self.show()
def open_gui(self):
client = self.client_dropdown.currentText()
if client == 'Galicia':
self.galicia_gui = GaliciaGUI()
self.galicia_gui.setGeometry(100, 100, 700, 600)
self.galicia_gui.activateWindow()
self.galicia_gui.raise_()
self.galicia_gui.show()
app = QApplication.instance()
app.setActiveWindow(self.galicia_gui)
elif client == 'Client 2':
# Open Client 2 GUI
pass # Replace client
elif client == 'Client 3':
# Open Client 3 GUI
pass # Replace with client
if __name__ == '__main__':
app = QApplication(sys.argv)
landing_page = LandingPage()
app.exec_()
(不要忘记验证答案。谢谢)