PyQtGraph errors in Spyder/IPython

Heads up! You've already completed this tutorial.

Daan_Wielens | 2020-05-07 21:08:29 UTC | #1

I get an error when adding titles / labels with specific colors and/or font sizes. Even running the example code leaves me with problems. I use Python 3.7.3 (through Spyder, iPython 7.6.1).

This line:

python
self.graphWidget.setTitle("Your Title Here", color='blue', size=30)

leads to:

python
File "C:\ProgramData\Anaconda3\lib\site-packages\pyqtgraph\functions.py", line 182, in mkColor
g = int(c[1]*2, 16)
ValueError: invalid literal for int() with base 16: 'll'

Changing the color to 'black' strangely leads to:

python
File "C:\ProgramData\Anaconda3\lib\site-packages\pyqtgraph\functions.py", line 222, in mkColor
args = [r,g,b,a]
UnboundLocalError: local variable 'r' referenced before assignment

When removing the color argument, I get:

python
File "C:\ProgramData\Anaconda3\lib\site-packages\pyqtgraph\graphicsItems\LabelItem.py", line 61, in setText
optlist.append('font-size: ' + opts['size'])
TypeError: can only concatenate str (not "int") to str

Since obviously your code seems to work on the website and in the examples, is this Spyder / iPython related? And if so, how could I solve this?

Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PyQt5 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book


erhughes4606 | 2020-06-05 18:29:40 UTC | #2

I also could not run the code as is. I have version Python 3.8.1 and am running on Windows 10. I first loaded 0.11.0rc0 to get rid of the time.clock error mentioned earlier.

The Title error happens because of the way the color is called (color='blue'). if you look at functions.py, it is not expecting 'blue'. If you just make that 'b', it will get past that error. In my case, I altered functions.py by adding some if statements to convert any color that is completely spelled out to the appropriate single color letter, like blue = b, red = r, green=g, black = k, and so on. This solved the problem and actually permits any color full name to be used. Below is the code correction for the color blue in functions.py. Repeat this for green, yellow, red, ….

python
if args[0] == 'blue':
    argslist=list(args)
    argslist[0]='b'
    args=tuple(argslist)
    print("args set to: ", args)

Next you need to correct LabelItem.py. In the example, they call size=30. LabelItem.py expects this to be 30pt in the setText definition. So I corrected the program by adding if opts['size'].endswith("pt"):. Using a try/except around this, it will give an AttributeError if "pt" is not there. So I then added the line: opts['size']=str(opts['size'])+"pt" under the exception to set opts to the appropriate value. These two items will allow the program to run. Note that this is within the if statement if 'size' in opts (starts at line 60) of the LabelItem.py script. Here is the working code in setText of LabelItem.py.

if 'size' in opts: try: if opts['size'].endswith("pt"): print("ends with pt") except AttributeError: opts['size']=str(opts['size'])+"pt" pass optlist.append('font-size: ' + opts['size'])

I just ran into another error... adding the additional code to allow the data to scroll. This gives me the error Typeerror: 'builtin_function_or_method' object is not subscriptable. The line self.x[0]... is the culprit. If I find the cause of this error, I will post it.


hlipperjohn6287 | 2020-09-11 20:11:50 UTC | #3

[quote="Daan_Wielens, post:1, topic:101"] UnboundLocalError: local variable [/quote]

An UnboundLocalError is raised when a local variable is referenced before it has been assigned. In most cases this will occur when trying to modify a local variable before it is actually assigned within the local scope. Python doesn't have variable declarations, so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local.

Python has lexical scoping by default, which means that although an enclosed scope can access values in its enclosing scope, it cannot modify them (unless they're declared global with the global keyword). A closure binds values in the enclosing environment to names in the local environment. The local environment can then use the bound value, and even reassign that name to something else, but it can't modify the binding in the enclosing environment. UnboundLocalError happend because when python sees an assignment inside a function then it considers that variable as local variable and will not fetch its value from enclosing or global scope when we execute the function. However, to modify a global variable inside a function, you must use the global keyword.


The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

PyQtGraph errors in Spyder/IPython was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.