开发者社区> 问答> 正文

如何在Pyqt5中使用按钮生成一个圆

单击按钮在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_())

在这里,我遇到很多错误:

\*raceback (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\*        

问题来源:stackoverflow

展开
收起
is大龙 2020-03-25 09:22:23 4148 0
1 条回答
写回答
取消 提交回答
  • 通常的方法如果要在窗口小部件上绘制额外的形状,则是要覆盖paintEvent,创建一个QPainter对象,然后使用此painter绘制所需的形状。在您的情况下,由于您只想在某些情况下绘制圆,因此可以使用一个标志来指示是否应绘制该圆,并且仅当此标志为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()
    

    回答来源:stackoverflow

    2020-03-25 09:22:30
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载