python PyQt6学习笔记 有注释

PyQt 当前日期和时间

from PyQt6.QtCore import QDate, QTime, QDateTime, Qt


now=QDate.currentDate()
print(now)
print(now.toString(Qt.DateFormat.ISODate))
print(now.toString(Qt.DateFormat.RFC2822Date))

datetime = QDateTime.currentDateTime()
print(datetime,type(datetime),str(datetime))
print(datetime.toString())

PyQt6 的第一个程序

import sys
from PyQt6.QtWidgets import QApplication, QWidget
def main():
    app = QApplication(sys.argv)
    w = QWidget()
    w.resize(600, 500)
    w.move(500,300)

    w.setWindowTitle('第一个程序')
    w.show()

    sys.exit(app.exec())

if __name__ == '__main__':
    main()

创建按钮

import sys
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QFont


class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        QToolTip.setFont(QFont('SansSerif', 10))#设置字体,10pt的SansSerif字体
        self.setToolTip('<b>背景提示</b>')#提示框,可以使用富文本内容

        btn = QPushButton('Button', self)
        btn.setToolTip('按钮提示')#添加了一个按钮部件

        btn.resize(btn.sizeHint())#系统建议的尺寸
        btn.move(50, 50)#移动按钮的位置

        self.setGeometry(300, 300,500, 200)#设置窗口位置和大小
        self.setWindowTitle('Tooltips')
        self.show()
def main():

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec())
if __name__ == '__main__':
    main()

添加退出功能

btn.clicked.connect(QApplication.instance().quit)#事件处理插槽可以是 Qt 自带的插槽,也可以是普通 Python 函数

弹窗(用不了)

import sys
from PyQt6.QtWidgets import QWidget, QMessageBox, QApplication

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.setGeometry(300, 300, 350, 200)
        self.setWindowTitle('Message box')
        self.show()
    def closeEvent(self, event):
        reply = QMessageBox.question(self, 'Message',
                    "Are you sure to quit?", QMessageBox.StandardButtons.Yes |
                    QMessageBox.StandardButtons.No, QMessageBox.StandardButtons.No)
        if reply == QMessageBox.StandardButtons.Yes:
            event.accept()
        else:
            event.ignore()
def main():
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec())
if __name__ == '__main__':
    main()

窗口居中

import sys
from PyQt6.QtWidgets import QWidget, QApplication

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.resize(350, 250)
        self.center()
        self.setWindowTitle('Center')
        self.show()
    def center(self):#自定义 center 方法
        qr = self.frameGeometry()#得到一个矩形的窗口
        print(qr)
        cp = self.screen().availableGeometry().center()#从屏幕属性里计算出分辨率,然后计算出中心点位置。
        print(self.screen().availableGeometry(),cp)
        qr.moveCenter(cp)#把矩形窗口的中心点放置到屏幕窗口的中心点
        self.move(qr.topLeft())#左上方点坐标移动到矩形窗口的左上方
def main():
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec())
if __name__ == '__main__':
    main()

 创建状态栏

self.statusBar().showMessage('Ready')#创建状态栏

​​​​​​​

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值