-2

Hi can anyone help me for this problem AttributeError: 'NoneType' object has no attribute 'clicked'

my code is :

import sys
from PyQt5.QtWidgets import *

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(500,250,300,400)
        self.setWindowTitle("SMURF")
        self.UI()
        self.show()
    def UI(self):
        self.text=QLabel("hi bitxg",self).move(150,100)
        b1=QPushButton("B1",self).move(130,120)
        b2=QPushButton("B2",self).move(170,120)
        b1.clicked.connect(self.b1f)
        b2.clicked.connect(self.b2f)    
    def b1f(self):
        self.text.setText("FIRST")
    def b2f(self):
        self.text.setText("SECOND")            

App=QApplication(sys.argv)
window=Window()
sys.exit(App.exec_())       
It_is_Chris
  • 13,504
  • 2
  • 23
  • 41
johnson
  • 1
  • 1
  • 2
    You shouldn't be calling `.move()` on the same line as initializing the widgets. [This question](https://stackoverflow.com/q/1101750/16775594) deals with a very similar problem in `tkinter`. Apparently, it's a common GUI-programming problem. – Sylvester Kruin Feb 07 '22 at 19:36
  • The real issue here is that you're not using layouts. There's very rarely any genuine need to use absoule positioning with child widgets. I suggest you read this [pyqt5 layout management tutorial](https://zetcode.com/gui/pyqt5/layout/). – ekhumoro Feb 07 '22 at 19:43
  • @SylvesterKruin I don't think that can be considered a "common GUI-programming problem". The function chaining pattern is to be expected only for very specific types of objects, and for which it makes sense to *always* return the (eventually) modified object. This is a rare requirement not only for GUI programming. – musicamante Feb 07 '22 at 19:51
  • @musicamante If you kept an eye on all the [tag:tkinter] questions like I do, you would see how common it is (hence my use of "apparently"). For instance, there are over 300 linked questions to the question I linked to in my above comment, and many of them are duplicates. I haven't seen very many similar questions that _didn't_ have to do with GUI programming, but I won't deny that they exist. GUI programming is just where I see this problem the most. – Sylvester Kruin Feb 07 '22 at 20:01

1 Answers1

2

You'll want to split this up:

b1=QPushButton("B1",self).move(130,120)
b2=QPushButton("B2",self).move(170,120)

Becomes:

b1=QPushButton("B1",self)
b1.move(130,120)

b2=QPushButton("B2",self)
b2.move(170,120)

Currently, you are instantiating an anonymous QPushButton object, and then binding the object returned by calling that anonymous object's move method to b1 and b2, which will be None.

Paul M.
  • 10,481
  • 2
  • 9
  • 15