小编ToS*_*ity的帖子

小部件调用 show() 后 resizeEvent 不起作用

# code#1
import sys
from PyQt5.QtWidgets import QApplication, QPushButton

def onResize(event):
    print("Nice to get here!")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = QPushButton('Test')
    widget.resize(640, 480)
    widget.show()
    widget.resizeEvent = onResize
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

新的 resizeEvent 在这段代码中永远不会被触发#1(当我手动调整窗口大小时)。

# code#2
import sys
from PyQt5.QtWidgets import QApplication, QPushButton

def onResize(event):
    print("Nice to get here!")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = QPushButton('Test')
    widget.resize(640, 480)
    widget.resizeEvent = onResize
    widget.show()
    sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

新的 resizeEvent 在此代码中很好地触发#2(当我手动调整窗口大小时)。我可以看到打印出的消息。

有谁知道原因吗?即使我在 code#1 中添加widget.update()andwidget.show()之后widget.resizeEvent = onResize …

python pyqt pyqt5

3
推荐指数
1
解决办法
2069
查看次数

标签 统计

pyqt ×1

pyqt5 ×1

python ×1