PYQT5如何按日期"mon"、"tues"等打开新的window
PYQT5 How to open a new window by date "mon" , "tues", etc
如何在日历上选择日期后打开新的 window?我在哪里可以根据日期打开不同的windows?
下面是我的代码:
我试过使用 if-else 但无法获取数据。如何通过读取获取日历数据的标签中的单词来打开新的 window?
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())
如果您需要根据工作日执行不同的操作,您需要做的就是将 clicked
信号连接到将使用 QCalendar.selectedDate().dayOfWeek()
的函数(它有一个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...
如何在日历上选择日期后打开新的 window?我在哪里可以根据日期打开不同的windows?
下面是我的代码: 我试过使用 if-else 但无法获取数据。如何通过读取获取日历数据的标签中的单词来打开新的 window?
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())
如果您需要根据工作日执行不同的操作,您需要做的就是将 clicked
信号连接到将使用 QCalendar.selectedDate().dayOfWeek()
的函数(它有一个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...