Python Forum
A Simple PyQt5 Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A Simple PyQt5 Class
#1
Hello,

I have simple class using PyQt5 library, it just a very beginner signal/slot example, where when a button is clicked there is label that should display "clicked"

class MyForm(QWidget):



    def __init__(self):


        super(MyForm, self).__init__()

        vbox = QVBoxLayout()
        label = QLabel("aos", self)
        button = QPushButton("Click")
        
        button.clicked.connect(self.clickeda)

        vbox.addWidget(label)
        vbox.addWidget(button)
        self.setLayout(vbox)
        self.show()

    def clickeda(self):
        self.label.setText("button clicked")
and here is the error:

<class 'AttributeError'> 'MyForm' object has no attribute 'label' <traceback object at 0x03E3FC68>
Can any one help
#2
Any one ?
#3
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QLabel,QPushButton

class MyForm(QWidget):
 
    def __init__(self):
 
 
        super(MyForm, self).__init__()
 
        vbox = QVBoxLayout()
        self.label = QLabel("aos", self)
        button = QPushButton("Click")
         
        button.clicked.connect(self.clickeda)
 
        vbox.addWidget(self.label)
        vbox.addWidget(button)
        self.setLayout(vbox)
        self.setGeometry(100, 100, 200, 100)
 
    def clickeda(self):
        self.label.setText("button clicked")
        
if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    win = MyForm()
    win.setWindowTitle("MyForm")
    win.show()
    sys.exit(app.exec_())
#4
@Axel_Erfurt
Thank you very much for your reply !
However, care to explain why my code wasn't working, as I don't see much of a difference between yours and mine (other than the self prefix)
#5
read your Error Message and you see it,

You are using label and self.label
#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.
#7
Dennie Wrote: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
Here is a cleaned up and proper version of Axel_Erfurt's example hopefully he will start providing more quality code in the future
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.
Just to make me option crystal clear @Dennie,so is not @Axel_Erfurt propagate bad code bye using super().
I am okay with someone not is liking super(),then they could use an other tone like i preferred it this way without super() then post code for that,
and not heavily criticize a other users for using super().

In PyQt5 official doc A simple-example there is super() used,and super() are used in almost all code in documentation for both PyQt5 and Qt for Python.

I do not want long discussion with you abut this topic,where you probably will drag up from 2006 Python's Super Considered Harmful.
This is later in 2007 rebuttal bye Ned Batchelder and 2011 Raymond Hettinger Python’s super() considered super!.
Today there is little discussion about super() as it adopted and used bye the whole Python community.

We have discussed this topic in Admin/Mod part of forum and we do agree about this topic.
So just to make it clear @Dennie if up want to keep up the same same nauseous tone against other users(which is the biggest problem here and not super()),
feel free to leave the forum.
#8
Okay @snippsat as stated and I know this because I have already informed @Axel_Erfurt about the issues with using super() so I know he knows.

Second not using super() is not a preference it is called quality coding something everyone ought to be promoting. Further your logic would have you jumping off a cliff just because everyone else was doing it (aka the Lemming effect). The really sad thing is that you state the your entire Admin/Mod group has accepted this very bad coding practice as acceptable. Probably because you did not bother to fully research it nor fully understand why it was created to begin with. The whole concept of super() as it is being abused is horrible coding. It was created for a rather rare occurring situation where introducing three new problematic issues is the trade off for dealing with that rare occurrence. Then for some hair-brained reason others chose to adopt this and use it every where. So when you use super() you are adding 3 additional issues that you have to make sure you properly code for even though you most likely do not even need to use super() because you are not dealing with that rather rare issue it was designed for.

I will save you the long discussion about the facts but you as and Admin ought to be all over this and have thoroughly investigated it before you apply your stamp of approval to highly bad coding practices. By the way I am not some programmer-wanna-be I am a software engineer with over 30+ years of experience in diverse programming environments. Note only do I know what I am talking about but I am also a tutor and have helped numerous folks understand how to code and how to determine the fact from fiction when it comes to coding practices. This is to say I do not take stuff at the surface just because a bunch of potential Lemmings are saying it is so. I dig down deep into the facts of the matter to find out the actual truth. Which is to say I have read those links you provided and I can counter each and everyone of them with why they are wrong because I actually dug a lot deeper than apparently you did. Also in PyQt5 documentation examples they show sub-classing QThreads which is NOT they way it works actually in PyQt5 and it is a known issue with their documentation -- it is sometimes just wrong as it has not been updated. So becareful what you use for a source to back your incorrect stance.

So @snippsat I am simply going to ask you 2 questions if you can answer these then you will have the knowledge you need.

1) What was the true reason that super() was created (aka what is that rare coding issue)

2) What are the 3 coding issues that you need to guard against that super() creates when you add it to a program

If you cannot answer these 2 questions then you are blowing smoke and perpetuating a problem rather than solving one

Lastly if you and your entire Admin/mod group want me to stop talking out against this very bad coding practice. Prove that your position is correct, and not by quoting some Lemming who has propagated a lie. Learn the facts and speak to them.

Note I am perfectly fine with adding you to my list of very bad forums that promote bad coding practices and adding you to my blog so that others can find out about it before wasting their time coming here. Again I teach this stuff, I conduct a free online classrooms as such I touch a lot of folks because of that and information can spread very fast due to that Lemming affect you seem to adhere to as you are not the only fish in the sea.

If you are truly interested in helping others with coding then come join one of the classrooms it is not much different than a forum except that you can give/receive more extensive help.
#9
(Feb-28-2020, 04:25 PM)Denni Wrote: Note I am perfectly fine with adding you to my list of very bad forums that promote bad coding practices and adding you to my blog so that others can find out about it before wasting their time coming here.
No problem do what you want.

Good luck with your teaching,this forum is clearly not for you as it's a "bad forum".
So i see no reason why you should stay here at all,take that as hint goodbye.
#10
(Feb-28-2020, 05:33 PM)snippsat Wrote: No problem do what you want.

Good luck with your teaching,this forum is clearly not for you as it's a "bad forum".
So i see no reason why you should stay here at all,take that as hint goodbye.

Okay so let us be completely truthful here -- you are not really about helping others as you are perfectly fine with promoting bad coding practices because you are not interested in the facts.

So yep sounds like a very very bad forum and that goes for everyone seeking quality answers not just me and thanks for the quote ;) Oh and btw no skin off my teeth I was doing far more for you than you were doing for me -- aka you lose


Possibly Related Threads…
Thread Author Replies Views Last Post
  help needed running a simple function in pyqt5 diodes 27 8,374 Jan-24-2023, 12:19 PM
Last Post: GetOnData
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,821 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