<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Python GUIs - spyder</title><link href="https://www.pythonguis.com/" rel="alternate"/><link href="https://www.pythonguis.com/feeds/spyder.tag.atom.xml" rel="self"/><id>https://www.pythonguis.com/</id><updated>2021-01-30T09:00:00+00:00</updated><subtitle>Create GUI applications with Python and Qt</subtitle><entry><title>PyQt Windows Won't Reopen in Spyder — How to Fix It — Why your PyQt app only runs once in Spyder, and what you can do about it</title><link href="https://www.pythonguis.com/faq/problem-when-using-spyder/" rel="alternate"/><published>2021-01-30T09:00:00+00:00</published><updated>2021-01-30T09:00:00+00:00</updated><author><name>Martin Fitzpatrick</name></author><id>tag:www.pythonguis.com,2021-01-30:/faq/problem-when-using-spyder/</id><summary type="html">I'm using Spyder in Anaconda to learn PyQt5. The first time I run my code, the Qt window pops up fine. But after I close the window and try to run the code again, nothing happens &amp;mdash; no window, no error, nothing. I have to completely restart Spyder every time. Why does this happen?</summary><content type="html">
            &lt;blockquote&gt;
&lt;p&gt;I'm using Spyder in Anaconda to learn PyQt5. The first time I run my code, the Qt window pops up fine. But after I close the window and try to run the code again, nothing happens &amp;mdash; no window, no error, nothing. I have to completely restart Spyder every time. Why does this happen?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is one of the most common problems people run into when starting out with PyQt in Spyder, so if you've hit this wall, you're in good company. The good news is that it's not a problem with your code &amp;mdash; it's a known quirk of how Spyder's IPython console interacts with the Qt event loop.&lt;/p&gt;
&lt;h2 id="why-this-happens"&gt;Why This Happens&lt;/h2&gt;
&lt;p&gt;When you run a PyQt application, a key part of the process is starting the &lt;strong&gt;Qt event loop&lt;/strong&gt; by calling &lt;code&gt;app.exec()&lt;/code&gt;. This event loop is what keeps your window open, responds to clicks and keypresses, and generally makes your GUI work. If you're new to this concept, our tutorial on &lt;a href="https://www.pythonguis.com/tutorials/pyqt6-signals-slots-events/"&gt;signals, slots, and events&lt;/a&gt; covers how the event loop drives a PyQt application.&lt;/p&gt;
&lt;p&gt;In a normal Python script run from a terminal, calling &lt;code&gt;app.exec()&lt;/code&gt; starts the event loop, and when you close the window, the loop ends and the script finishes cleanly. The next time you run the script, everything starts fresh.&lt;/p&gt;
&lt;p&gt;Spyder uses an embedded &lt;strong&gt;IPython console&lt;/strong&gt;, and IPython has its own integration with Qt's event loop. When your script calls &lt;code&gt;QApplication()&lt;/code&gt;, it creates an application instance inside that console session. After you close the window and the event loop ends, things aren't fully cleaned up &amp;mdash; the old &lt;code&gt;QApplication&lt;/code&gt; instance still exists in memory. When you try to run the script again, Qt sees that an application already exists and doesn't create a new one properly, so your window never appears.&lt;/p&gt;
&lt;h2 id="solutions"&gt;Solutions&lt;/h2&gt;
&lt;p&gt;There are several ways to work around this. Pick the one that fits your workflow best.&lt;/p&gt;
&lt;h3&gt;Use &lt;code&gt;%gui qt&lt;/code&gt; magic in the IPython Console&lt;/h3&gt;
&lt;p&gt;Spyder's IPython console supports a special command that tells IPython to manage the Qt event loop for you. Before running your script, type the following into Spyder's IPython console:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;%gui qt
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This tells IPython to integrate with Qt's event loop. With this active, you may also need to adjust your script so it doesn't call &lt;code&gt;app.exec()&lt;/code&gt; itself (since IPython is already running the event loop). For example:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel

app = QApplication.instance()
if app is None:
    app = QApplication(sys.argv)

window = QMainWindow()
window.setCentralWidget(QLabel("Hello, PyQt!"))
window.show()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;QApplication.instance()&lt;/code&gt; check is the important part here &amp;mdash; it reuses the existing application object if one already exists, instead of trying to create a second one (which Qt doesn't allow).&lt;/p&gt;
&lt;h3&gt;Change the Run Configuration&lt;/h3&gt;
&lt;p&gt;You can tell Spyder to run your script in a fresh, dedicated Python process instead of the built-in IPython console. This avoids the event loop conflict entirely.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Go to &lt;strong&gt;Run &amp;rarr; Configuration per file&lt;/strong&gt; (or press &lt;code&gt;Ctrl+F6&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Under &lt;strong&gt;Console&lt;/strong&gt;, select &lt;strong&gt;Execute in an external system terminal&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;OK&lt;/strong&gt; and run your script.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now each time you run the script, it starts in a clean process &amp;mdash; just like running it from a terminal. The window will appear in its own process, and you can re-run as many times as you like.&lt;/p&gt;
&lt;h3&gt;Use Run &amp;rarr; Profile&lt;/h3&gt;
&lt;p&gt;As discovered in the original forum thread, using &lt;strong&gt;Run &amp;rarr; Profile&lt;/strong&gt; instead of the normal &lt;strong&gt;Run&lt;/strong&gt; command also works around this issue. Profiling runs the script in a separate process, which avoids the stale &lt;code&gt;QApplication&lt;/code&gt; problem. This is a quick workaround, though it's not really what profiling is designed for.&lt;/p&gt;
&lt;h3&gt;Run from the Terminal&lt;/h3&gt;
&lt;p&gt;The most reliable way to develop PyQt applications is to run them directly from a terminal or command prompt. In your terminal, navigate to the folder containing your script and run:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-bash"&gt;bash&lt;/span&gt;
&lt;pre&gt;&lt;code class="bash"&gt;python my_app.py
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Each run starts a completely fresh Python process, so you'll never hit the stale event loop issue. You can still use Spyder (or any other editor) to write and edit your code &amp;mdash; just switch to the terminal to run it.&lt;/p&gt;
&lt;h2 id="a-complete-working-example"&gt;A Complete Working Example&lt;/h2&gt;
&lt;p&gt;Here's a minimal PyQt6 application that you can use to test any of the solutions above. If you need help setting up PyQt6 first, see our guide on &lt;a href="https://www.pythonguis.com/tutorials/pyqt6-creating-your-first-window/"&gt;creating your first PyQt6 window&lt;/a&gt;.&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("PyQt in Spyder &amp;mdash; Test")

        button = QPushButton("Click me!")
        button.clicked.connect(self.on_button_clicked)

        layout = QVBoxLayout()
        layout.addWidget(button)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

    def on_button_clicked(self):
        print("Button was clicked!")


app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;If you're running this inside Spyder's IPython console and want to be able to re-run without restarting, replace the last three lines with:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;app = QApplication.instance()
if app is None:
    app = QApplication(sys.argv)

window = MainWindow()
window.show()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 id="which-approach-should-i-use-long-term"&gt;Which Approach Should I Use Long-Term?&lt;/h2&gt;
&lt;p&gt;For learning and day-to-day development, running your scripts from a terminal is the most straightforward option. It eliminates any IDE-specific quirks and ensures your application behaves exactly the way it will for your users. You can use Spyder (or &lt;a href="https://www.pythonguis.com/tutorials/getting-started-vs-code-python/"&gt;VS Code&lt;/a&gt;, &lt;a href="https://www.pythonguis.com/tutorials/getting-started-pycharm/"&gt;PyCharm&lt;/a&gt;, or any text editor you like) for writing code, and simply keep a terminal window open alongside it for running your scripts.&lt;/p&gt;
&lt;p&gt;If you prefer to stay entirely within Spyder, using the &lt;strong&gt;external terminal&lt;/strong&gt; run configuration is the cleanest fix, since it doesn't require any changes to your actual code.&lt;/p&gt;
            &lt;p&gt;For an in-depth guide to building Python GUIs with PyQt6 see my book, &lt;a href="https://www.mfitzp.com/pyqt6-book/"&gt;Create GUI Applications with Python &amp; Qt6.&lt;/a&gt;&lt;/p&gt;
            </content><category term="pyqt5"/><category term="pyqt6"/><category term="spyder"/><category term="python"/><category term="troubleshooting"/><category term="qt"/><category term="qt5"/><category term="qt6"/></entry><entry><title>PyQtGraph errors in Spyder/IPython — Fixing common PyQtGraph color, font size, and compatibility issues</title><link href="https://www.pythonguis.com/faq/pyqtgraph-errors-in-spyder-ipython/" rel="alternate"/><published>2020-05-07T09:00:00+00:00</published><updated>2020-05-07T09:00:00+00:00</updated><author><name>Martin Fitzpatrick</name></author><id>tag:www.pythonguis.com,2020-05-07:/faq/pyqtgraph-errors-in-spyder-ipython/</id><summary type="html">If you're working through PyQtGraph tutorials and running into errors when setting colors, titles, or font sizes, you're not alone. These issues come up frequently, especially when using older versions of PyQtGraph in environments like Spyder or IPython. The good news is that most of these problems have straightforward fixes.</summary><content type="html">
            &lt;p&gt;If you're working through PyQtGraph tutorials and running into errors when setting colors, titles, or font sizes, you're not alone. These issues come up frequently, especially when using older versions of PyQtGraph in environments like Spyder or IPython. The good news is that most of these problems have straightforward fixes.&lt;/p&gt;
&lt;p&gt;In this article, we'll walk through the most common PyQtGraph errors related to colors and label formatting, explain why they happen, and show you how to solve them.&lt;/p&gt;
&lt;h2 id="the-color-name-error"&gt;The color name error&lt;/h2&gt;
&lt;p&gt;You might see this error when trying to set a title color using a full color name like &lt;code&gt;'blue'&lt;/code&gt;:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;self.graphWidget.setTitle("Your Title Here", color='blue', size='30pt')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;File "...\pyqtgraph\functions.py", line 182, in mkColor
    g = int(c[1]*2, 16)
ValueError: invalid literal for int() with base 16: 'll'
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Or, when using &lt;code&gt;'black'&lt;/code&gt;:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;UnboundLocalError: local variable 'r' referenced before assignment
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These errors occur because older versions of PyQtGraph's &lt;code&gt;mkColor&lt;/code&gt; function don't understand full color names like &lt;code&gt;'blue'&lt;/code&gt; or &lt;code&gt;'black'&lt;/code&gt;. When you pass &lt;code&gt;'blue'&lt;/code&gt;, PyQtGraph tries to interpret it as a hex color string, picks out individual characters (&lt;code&gt;'l'&lt;/code&gt;, &lt;code&gt;'u'&lt;/code&gt;, etc.), and fails to parse them as hexadecimal values.&lt;/p&gt;
&lt;h3&gt;The fix: use single-letter color codes or update PyQtGraph&lt;/h3&gt;
&lt;p&gt;The simplest immediate fix is to use PyQtGraph's single-letter color codes instead of full color names:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Letter&lt;/th&gt;
&lt;th&gt;Color&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'r'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Red&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'g'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Green&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'b'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blue&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'c'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Cyan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'m'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Magenta&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'y'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Yellow&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'k'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Black&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'w'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;White&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;So instead of &lt;code&gt;color='blue'&lt;/code&gt;, use &lt;code&gt;color='b'&lt;/code&gt;:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;self.graphWidget.setTitle("Your Title Here", color="b", size="30pt")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also pass colors as hex strings or RGB tuples:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;# Hex color
self.graphWidget.setTitle("Your Title Here", color="#0000FF", size="30pt")

# RGB tuple (values 0-255)
self.graphWidget.setTitle("Your Title Here", color=(0, 0, 255), size="30pt")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The better long-term fix is to &lt;strong&gt;update PyQtGraph&lt;/strong&gt; to a newer version, where full color name support has been improved:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-bash"&gt;bash&lt;/span&gt;
&lt;pre&gt;&lt;code class="bash"&gt;pip install pyqtgraph --upgrade
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 id="the-font-size-typeerror"&gt;The font size TypeError&lt;/h2&gt;
&lt;p&gt;Another common error appears when passing the &lt;code&gt;size&lt;/code&gt; parameter as an integer:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;self.graphWidget.setTitle("Your Title Here", color="b", size=30)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;File "...\pyqtgraph\graphicsItems\LabelItem.py", line 61, in setText
    optlist.append('font-size: ' + opts['size'])
TypeError: can only concatenate str (not "int") to str
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This happens because PyQtGraph's &lt;code&gt;LabelItem.setText&lt;/code&gt; method builds a CSS style string internally. It expects the &lt;code&gt;size&lt;/code&gt; value to already be a string with a unit suffix like &lt;code&gt;'30pt'&lt;/code&gt;. When you pass the integer &lt;code&gt;30&lt;/code&gt;, Python can't concatenate a string and an integer.&lt;/p&gt;
&lt;h3&gt;The fix: pass size as a string with units&lt;/h3&gt;
&lt;p&gt;Always pass the &lt;code&gt;size&lt;/code&gt; parameter as a string that includes the CSS unit:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;self.graphWidget.setTitle("Your Title Here", color="b", size="30pt")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;pt&lt;/code&gt; suffix stands for "points," which is a standard CSS font-size unit. You could also use &lt;code&gt;px&lt;/code&gt; (pixels) if you prefer:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;self.graphWidget.setTitle("Your Title Here", color="b", size="30px")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 id="updating-pyqtgraph"&gt;Updating PyQtGraph&lt;/h2&gt;
&lt;p&gt;Many of these issues were fixed in later releases of PyQtGraph. If you're using an older version (especially anything before 0.11), upgrading will resolve most of these problems and give you access to better features and performance.&lt;/p&gt;
&lt;p&gt;Check your current version:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-bash"&gt;bash&lt;/span&gt;
&lt;pre&gt;&lt;code class="bash"&gt;pip show pyqtgraph
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Upgrade to the latest version:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-bash"&gt;bash&lt;/span&gt;
&lt;pre&gt;&lt;code class="bash"&gt;pip install pyqtgraph --upgrade
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;If you're using Anaconda (common with Spyder), you can also update through conda:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-bash"&gt;bash&lt;/span&gt;
&lt;pre&gt;&lt;code class="bash"&gt;conda install -c conda-forge pyqtgraph
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 id="the-timeclock-error"&gt;The &lt;code&gt;time.clock&lt;/code&gt; error&lt;/h2&gt;
&lt;p&gt;If you're on Python 3.8 or later with an older PyQtGraph version, you may also see an error related to &lt;code&gt;time.clock&lt;/code&gt;. This function was removed in Python 3.8. Upgrading PyQtGraph to version 0.11.0 or later fixes this as well.&lt;/p&gt;
&lt;h2 id="a-complete-working-example"&gt;A complete working example&lt;/h2&gt;
&lt;p&gt;Here's a complete example that uses the correct color and size formats, and should work across PyQtGraph versions:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import sys

from PyQt5.QtWidgets import QApplication, QMainWindow

import pyqtgraph as pg


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.graphWidget = pg.PlotWidget()
        self.setCentralWidget(self.graphWidget)

        # Set background color
        self.graphWidget.setBackground("w")

        # Set title with single-letter color code and string size
        self.graphWidget.setTitle(
            "Temperature Over Time", color="b", size="20pt"
        )

        # Set axis labels
        styles = {"color": "r", "font-size": "18px"}
        self.graphWidget.setLabel("left", "Temperature (&amp;deg;C)", **styles)
        self.graphWidget.setLabel("bottom", "Time (min)", **styles)

        # Add grid
        self.graphWidget.showGrid(x=True, y=True)

        # Sample data
        hour = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        temperature = [30, 32, 34, 32, 33, 31, 29, 32, 35, 30]

        # Plot with a blue pen
        pen = pg.mkPen(color="b", width=2)
        self.graphWidget.plot(hour, temperature, pen=pen)


app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This example avoids all the common pitfalls:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Colors use single-letter codes (&lt;code&gt;"b"&lt;/code&gt;, &lt;code&gt;"r"&lt;/code&gt;) instead of full names&lt;/li&gt;
&lt;li&gt;The title &lt;code&gt;size&lt;/code&gt; is a string with a unit (&lt;code&gt;"20pt"&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Axis label styles use string values with units (&lt;code&gt;"18px"&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you run this and see your plot with a blue title, red axis labels, and a line chart, everything is working correctly.&lt;/p&gt;
&lt;h2 id="summary-of-fixes"&gt;Summary of fixes&lt;/h2&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Error&lt;/th&gt;
&lt;th&gt;Cause&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ValueError: invalid literal for int() with base 16&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Full color name like &lt;code&gt;'blue'&lt;/code&gt; not supported&lt;/td&gt;
&lt;td&gt;Use &lt;code&gt;'b'&lt;/code&gt; or a hex/RGB value&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;UnboundLocalError: local variable 'r' referenced before assignment&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Unrecognized color string&lt;/td&gt;
&lt;td&gt;Use single-letter codes, hex, or RGB tuples&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;TypeError: can only concatenate str (not "int") to str&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Size passed as integer (&lt;code&gt;30&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Pass as string with unit (&lt;code&gt;"30pt"&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;AttributeError: module 'time' has no attribute 'clock'&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Python 3.8+ with old PyQtGraph&lt;/td&gt;
&lt;td&gt;Upgrade PyQtGraph to 0.11.0+&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;When in doubt, upgrading PyQtGraph to the latest version is the single most effective thing you can do. Most of these quirks have been addressed in recent releases, and you'll benefit from better compatibility with modern Python and Qt versions.&lt;/p&gt;
            &lt;p&gt;For an in-depth guide to building Python GUIs with PyQt5 see my book, &lt;a href="https://www.mfitzp.com/pyqt5-book/"&gt;Create GUI Applications with Python &amp; Qt5.&lt;/a&gt;&lt;/p&gt;
            </content><category term="pyqt5"/><category term="pyqt"/><category term="pyqtgraph"/><category term="spyder"/><category term="ipython"/><category term="python"/><category term="qt"/><category term="qt5"/><category term="data-science"/><category term="pyqt5-data-science"/></entry><entry><title>Spyder IDE gets stuck when running PyQt5 applications — How to fix the frozen console and unresponsive window problem in Spyder</title><link href="https://www.pythonguis.com/faq/spyder-ide-gets-stuck/" rel="alternate"/><published>2020-05-07T09:00:00+00:00</published><updated>2020-05-07T09:00:00+00:00</updated><author><name>Martin Fitzpatrick</name></author><id>tag:www.pythonguis.com,2020-05-07:/faq/spyder-ide-gets-stuck/</id><summary type="html">If you're following a PyQt5 tutorial and running your code in Spyder, you may have hit an annoying problem: the console freezes as soon as your window appears, clicking the close button ("x") doesn't work, and you're forced to restart the kernel. This is a well-known issue with how Spyder handles Qt event loops, and the fix is straightforward.</summary><content type="html">
            &lt;p&gt;If you're following a PyQt5 tutorial and running your code in Spyder, you may have hit an annoying problem: the console freezes as soon as your window appears, clicking the close button ("x") doesn't work, and you're forced to restart the kernel. This is a well-known issue with how Spyder handles Qt event loops, and the fix is straightforward.&lt;/p&gt;
&lt;h2 id="why-does-this-happen"&gt;Why does this happen?&lt;/h2&gt;
&lt;p&gt;Spyder itself is built with PyQt5. It runs its own Qt event loop behind the scenes to power its interface. When you launch a &lt;em&gt;second&lt;/em&gt; PyQt5 application from within Spyder &amp;mdash; your application &amp;mdash; the two event loops can conflict with each other, causing the console to lock up and your window to become unresponsive.&lt;/p&gt;
&lt;p&gt;The typical code that triggers this looks something like:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

app = QApplication(sys.argv)

window = QMainWindow()
window.show()

app.exec()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Running this in Spyder can cause the IDE to hang because &lt;code&gt;app.exec()&lt;/code&gt; starts a blocking event loop that clashes with Spyder's own loop. If you're just getting started with PyQt5, see our tutorial on &lt;a href="https://www.pythonguis.com/tutorials/creating-your-first-pyqt-window/"&gt;creating your first PyQt window&lt;/a&gt; for the basics of how &lt;code&gt;QApplication&lt;/code&gt; and the event loop work.&lt;/p&gt;
&lt;h2 id="the-fix-change-spyders-graphics-backend"&gt;The fix: change Spyder's graphics backend&lt;/h2&gt;
&lt;p&gt;Spyder has a built-in setting that lets it share its own Qt event loop with your application. When this is enabled, you don't need to create your own &lt;code&gt;QApplication&lt;/code&gt; or call &lt;code&gt;app.exec()&lt;/code&gt; &amp;mdash; Spyder takes care of it for you.&lt;/p&gt;
&lt;p&gt;Here's how to enable it:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;In Spyder, go to &lt;strong&gt;Tools &amp;rarr; Preferences&lt;/strong&gt; (or &lt;strong&gt;Python &amp;rarr; Preferences&lt;/strong&gt; on macOS).&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;IPython console&lt;/strong&gt; from the left-hand panel.&lt;/li&gt;
&lt;li&gt;Click on the &lt;strong&gt;Graphics&lt;/strong&gt; tab.&lt;/li&gt;
&lt;li&gt;Under &lt;strong&gt;Graphics backend&lt;/strong&gt;, change the dropdown to &lt;strong&gt;Qt5&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;OK&lt;/strong&gt; and &lt;strong&gt;restart the kernel&lt;/strong&gt; (go to &lt;strong&gt;Consoles &amp;rarr; Restart kernel&lt;/strong&gt;, or press the restart button in the console toolbar).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;After doing this, Spyder's IPython console will integrate with the Qt event loop automatically.&lt;/p&gt;
&lt;h2 id="adjusting-your-code-for-spyder"&gt;Adjusting your code for Spyder&lt;/h2&gt;
&lt;p&gt;With the Qt5 backend enabled, you'll want to modify your code slightly so it works both inside Spyder and as a standalone script. The main change is to check whether a &lt;code&gt;QApplication&lt;/code&gt; instance already exists before creating a new one:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import sys
from PyQt5.QtWidgets import QApplication, QMainWindow

app = QApplication.instance()
if app is None:
    app = QApplication(sys.argv)

window = QMainWindow()
window.setWindowTitle("My Window")
window.show()

app.exec()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;QApplication.instance()&lt;/code&gt; returns the existing application object if one is already running (which it will be inside Spyder with the Qt5 backend). If there isn't one &amp;mdash; for example, when you run the script from a terminal &amp;mdash; it creates a new one as usual.&lt;/p&gt;
&lt;p&gt;This pattern means your script will work correctly in both environments.&lt;/p&gt;
&lt;h2 id="alternative-run-your-scripts-externally"&gt;Alternative: run your scripts externally&lt;/h2&gt;
&lt;p&gt;If you'd rather not change Spyder's settings, another option is to run your PyQt5 scripts in an external terminal instead of Spyder's built-in console.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Go to &lt;strong&gt;Run &amp;rarr; Configuration per file&lt;/strong&gt; (or press &lt;strong&gt;Ctrl+F6&lt;/strong&gt;).&lt;/li&gt;
&lt;li&gt;Under &lt;strong&gt;Console&lt;/strong&gt;, select &lt;strong&gt;Execute in an external system terminal&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;OK&lt;/strong&gt; and run the script.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Your PyQt5 application will open in a separate process, completely independent of Spyder's event loop. This avoids the conflict entirely and behaves the same as running &lt;code&gt;python your_script.py&lt;/code&gt; from a terminal.&lt;/p&gt;
&lt;p&gt;You might also consider using a different IDE altogether. &lt;a href="https://www.pythonguis.com/tutorials/getting-started-vs-code-python/"&gt;VS Code&lt;/a&gt; and &lt;a href="https://www.pythonguis.com/tutorials/getting-started-pycharm/"&gt;PyCharm&lt;/a&gt; are both popular alternatives that don't share a Qt event loop with your application, so they avoid this issue entirely.&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary&lt;/h2&gt;
&lt;p&gt;The freezing issue happens because Spyder and your PyQt5 application both try to run their own Qt event loops. You can fix it by either:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Setting Spyder's graphics backend to &lt;strong&gt;Qt5&lt;/strong&gt;, which lets Spyder manage the event loop for you.&lt;/li&gt;
&lt;li&gt;Running your script in an &lt;strong&gt;external terminal&lt;/strong&gt;, which keeps the two processes separate.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both approaches work well. The graphics backend approach is convenient for quick experimentation in the console, while the external terminal approach keeps your code identical to what you'd run outside of Spyder. Pick whichever fits your workflow best.&lt;/p&gt;
&lt;p&gt;If you're considering upgrading to PyQt6 or PySide6 for your projects, take a look at our &lt;a href="https://www.pythonguis.com/faq/pyqt6-vs-pyside6/"&gt;comparison of PyQt6 vs PySide6&lt;/a&gt; to help you decide which is right for you.&lt;/p&gt;
            &lt;p&gt;For an in-depth guide to building Python GUIs with PySide6 see my book, &lt;a href="https://www.mfitzp.com/pyside6-book/"&gt;Create GUI Applications with Python &amp; Qt6.&lt;/a&gt;&lt;/p&gt;
            </content><category term="pyqt5"/><category term="pyqt"/><category term="spyder"/><category term="python"/><category term="qt"/><category term="qt5"/></entry></feed>