3.主窗口类型QMainWindow,QWidget,QDialog

本文介绍了PyQt5中三种主要的窗口类型:QMainWindow、QWidget和QDialog,并提供了一个使用QMainWindow创建带有按钮的窗口的示例,演示了如何通过按钮获取屏幕和窗口尺寸,以及调整窗口大小和位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主窗口类型:

QMainWindow: 可以包含菜单栏,工具栏,状态栏和标题栏,是最常见的窗口形式

QWidget:  不确定窗口的用途的时候, 就用这个。

QDialog: 是对话窗口的基类。是没有工具栏,状态栏,菜单栏的。

 

简单案例:

from PyQt5.QtWidgets import QApplication,QWidget,QMainWindow,QPushButton,QHBoxLayout,QDesktopWidget
import  sys

class QuitApplication(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui_init()

    def ui_init(self):
        self.setWindowTitle('let window go')
        self.resize(400,400)

        self.button1 = QPushButton('quit window')
        self.button1.clicked.connect(self.onClicked_Button_quit)
        self.button2 = QPushButton('get x,y,h,w, size')
        self.button2.clicked.connect(self.onClicked_Button_getSize)

        layout = QHBoxLayout()
        mainFrame = QWidget()

        layout.addWidget(self.button1)
        layout.addWidget(self.button2)
        mainFrame.setLayout(layout)
        self.setCentralWidget(mainFrame)

    def onClicked_Button_quit(self):
        sender = self.sender()
        print(sender.text() + 'button clicked')
        app = QApplication.instance()
        app.quit()

    def onClicked_Button_getSize(self):
        screen = QDesktopWidget().screenGeometry()
        size = self.geometry()
        print('screen.size:{}x{}'.format(screen.width(), screen.height()))
        print('widget.size:{}x{}'.format(size.x(), size.y()))
        print('widget.size:{}x{}'.format(size.width(), size.height()))

        newLeft = (screen.width() - size.width()) /2
        newTop  = (screen.height() - size.height()) /2

        self.move(newLeft, newTop)
        self.resize(600,600)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QuitApplication()
    main.show()
    sys.exit(app.exec_())

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值