亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用按鈕單擊創建新窗口

如何使用按鈕單擊創建新窗口

收到一只叮咚 2022-05-24 13:07:59
我想在單擊按鈕時創建一個新窗口。稍后我將根據輸入的數據動態創建窗口。但我想先從簡單開始。from PyQt5 import QtWidgetsfrom PyQt5.QtWidgets import QPushButton, QGridLayout, QWidgetclass MyWindow(QtWidgets.QMainWindow, QPushButton):    def __init__(self):        super(MyWindow, self).__init__()        centralWidget = QWidget()        self.setCentralWidget(centralWidget)        self.setWindowTitle("ASSET")        self.Button = QPushButton('Action',self)        self.Button.clicked.connect(self.Action)        self.layout = QGridLayout(centralWidget)        self.layout.addWidget(self.Button)    def Action(self):        passif __name__ == "__main__":    import sys    app = QtWidgets.QApplication(sys.argv)    window = MyWindow()    window.show()    sys.exit(app.exec_())
查看完整描述

1 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

您可以創建另一個QMainWindow(),當單擊按鈕時,激活該show()方法


from PyQt5 import QtWidgets

from PyQt5.QtWidgets import QPushButton, QGridLayout, QWidget, QLabel


class NewWindow(QtWidgets.QMainWindow):

    def __init__(self, parent=None):

        super(NewWindow, self).__init__(parent)


        self.label = QLabel('New Window!')

        centralWidget = QWidget()

        self.setCentralWidget(centralWidget)

        self.layout = QGridLayout(centralWidget)

        self.layout.addWidget(self.label)


class MyWindow(QtWidgets.QMainWindow, QPushButton):

    def __init__(self):

        super(MyWindow, self).__init__()

        centralWidget = QWidget()

        self.setCentralWidget(centralWidget)

        self.setWindowTitle("ASSET")

        self.Button = QPushButton('Action',self)

        self.Button.clicked.connect(self.Action)

        self.layout = QGridLayout(centralWidget)

        self.layout.addWidget(self.Button)


        self.new_window = NewWindow(self)


    def Action(self):

        self.new_window.show()


if __name__ == "__main__":

    import sys


    app = QtWidgets.QApplication(sys.argv)

    window = MyWindow()

    window.show()

    sys.exit(app.exec_())

http://img1.sycdn.imooc.com//628c684c0001fcb102550310.jpg

查看完整回答
反對 回復 2022-05-24
  • 1 回答
  • 0 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號