简体   繁体   English

如何制作一个由pyqt5中的按钮触发的圆圈?

[英]how to make a circle that is triggered by a button in pyqt5?

Code for adding a circle in pyqt5 window on clicking a button单击按钮时在pyqt5窗口中添加圆圈的代码

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow,QPushButton,QWidget
from PyQt5 import QtGui
from PyQt5.QtCore import QRect,Qt
from PyQt5.QtGui import QPainter,QBrush, QPen
from PyQt5 import QtCore


class Window(QMainWindow):
   def __init__(self):
        super().__init__()
        title="layout management"
        left=500
        top=200
        width=500
        height=400
        iconName="fosseeicon.jpg"
        self.setWindowTitle(title)
        self.setWindowIcon(QtGui.QIcon(iconName))
        self.setGeometry(left, top, width, height)
        self.windowcomponents()
        self.show()
   def windowcomponents(self):
       button=QPushButton("Add", self)
       button.setGeometry(QRect(0, 0, 50, 28))
       button.setIcon(QtGui.QIcon("addbutton.png"))
       button.setToolTip("<h3>This is for creating random circles<h3>")
       button.clicked.connect(self.paintcircle)
       button=QPushButton("Generate Report", self)
       button.setGeometry(QRect(49,0,150,28))
       button.setIcon(QtGui.QIcon("generatereport.png"))
       button.setToolTip("This is for generating pdf report of connection between two circles")
       button=QPushButton("Save", self)
       button.setGeometry(QRect(199,0,120,28))
       button.setIcon(QtGui.QIcon("saveicon.png"))
       button.setToolTip("This is for saving an image of canvas area")

   def paintcircle(self, event):
       painter.begin(self)
       painter.setRenderHint(QPainter.Antialiasing)
       painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
       painter.drawEllipse(100, 100, 100, 100)
app = QApplication(sys.argv)
ex = Window()
circle=paintcircle()
circle.show()
sys.exit(app.exec_())

here, i am getting many errors:在这里,我遇到了很多错误:

*Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/DishaKhattri/PycharmProjects/helloworld/helloworld.py", line 45, in <module>
    circle=paintcircle()
NameError: name 'paintcircle' is not defined*

The usual wayIf you want to draw extra shapes on your widget is to override paintEvent , create a QPainter object, and use this painter to draw the shapes you want.通常的方法如果你想在你的小部件上绘制额外的形状是覆盖paintEvent ,创建一个QPainter对象,并使用这个画家来绘制你想要的形状。 In your case, since you only want to draw the circle under certain circumstances, you could use a flag to indicate whether the circle should be drawn or not, and only draw the circle in paintEvent when this flag is True, eg在您的情况下,由于您只想在某些情况下绘制圆圈,您可以使用一个标志来指示是否应该绘制圆圈,并且仅当此标志为 True 时才在paintEvent绘制圆圈,例如

class Window(QMainWindow):
    def __init__(self):
        ....
        self.should_paint_circle = False

    def paintEvent(self, event):
        super().paintEvent(event)
        if self.should_paint_circle:
            painter = QtGui.QPainter(self)
            painter.setRenderHint(QPainter.Antialiasing)
            painter.setPen(QPen(Qt.black, 5, Qt.SolidLine))
            painter.drawEllipse(100, 100, 100, 100)

    def paintcircle(self):
        self.should_paint_circle = True
        self.update()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM