Python Forum
A Simple PyQt5 Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A Simple PyQt5 Class
#6
@zoro I am not sure why @Axel_Erfurt continues to propagate bad code as he is fully aware by now that using Super() is more dangerous than using explicit declarations. Further it is not easier since you have to make sure to code in a particular way as to compensate for the 3 known issues that using Super() creates. Finally Super() was initially created to fix an very rare issue with using the explicit methodology and if you are not using it for this reason then you are creating more issues than you are fixing.

Next @Axel_Erfurt is also aware that sys.exit(app.exec_()) is PyQt4 coding and that PyQt5 now uses app.exec() and further that QApplication(sys.argv) is not quality since QApplication([]) is the better way to use this and then IF you happen to be using Command Line arguments you employ the argparser library as it handles Command Line arguments much more cleanly

Next do not pass self to QLabel that is totally unnecessary and completely unused in this case.

Here is a cleaned up and proper version of Axel_Erfurt's example hopefully he will start providing more quality code in the future
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QLabel,QPushButton
 
class MyForm(QWidget):
    def __init__(self):
      # Do Not use Super() unless you know the 3 issues you have to protect
      # against when using it and further it is only needed if you are create
      # a program that has the rare issue it was created to handle
        QWidget.__init__(self)
      # This should be set in here
        self.setWindowTitle("MyForm")
        Left = 100; Top = 100; Wdth = 200; Hght = 100
        self.setGeometry(Left, Top, Wdth, Hght)

        self.btnClick = QPushButton('Click')
        self.btnClick.clicked.connect(self.Clicked)
  
      # self. is needed whenever you may need to reference
      # definitely needed here with QLabel as you plan to 
      # reference it later on
        self.lblAOS = QLabel('AOS')

        vbox = QVBoxLayout()
        vbox.addWidget(self.lblAOS)
        vbox.addWidget(self.btnClick)

        self.setLayout(vbox)
  
    def Clicked(self):
        self.lblAOS.setText("Button Clicked")

if __name__ == '__main__':
    MainEvntHndlr = QApplication([])

    MainApp = MyForm()
    MainApp.show()

    MainEvntHndlr.exec()
P.S. @Axel_Erfurt if you need help with creating quality python-Qt5 code I am more than willing to help just shoot me a PM.


Messages In This Thread
A Simple PyQt5 Class - by zoro - Feb-21-2020, 08:03 PM
RE: A Simple PyQt5 Class - by zoro - Feb-22-2020, 07:40 AM
RE: A Simple PyQt5 Class - by Axel_Erfurt - Feb-22-2020, 03:44 PM
RE: A Simple PyQt5 Class - by zoro - Feb-23-2020, 11:13 AM
RE: A Simple PyQt5 Class - by Axel_Erfurt - Feb-23-2020, 02:53 PM
RE: A Simple PyQt5 Class - by Denni - Feb-24-2020, 03:03 PM
RE: A Simple PyQt5 Class - by snippsat - Feb-28-2020, 01:07 PM
RE: A Simple PyQt5 Class - by Denni - Feb-28-2020, 04:25 PM
RE: A Simple PyQt5 Class - by snippsat - Feb-28-2020, 05:33 PM
RE: A Simple PyQt5 Class - by Denni - Feb-28-2020, 05:43 PM
RE: A Simple PyQt5 Class - by Gribouillis - Feb-28-2020, 05:56 PM
RE: A Simple PyQt5 Class - by Denni - Feb-28-2020, 09:23 PM
RE: A Simple PyQt5 Class - by Gribouillis - Feb-28-2020, 10:13 PM
RE: A Simple PyQt5 Class - by Denni - Feb-28-2020, 10:47 PM
RE: A Simple PyQt5 Class - by snippsat - Feb-28-2020, 11:42 PM
RE: A Simple PyQt5 Class - by micseydel - Mar-02-2020, 07:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  help needed running a simple function in pyqt5 diodes 27 8,826 Jan-24-2023, 12:19 PM
Last Post: GetOnData
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,884 Apr-06-2019, 11:15 PM
Last Post: ZenWoR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020