【问题标题】:How to open new window with push button [duplicate]如何用按钮打开新窗口[重复]
【发布时间】:2019-10-21 14:23:37
【问题描述】:

如何打开一个允许我从以下代码中选择时间的新窗口?我尝试使用connect函数连接到windows2,但似乎出现错误。

我想通过 Dropbox 选择时间,我可以在上午 10 点、上午 11 点等之前选择时间。有谁知道您也可以如何实现这一点?

class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        self.label = QLabel()
        self.calendar = QCalendarWidget()
        self.title="Select date from calendar"
        self.left = 600
        self.top = 300
        self.width = 500
        self.height = 480
        self.iconName = "home.png"
        self.setWindowTitle(self.title)
        self.setWindowIcon(QtGui.QIcon(self.iconName))
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.proceedbutton = QPushButton("Proceed to select time", self)
        self.proceedbutton.setGeometry(290, 430, 190, 40)
        self.proceedbutton.setToolTip("<h3>Start the Session</h3>")
        self.proceedbutton.clicked.connect(self.window2)

        self.hide()

        self.backbutton = QPushButton("Back", self)
        self.backbutton.setGeometry(200, 430, 80, 40)
        self.backbutton.setToolTip("<h3>Start the Session</h3>")

        self.Calendar()
        self.show()

    def Calendar(self):
        CalendarVbox = QVBoxLayout()
        self.calendar.setGridVisible(True)

        self.label.setFont(QtGui.QFont("Sanserif", 10))
        self.label.setStyleSheet('color:black')
        CalendarVbox.addWidget(self.calendar)
        CalendarVbox.addWidget(self.label)

        self.setLayout(CalendarVbox)
        self.calendar.selectionChanged.connect(self.onSelectionChanged)

    def window2(self):
        self.label = QLabel("Select Time", self)
        self.label.move(200,430)
        self.setWindowTitle("Select Time")
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.show()

    def onSelectionChanged(self):
        ca = self.calendar.selectedDate()
        self.label.setText(ca.toString())

App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:

    开始使用布局

    没有父级的小部件 - 有一个窗口。

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    
    class Window(QWidget):         #(QMainWindow):
        def __init__(self):
            super().__init__()
    
            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.clicked.connect(self.window2)
    
            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 = QWidget() 
            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)
    
            self.comboBox = QtWidgets.QComboBox()
            self.comboBox.addItems(["choose time", "10", "11", "12"])       
            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())
    
        def onComboActivated(self, text):
            print("choose time: {}".format(text))
    
    if __name__ == '__main__':
        App = QApplication(sys.argv)
        window = Window()
        window.show()
        sys.exit(App.exec())
    

    【讨论】:

      猜你喜欢
      • 2020-12-13
      • 2012-06-17
      • 1970-01-01
      • 2011-06-06
      • 2017-06-14
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多