问题
how do I open a new window after selecting the date on the calendar? Where I will be able to open different windows according to dates?
Below is my code: I've tried using if-else but I couldn't fetch the data. How can I open a new window by reading the word from the label that fetches the calendar data?
class Window(QWidget):
def __init__(self):
super().__init__()
self.left, self.top, self.width, self.height = 600, 100, 500, 480
self.page1 = self.create_page1()
self.page2 = self.create_page2()
self.stack = QStackedWidget()
self.stack.addWidget(self.page1)
self.stack.addWidget(self.page2)
hbox = QHBoxLayout(self)
hbox.addWidget(self.stack)
self.setLayout(hbox)
self.show_page1()
self.show()
def create_page1(self):
page = QWidget()
page.calendar = QCalendarWidget(page)
page.calendar.setGridVisible(True)
page.calendar.selectionChanged.connect(self.onSelectionChanged)
page.label = QLabel(page)
#page.label.setFont(QtGui.QFont("Sanserif", 10))
page.label.setStyleSheet('color: blue;')
page.proceedbutton = QPushButton("Proceed to select time", page)
page.proceedbutton.setToolTip("<h3>Start the Session</h3>")
page.proceedbutton.setEnabled(False)
page.proceedbutton.clicked.connect(self.show_page2)
page.backbutton = QPushButton("Back", page)
page.backbutton.setToolTip("<h3>Start the Session</h3>")
page.comboBox = None
page.grid = QGridLayout(page)
page.grid.addWidget(page.calendar, 0, 0, 1, 3)
page.grid.addWidget(page.label, 1, 0, 1, 3)
page.grid.addWidget(page.backbutton, 2, 1, 1, 1)
page.grid.addWidget(page.proceedbutton, 2, 2, 1, 1)
return page
def create_page2(self):
page = QWidget()
layout = QFormLayout(page)
page.comboBox = QComboBox(page)
page.comboBox.addItems(["choose time", "0700", "0800"])
page.comboBox.activated[str].connect(self.onComboActivated)
layout.addRow('Choose Time', page.comboBox)
page.viewbutton = QPushButton("View Stalls", page)
page.viewbutton.setEnabled(False)
page.viewbutton.clicked.connect(self.show_page3)
layout.addRow(page.viewbutton)
page.reselectdatebutton = QPushButton("Return to re-select date", page)
page.reselectdatebutton.clicked.connect(self.show_page1)
layout.addRow(page.reselectdatebutton)
return page
def show_page1(self):
self.setWindowTitle("Select date from calendar")
self.setGeometry(self.left, self.top, self.width, self.height)
self.stack.setCurrentIndex(0)
def show_page2(self):
self.setWindowTitle("Select Time")
self.setGeometry(600, 100, 500, 480)
self.stack.setCurrentIndex(1)
def show_page3(self):
self.setWindowTitle("View Stalls")
self.setGeometry(600, 100, 500, 480)
self.stack.setCurrentIndex(2)
def onSelectionChanged(self):
ca = self.page1.calendar.selectedDate()
self.page1.label.setText(ca.toString())
print(ca.toString())
self.page1.proceedbutton.setEnabled(True)
def onComboActivated(self, text):
print("choose time: {}".format(text))
self.page2.viewbutton.setEnabled(True)
if __name__ == '__main__':
App = QApplication([])
window = Window()
window.show()
sys.exit(App.exec())
回答1:
If you need to do a different action according to the week day, all you need to do is to connect the clicked
signal to a function that will use the QCalendar.selectedDate().dayOfWeek() (which has a range(1, 8)
).
class Window(QWidget):
# ...
def create_page3(self):
# ...
page.viewmenubutton.clicked.connect(self.openviewforday)
def openviewforday(self):
weekDay = self.page1.calendar.selectedDate().dayOfWeek()
if weekDay == 1:
# do something for Monday
elif weekDay == 2:
# do something for Tuesday
# etc...
来源:https://stackoverflow.com/questions/58504853/pyqt5-how-to-open-a-new-window-by-date-mon-tues-etc