【发布时间】:2018-10-01 19:38:39
【问题描述】:
我试图让它在我单击 QPushButton 时绘制一条线。然而,我现在拥有的代码在代码启动时在 beginning 处而不是 在 之后。 QPushButton 似乎不做任何绘图。
我也不太明白为什么在绘图时需要在函数中添加“事件”参数。
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QGridLayout,QPushButton, QApplication, QWidget
from PyQt5.QtCore import QSize, QCoreApplication, Qt
from PyQt5.QtGui import QPainter, QPen
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(300, 300))
pybutton = QPushButton('button', self)
pybutton.clicked.connect(self.paintEvent)
pybutton.resize(100,100)
pybutton.move(100, 100)
def paintEvent(self,event):
print('click')
painter = QPainter(self)
pen = QPen(Qt.red, 3)
painter.setPen(pen)
painter.drawLine(0,0,100,100)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )
【问题讨论】:
标签: python line pyqt5 qpushbutton paintevent