如何让 window 一次打开一个? PYQT5
How do i make one window open at a time ? PYQT5
如何使代码以一种方式工作,当我单击继续按钮时,'select time' 页面出现但日历页面隐藏?
当我点击继续按钮时,两个页面都打开了。
预期的输出是一次只打开一页!!
我的代码:
class Window(QWidget):
def __init__(self):
super().__init__()
self.window = QWidget()
self.title = "Select date from calendar"
self.left, self.top, self.width, self.height = 600, 100, 500, 480
self.iconName = "Ok.png" # <--- home.png
self.setWindowTitle(self.title)
self.setWindowIcon(QtGui.QIcon(self.iconName))
self.setGeometry(self.left, self.top, self.width, self.height)
self.calendar = QCalendarWidget()
self.calendar.setGridVisible(True)
self.calendar.selectionChanged.connect(self.onSelectionChanged)
self.label = QLabel()
self.label.setFont(QtGui.QFont("Sanserif", 10))
self.label.setStyleSheet('color: blue;')
self.proceedbutton = QPushButton("Proceed to select time", self)
self.proceedbutton.setToolTip("<h3>Start the Session</h3>")
self.proceedbutton.setEnabled(False)
self.proceedbutton.clicked.connect(self.window2)
self.show()
self.hide()
self.backbutton = QPushButton("Back", self)
self.backbutton.setToolTip("<h3>Start the Session</h3>")
self.comboBox = None
self.grid = QtWidgets.QGridLayout(self)
self.grid.addWidget(self.calendar, 0, 0, 1, 3)
self.grid.addWidget(self.label, 1, 0, 1, 3)
self.grid.addWidget(self.backbutton, 2, 1, 1, 1)
self.grid.addWidget(self.proceedbutton, 2, 2, 1, 1)
def window2(self):
self.window.setWindowTitle("Select Time")
self.window.setGeometry(self.left / 3, self.top, self.width / 3, self.height / 3)
self.label = QLabel("Select Time")
self.comboBox = QtWidgets.QComboBox()
self.comboBox.addItems(["choose time", "0700", "0800", "0900", "1000", "1100", "1200", "1300",
"1400", "1500","1600","1700","1800","1900","2000","2100", "2200"])
self.comboBox.activated[str].connect(self.onComboActivated)
layout = QFormLayout(self.window)
layout.addRow('Choose Time', self.comboBox)
self.window.show()
def onSelectionChanged(self):
ca = self.calendar.selectedDate()
self.label.setText(ca.toString())
self.proceedbutton.setEnabled(True)
def onComboActivated(self, text):
print("choose time: {}".format(text))
if __name__ == '__main__':
App = QApplication([])
window = Window()
window.show()
sys.exit(App.exec())
image
此代码使用QStackedWidget
替换一个window中的内容。
它有两个功能来创建两个小部件
内容 - 它们被放入堆栈 QStackedWidget
。
首先,它使用 self.stack.setCurrentIndex(0)
显示带日历的小部件和运行功能的按钮 self.stack.setCurrentIndex(1)
以显示带时间的小部件。此小部件还有再次运行的按钮 self.stack.setCurrentIndex(0)
以再次显示日历。
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
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()
#self.iconName = "Ok.png" # <--- home.png
#self.setWindowIcon(QtGui.QIcon(self.iconName))
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", "0900", "1000", "1100", "1200", "1300",
"1400", "1500","1600","1700","1800","1900","2000","2100", "2200"])
page.comboBox.activated[str].connect(self.onComboActivated)
layout.addRow('Choose Time', page.comboBox)
page.proceedbutton = QPushButton("Back", page)
page.proceedbutton.clicked.connect(self.show_page1)
layout.addRow(page.proceedbutton)
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(self.left / 3, self.top, self.width / 3, self.height / 3)
self.stack.setCurrentIndex(1)
def onSelectionChanged(self):
ca = self.page1.calendar.selectedDate()
self.page1.label.setText(ca.toString())
self.page1.proceedbutton.setEnabled(True)
def onComboActivated(self, text):
print("choose time: {}".format(text))
if __name__ == '__main__':
App = QApplication([])
window = Window()
window.show()
sys.exit(App.exec())
如何使代码以一种方式工作,当我单击继续按钮时,'select time' 页面出现但日历页面隐藏?
当我点击继续按钮时,两个页面都打开了。
预期的输出是一次只打开一页!!
我的代码:
class Window(QWidget):
def __init__(self):
super().__init__()
self.window = QWidget()
self.title = "Select date from calendar"
self.left, self.top, self.width, self.height = 600, 100, 500, 480
self.iconName = "Ok.png" # <--- home.png
self.setWindowTitle(self.title)
self.setWindowIcon(QtGui.QIcon(self.iconName))
self.setGeometry(self.left, self.top, self.width, self.height)
self.calendar = QCalendarWidget()
self.calendar.setGridVisible(True)
self.calendar.selectionChanged.connect(self.onSelectionChanged)
self.label = QLabel()
self.label.setFont(QtGui.QFont("Sanserif", 10))
self.label.setStyleSheet('color: blue;')
self.proceedbutton = QPushButton("Proceed to select time", self)
self.proceedbutton.setToolTip("<h3>Start the Session</h3>")
self.proceedbutton.setEnabled(False)
self.proceedbutton.clicked.connect(self.window2)
self.show()
self.hide()
self.backbutton = QPushButton("Back", self)
self.backbutton.setToolTip("<h3>Start the Session</h3>")
self.comboBox = None
self.grid = QtWidgets.QGridLayout(self)
self.grid.addWidget(self.calendar, 0, 0, 1, 3)
self.grid.addWidget(self.label, 1, 0, 1, 3)
self.grid.addWidget(self.backbutton, 2, 1, 1, 1)
self.grid.addWidget(self.proceedbutton, 2, 2, 1, 1)
def window2(self):
self.window.setWindowTitle("Select Time")
self.window.setGeometry(self.left / 3, self.top, self.width / 3, self.height / 3)
self.label = QLabel("Select Time")
self.comboBox = QtWidgets.QComboBox()
self.comboBox.addItems(["choose time", "0700", "0800", "0900", "1000", "1100", "1200", "1300",
"1400", "1500","1600","1700","1800","1900","2000","2100", "2200"])
self.comboBox.activated[str].connect(self.onComboActivated)
layout = QFormLayout(self.window)
layout.addRow('Choose Time', self.comboBox)
self.window.show()
def onSelectionChanged(self):
ca = self.calendar.selectedDate()
self.label.setText(ca.toString())
self.proceedbutton.setEnabled(True)
def onComboActivated(self, text):
print("choose time: {}".format(text))
if __name__ == '__main__':
App = QApplication([])
window = Window()
window.show()
sys.exit(App.exec())
image
此代码使用QStackedWidget
替换一个window中的内容。
它有两个功能来创建两个小部件
内容 - 它们被放入堆栈 QStackedWidget
。
首先,它使用 self.stack.setCurrentIndex(0)
显示带日历的小部件和运行功能的按钮 self.stack.setCurrentIndex(1)
以显示带时间的小部件。此小部件还有再次运行的按钮 self.stack.setCurrentIndex(0)
以再次显示日历。
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
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()
#self.iconName = "Ok.png" # <--- home.png
#self.setWindowIcon(QtGui.QIcon(self.iconName))
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", "0900", "1000", "1100", "1200", "1300",
"1400", "1500","1600","1700","1800","1900","2000","2100", "2200"])
page.comboBox.activated[str].connect(self.onComboActivated)
layout.addRow('Choose Time', page.comboBox)
page.proceedbutton = QPushButton("Back", page)
page.proceedbutton.clicked.connect(self.show_page1)
layout.addRow(page.proceedbutton)
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(self.left / 3, self.top, self.width / 3, self.height / 3)
self.stack.setCurrentIndex(1)
def onSelectionChanged(self):
ca = self.page1.calendar.selectedDate()
self.page1.label.setText(ca.toString())
self.page1.proceedbutton.setEnabled(True)
def onComboActivated(self, text):
print("choose time: {}".format(text))
if __name__ == '__main__':
App = QApplication([])
window = Window()
window.show()
sys.exit(App.exec())