I have created a very simple GUI using the designer. I am now trying to draw a circle between two push buttons,but for some reason I cannot get right.. I tried a couple of methods I searched online, but none of them helped me.
this is my code:
from PyQt5 import QtCore, QtGui, QtWidgets
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Dialog")
MainWindow.resize(298, 207)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.turnRed = QtWidgets.QPushButton(MainWindow)
self.turnRed.setGeometry(QtCore.QRect(70, 90, 75, 23))
self.turnRed.setObjectName("turnRed")
self.turnBlue = QtWidgets.QPushButton(MainWindow)
self.turnBlue.setGeometry(QtCore.QRect(190, 90, 75, 23))
self.turnBlue.setObjectName("turnBlue")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def paintEvent(self, e):
painter = QtGui.QPainter(self)
painter.setPen(QtGui.QPen(QtCore.Qt.blue, 8, QtCore.Qt.SolidLine))
painter.drawEllipse(120,60,50,50)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("Dialog", "Dialog"))
self.turnRed.setText(_translate("Dialog", "RED"))
self.turnBlue.setText(_translate("Dialog", "GREEN"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
And this is the GUI:
When I run this I see both push buttons but I can't see the ellipse/circle? What am I missing out??
note
I know it's bad to change the code generated from the designer, but all I want for this things is to see the ellipse so I can implement the same thing on my project.
If you know that it is bad to change the code generated from the constructor, so why do you pop it?
A class that inherits from object
does not have apaintEvent
method
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Dialog")
MainWindow.resize(298, 207)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.turnRed = QtWidgets.QPushButton(MainWindow)
self.turnRed.setGeometry(QtCore.QRect(70, 90, 75, 23))
self.turnRed.setObjectName("turnRed")
self.turnBlue = QtWidgets.QPushButton(MainWindow)
self.turnBlue.setGeometry(QtCore.QRect(190, 90, 75, 23))
self.turnBlue.setObjectName("turnBlue")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("Dialog", "Dialog"))
self.turnRed.setText(_translate("Dialog", "RED"))
self.turnBlue.setText(_translate("Dialog", "GREEN"))
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def paintEvent(self, e):
painter = QtGui.QPainter(self)
painter.setPen(QtGui.QPen(QtCore.Qt.blue, 8, QtCore.Qt.SolidLine))
painter.drawEllipse(120, 60, 50, 50)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
Update
But now that I am trying to change the color of painter, it does not let me.. So basically when I run this I get the blue circle, but when I try to change it (by clicked RED button and connect it by a function), nothing happens. could you give me a hand with that please?
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("Dialog")
MainWindow.resize(298, 207)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.turnRed = QtWidgets.QPushButton(MainWindow)
self.turnRed.setGeometry(QtCore.QRect(70, 90, 75, 23))
self.turnRed.setObjectName("turnRed")
self.turnBlue = QtWidgets.QPushButton(MainWindow)
self.turnBlue.setGeometry(QtCore.QRect(190, 90, 75, 23))
self.turnBlue.setObjectName("turnBlue")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("Dialog", "Dialog"))
self.turnRed.setText(_translate("Dialog", "RED"))
self.turnBlue.setText(_translate("Dialog", "GREEN"))
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.pen = QtGui.QPen(QtCore.Qt.blue, 8, QtCore.Qt.SolidLine)
self.ui.turnRed.clicked.connect(self.onRed)
self.ui.turnBlue.clicked.connect(self.onGreen)
def paintEvent(self, e):
painter = QtGui.QPainter(self)
painter.setPen(self.pen)
painter.drawEllipse(120, 60, 50, 50)
def onRed(self):
self.pen = QtGui.QPen(QtCore.Qt.red, 8, QtCore.Qt.SolidLine)
self.update()
def onGreen(self):
self.pen = QtGui.QPen(QtCore.Qt.green, 8, QtCore.Qt.SolidLine)
self.update()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())