Skip to content

Instantly share code, notes, and snippets.

@shaperilio
Created December 9, 2022 17:39
Show Gist options
  • Save shaperilio/f10272fc2c205c6b25a0611baeeafedb to your computer and use it in GitHub Desktop.
Save shaperilio/f10272fc2c205c6b25a0611baeeafedb to your computer and use it in GitHub Desktop.
Notebook to try out the functionality of `%gui qt`
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "8d0275a6",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "781e9597",
"metadata": {},
"source": [
"Case 1:\n",
"1. Choose a Qt version that isn't installed. \n",
"2. Choose one that shows as installed"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cb56d71b",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:root:\n",
" Could not load requested Qt binding. Please ensure that\n",
" PyQt4 >= 4.7, PyQt5, PySide >= 1.0.3 or PySide2 is available,\n",
" and only one is imported per session.\n",
"\n",
" Currently-imported Qt library: None\n",
" PyQt4 available (requires QtCore, QtGui, QtSvg): False\n",
" PyQt5 available (requires QtCore, QtGui, QtSvg, QtWidgets): True\n",
" PySide >= 1.0.3 installed: False\n",
" PySide2 installed: False\n",
" Tried to load: ['pyqt']\n",
" \n"
]
}
],
"source": [
"%gui qt4\n",
"%gui qt5"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0b46a30e",
"metadata": {},
"outputs": [],
"source": [
"# Evaluate here and verify loop is running\n",
"x = 5"
]
},
{
"cell_type": "markdown",
"id": "17a06336",
"metadata": {},
"source": [
"Case 1b:\n",
"1. Explicitly set `QT_API` environment variable\n",
"2. Request a different version"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cc52542e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Request for \"qt5\" will be ignored because `QT_API` environment variable is set to \"pyside6\"\n"
]
}
],
"source": [
"import os\n",
"os.environ['QT_API'] = 'pyside6'\n",
"%gui qt5"
]
},
{
"cell_type": "markdown",
"id": "d91ad56c",
"metadata": {},
"source": [
"Case 2:\n",
"1. Start a Qt loop.\n",
"2. Stop it.\n",
"3. Restart it."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "698b28d2",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%gui qt6"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ad130526",
"metadata": {},
"outputs": [],
"source": [
"%gui"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "97d6e16a",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%gui qt6"
]
},
{
"cell_type": "markdown",
"id": "f0424ce3",
"metadata": {},
"source": [
"Case 3:\n",
"1. Start a Qt loop.\n",
"2. Stop it.\n",
"3. Try to start a different version (won't work due to import mechanics, so we raise an exception)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "4b354e3d",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%gui qt6"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f8e91f9c",
"metadata": {},
"outputs": [],
"source": [
"%gui"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f593894f",
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:root:Cannot switch Qt versions for this session; must use qt6.\n"
]
}
],
"source": [
"%gui qt5"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "85885cd3",
"metadata": {},
"outputs": [],
"source": [
"# OK, request the correct one.\n",
"%gui qt6"
]
},
{
"cell_type": "markdown",
"id": "057eba0e",
"metadata": {},
"source": [
"Case 3b:\n",
"1. Start a Qt loop.\n",
"2. Stop it. Explicitly delete `QT_API` environment variable.\n",
"3. Try to start a different version (won't work due to import mechanics)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8dbb05af",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%gui qt6"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b5ef0512",
"metadata": {},
"outputs": [],
"source": [
"%gui\n",
"import os\n",
"del os.environ['QT_API']"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "491829de",
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ERROR:root:Cannot switch Qt versions for this session; must use qt6.\n"
]
}
],
"source": [
"%gui qt5"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a51515f9",
"metadata": {},
"outputs": [],
"source": [
"%gui qt6"
]
},
{
"cell_type": "markdown",
"id": "c8d9d803",
"metadata": {},
"source": [
"Case 4:\n",
"1. Request specific version\n",
"2. Stop it\n",
"3. Request generic (should proceed just fine)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f046b851",
"metadata": {},
"outputs": [],
"source": [
"%gui qt6"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ca31927a",
"metadata": {},
"outputs": [],
"source": [
"%gui"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d336b548",
"metadata": {},
"outputs": [],
"source": [
"%gui qt"
]
},
{
"cell_type": "markdown",
"id": "24ac69e3",
"metadata": {},
"source": [
"Final test, with actual window."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "de953bd0",
"metadata": {},
"outputs": [],
"source": [
"%gui qt6"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2372b14c",
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"from PySide6.QtWidgets import QApplication, QLabel, QWidget\n",
"\n",
"def qt_app() -> QApplication:\n",
" app = QApplication.instance()\n",
" if app is None:\n",
" app = QApplication()\n",
" return app\n",
"\n",
"\n",
"no_garbage_collector: List[QWidget] = []\n",
"\"\"\"\n",
"Keep all instances of `QWidget` from being garbage-collected by putting them\n",
"here. If a `QWidget` is not used, they will not be rendered by the event loop.\n",
"\"\"\"\n",
"\n",
"\n",
"def show_label():\n",
" label = QLabel(\"<font color=red size=40>Hello World!</font>\")\n",
" label.show()\n",
" no_garbage_collector.append(label)\n",
" # app.exec()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1d3649c4",
"metadata": {},
"outputs": [],
"source": [
"show_label()\n",
"x = 5"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a409d3b3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e79df8ac",
"metadata": {},
"outputs": [],
"source": [
"# leaves windows hanging\n",
"%gui"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "5784598a",
"metadata": {},
"outputs": [],
"source": [
"# Windows \"wake up\"\n",
"%gui qt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ce9de66",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment