0

I'm trying to make my PyQt5 GUI in OOP way for example class for window and another for Pushbutton I'm really confiuse how to make it.Here is my code and my try to make pushbutton class.also my goal is to make classes for each of the items in the window

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
     def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        #making Pushbutton
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(180, 210, 93, 28))
        self.pushButton.setStyleSheet("QPushButton::setCheckable(bool)")
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
       self.pushButton.setText(_translate("MainWindow", "PushButton"))
# here I'm trying to make Buttom class
class Button(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Button, self).__init__(parent)


# Main function
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Izzo
  • 9
  • 3
  • Sorry but your question is really unclear (and it also seems incomplete). Can you clarify what you're trying to achieve? – musicamante Jun 05 '20 at 14:28
  • I'm trying to write all iteams in my window as classes – Izzo Jun 05 '20 at 14:31
  • I'm not sure I understand what you mean. Do you want to create subclasses for *each* widget you're using? What's the reason of that? – musicamante Jun 05 '20 at 14:33
  • yes subclasses I think It would be better and more elegant – Izzo Jun 05 '20 at 14:34
  • @Izzo Better and more elegant? I don't see that that is true. If you want to create a custom QPushButton then inherit from QPushButton, and then change `self.pushButton = QtWidgets.QPushButton(self.centralwidget)` with `self.pushButton = Button(self.centralwidget)` – eyllanesc Jun 05 '20 at 14:37
  • maybe I'm wrong but anyway I want to learn – Izzo Jun 05 '20 at 14:41
  • There's nothing wrong in being eager to learn. But the purpose should not be confused with the method. By looking at your code you seem to be still learning how PyQt works, and I strongly suggest you to begin with understanding how Qt, its classes and subclasses behave, and by studying the documentation. As a side note, consider that you should *never* edit the output of `pyuic` (nor try to mimic its code). Read more about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html) to understand how to use ui files. – musicamante Jun 05 '20 at 14:51
  • thanks for the advice – Izzo Jun 05 '20 at 14:56
  • [Differences between Procedural and Object Oriented Programming](https://www.geeksforgeeks.org/differences-between-procedural-and-object-oriented-programming/) [Functional vs Object-Oriented vs Procedural Programming](https://medium.com/@LiliOuakninFelsen/functional-vs-object-oriented-vs-procedural-programming-a3d4585557f3) – pippo1980 Jun 05 '20 at 16:31
  • musicamante and eyllanesc are very good experts and resourcefull but as a beginner myself I found not easy to start with Qt documentation, I mean I dont understand C and I am missing very basic concepts about programming. Its easy to find tutorial on PyQt5 , its more difficult to find out a tutorial that starts with basic programming concepts. – pippo1980 Jun 05 '20 at 16:51
  • I think my problem is understanding the pyqt classes therefore I just need the very basic example to how converting it to oop to make me understand the whole idea – Izzo Jun 05 '20 at 17:32
  • Object oriented programming can be defined as a programming model which is based upon the concept of objects. Objects contain data in the form of attributes and code in the form of methods. In object oriented programming, computer programs are designed using the concept of objects that interact with real world. Object oriented programming languages are various but the most popular ones are class-based, meaning that objects are instances of classes, which also determine their types. – pippo1980 Jun 05 '20 at 17:36
  • [The QObject class is the base class of all Qt objects](https://doc.qt.io/qt-5/qobject.html) click on more to have better description – pippo1980 Jun 05 '20 at 17:41
  • [MIT6001](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/readings/) I started from here but got stuck at lesson 4, OOP is at 8. Slides are nice [slides lesson 8](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/MIT6_0001F16_Lec8.pdf) – pippo1980 Jun 05 '20 at 17:45
  • thanks a lot for helping – Izzo Jun 05 '20 at 18:26

1 Answers1

0

from Subclassing of QPushButton :

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jun  6 16:21:24 2020

@author: Pietro
"""

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        #making Pushbutton
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(180, 210, 93, 28))
        self.pushButton.setStyleSheet("QPushButton::setCheckable(bool)")
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.pippo=CustomButtonClass(self.centralwidget)
        self.pippo.show()


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))

# here I'm trying to make Buttom class
class CustomButtonClass(QtWidgets.QPushButton):
    def __init__(self, *args, **kwargs):
        QtWidgets.QPushButton.__init__(self,*args, **kwargs)
        self.setGeometry(QtCore.QRect(280, 310, 93, 28))
        self.setText('customized')
        self.hide()
        self.setStyleSheet("QPushButton{\n"
            "    background-color: #9de650;\n"
            "}\n"
            "\n"
            "\n"
            "QPushButton:hover{\n"
            "    background-color: green;\n"
            "}\n"
            "\n"
            "")

# Main function
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
pippo1980
  • 2,181
  • 3
  • 14
  • 30