读PyQt6教程,用PyQt5来写,报如下提示DeprecationWarning: sipPyTypeDict() is deprecated
DeprecationWarning: sipPyTypeDict() is deprecated PyQt6代码在PyQt5中异常
文章共388字 · 阅读需要大约2分钟
一键AI生成摘要,助你高效阅读
问答
·
DeprecationWarning: sipPyTypeDict() is deprecated, the extension module should use sipPyTypeDictRef() instead class MyWindow(QWidget):
安装PyQt6,提示消失。各处也运行正常。
如下代码,按Esc键关闭窗口,在PyQt5中,按任意键均关闭。
import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QHBoxLayout
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import Qt
class MyWindow(QWidget):
"""从QWidget类派生的桌面应用程序窗口类"""
def __init__(self):
"""构造函数"""
super().__init__() # 调用基类的构造函数
self.setWindowTitle('事件和事件函数')
self.setGeometry(400, 300, 320, 80)
self.setWindowIcon(QIcon('res/Folder lila.ico'))
self.initUI()
self.show()
def initUI(self):
"""初始化界面"""
lab = QLabel('按Esc键关闭窗口')
box = QHBoxLayout()
box.addStretch(1)
box.addWidget(lab)
box.addStretch(1)
self.setLayout(box)
def keyPressEvent(self, evt):
"""重写按键事件函数"""
if evt.key() == Qt.Key.Key_Escape.value:
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
sys.exit(app.exec())
5下获取鼠标坐标也是异常。鼠标进入不显示坐标,直接闪退。
def enterEvent(self, evt):
"""重写进入事件函数"""
for item in dir(evt):
print(item)
pos = evt.position()
self.lab.setText('x=%d, y=%d' % (pos.x(), pos.y()))
5下,事件过滤器,运行闪退
def eventFilter(self, objwatched, evt):
"""事件过滤器"""
if evt.type() == QEvent.Type.KeyPress.value:
print('忽略按键事件')
return True
return super().eventFilter(objwatched, evt)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
app.installEventFilter(win)
sys.exit(app.exec())
更多推荐
已为社区贡献1条内容
所有评论(0)