如何在PyQt5的另一个函数中使用QDateEdit组件的用户设置日期
Hey there! Let's get your Run button working properly to grab the current date from your QDateEdit component. The main issue in your current code is that dateSelect is a local variable inside the group1 method—so your btn1_onClicked function can't access it. Plus, trying to call onDateChanged() directly won't work because that method expects a date parameter from the signal.
Here's how to adjust your code:
Step 1: Convert dateSelect to an Instance Variable
Instead of creating dateSelect as a local variable, make it a property of your Window class using self.dateSelect. This lets all methods in the class access it.
Step 2: Update the Button Click Handler
In btn1_onClicked, directly fetch the current date from self.dateSelect instead of trying to call onDateChanged. You can format it the same way you do in the date change handler.
Modified Full Code
import sys from PyQt5 import QtGui from PyQt5.QtCore import Qt, QDate from PyQt5.QtWidgets import (QApplication, QCheckBox, QGridLayout, QGroupBox, QMenu, QPushButton, QRadioButton, QVBoxLayout, QWidget, QFrame, QDateEdit) class Window(QWidget): def __init__(self, parent=None): super(Window, self).__init__(parent) self.setWindowIcon(QtGui.QIcon('logo.png')) self.setWindowTitle("Test") self.resize(400, 300) grid = QGridLayout() grid.addWidget(self.group1(), 0, 0) self.setLayout(grid) def group1(self): groupBox = QGroupBox("Box 1") date = QDate.currentDate().addDays(-1) # Make dateSelect an instance variable self.dateSelect = QDateEdit() self.dateSelect.setDate(date) self.dateSelect.dateChanged.connect(self.onDateChanged) checkbox1 = QCheckBox("Task 1") checkbox2 = QCheckBox("Task 2") checkbox3 = QCheckBox("Task 3") button1 = QPushButton('Run') button1.setMaximumWidth(75) button1.clicked.connect(self.btn1_onClicked) button2 = QPushButton('Run') button2.setMaximumWidth(75) separatorLine = QFrame() separatorLine.setFrameShape(QFrame.HLine) separatorLine.setFrameShadow(QFrame.Sunken) vbox = QVBoxLayout() vbox.addWidget(self.dateSelect) vbox.addWidget(checkbox1) vbox.addWidget(checkbox2) vbox.addWidget(button1) vbox.addWidget(separatorLine) vbox.addWidget(checkbox3) vbox.addWidget(button2) vbox.addStretch(1) groupBox.setLayout(vbox) return groupBox def btn1_onClicked(self): # Get the current date from the QDateEdit instance current_date = self.dateSelect.date().toString('yyyy-MM-dd') print(f"Run button clicked, using date: {current_date}") # You can use current_date in your task logic here def onDateChanged(self, newDate): date = newDate.toString('yyyy-MM-dd') print(f"Date changed to: {date}") return date if __name__ == '__main__': app = QApplication(sys.argv) clock = Window() clock.show() sys.exit(app.exec_())
Key Changes Explained
- Instance Variable: By using
self.dateSelect, we make the date picker accessible anywhere in theWindowclass, including the button click handler. - Direct Date Fetch: In
btn1_onClicked,self.dateSelect.date()gives us the currentQDateobject, which we can format to a string just like inonDateChanged. This is the most reliable way to get the current value when the button is clicked—no need to rely on storing the date from thedateChangedsignal (though you could do that too, but direct access is simpler).
Now when you click the Run button, it will grab whatever date is currently selected in the QDateEdit and print it (or use it in your task logic).
内容的提问来源于stack exchange,提问作者A-Aron

