2024¶
Matplotlib in Kivy, Display 3D graphs, Interactive graphs, Zoom and Pan
Article Error Reporting
Message @BadMetrics on the Kivy Discord.
ImagePainter App, Save and Draw on an Image!
Article Error Reporting
Message @BadMetrics on the Kivy Discord.
Why use the screen module?
Kivy's screen module is handy for emulating different screen sizes and dpis. This means that by using your dev machine, you can check out what your Kivy application would look like on other devices without having them. This is preferable to just guessing what your app would look like, but is in no way a replacement for actually having a test device.
Using Kivy's screen module to emulate devices
In this blog post, I will assume that you already followed the Tkinter VS Kivy Tutorial Quickstart and the How to install Kivy installation guide for your OS to install Kivy and an example project.
There is no special install or setup to use the screen module. It is built-in when you have installed Kivy.
To check it out, change directory with cd
to the correct folder.
Make sure your virtual environment is setup with poetry shell
!
Run this command:
$ python main.py -m screen:s3
How to the module command works:
First you call python
and the script name: main.py
The -m
means that you are running a Python module.
screen:s3
means that you are running the screen
module with argument s3
.
Examples on Windows, try them out!
Emulating an s3 screen.
Command:
$ python main.py -m screen:s3
Emulating an iphone6 screen.
Command:
$ python main.py -m screen:phone_iphone_6
How to add a custom device
To add your own device, go to the screen.py
module in Kivy. If you have followed our install tutorial, you can simply to go your .venv
folder, find kivy
, and then go here:
.venv/lib/site-packages/kivy/modules/screen.py
I did not follow any tutorial! How do I find my environment?
If you did not follow any tutorial, you can find your environment with Python only.
Reference: Finding your Python env
Steps:
Type python
in your terminal.
Type import sys
. Press Enter
Type print(sys.prefix)
. Press Enter.
That folder will be where your Kivy install is, and subsequently your screen module as well.
Next, to add a custom device, follow the format of the devices dictionary, and add your device as follows:
$ 'fake_device_name': ('my_device_name', width, height, dpi, density),
Next, call your custom device!
Command:
$ python main.py -m screen:fake_device
Examples on MacOS, try them out!
Emulating an s3 screen.
Command:
$ python main.py -m screen:s3
Emulating an iphone6 screen.
Command:
$ python main.py -m screen:phone_iphone_6
How to add a custom device
To add your own device, go to the screen.py
module in Kivy. If you have followed our install tutorial, you can simply to go your .venv
folder, find kivy
, and then go here:
.venv/lib/site-packages/kivy/modules/screen.py
I did not follow any tutorial! How do I find my environment?
If you did not follow any tutorial, you can find your environment with Python only.
Reference: Finding your Python env
Steps:
Type python
in your terminal.
Type import sys
. Press Enter
Type print(sys.prefix)
. Press Enter.
That folder will be where your Kivy install is, and subsequently your screen module as well.
Next, to add a custom device, follow the format of the devices dictionary, and add your device as follows:
$ 'fake_device_name': ('my_device_name', width, height, dpi, density),
Next, call your custom device!
Command:
$ python main.py -m screen:fake_device
On the Mac M1, you may need to use the scale command to get the screen to be the correct size:
Other relevant commands: portrait and scale
Other commands to try out:
Portrait mode:
$ python main.py -m screen:phone_samsung_galaxy_s5,portrait
Portrait and scale:
$ python main.py -m screen:tablet_nexus_7_13,portrait,scale=.75
Where to find screen data?
https://www.sven.de/dpi/
- has a list of common dpis and phone resolutions
Article Error Reporting
Message @BadMetrics on the Kivy Discord.
Tkinter VS Kivy Tutorial Quickstart: Which one is right for you?
Why Use Tkinter or Kivy In the First Place?
Tkinter and Kivy are both cross platform GUIs that are commonly used by Python users. There are other frameworks out there but they are either: in another programming language, not available on all platforms, or a combination of both.
In this blog post, you will go through the hello world
steps of both GUI interfaces on as much platforms as possible to get a feel for which one is right for you.
The only real test is to try them out!
Before following this guide, you need these tools and libraries:
- Python
These are helpful, but not required for this tutorial:
- Git
- Pyenv
- Poetry
- VSCode or editor of your choice
If you do not see any of the below sections, please click to show.
main.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Kivy Win Step 1: Get the repo from github
You can do this in two ways: git clone https://github.com/kivy-school/kivyhw
or download the zip file and unzip it somewhere.
How do I git clone?
- Press the ⊞ Windows key and type
cmd
to open command prompt.
- If git is installed, then change directory with the
cd
command to the correct folder you want to work in. dir
in Windows shows you the contents of the current directory.cd
changes to the directory you want.cd ..
goes up 1 directory level.
Then type git clone https://github.com/kivy-school/kivyhw
Then cd kivyhw
to enter the project.
Kivy Win Step 2: Install libraries
If poetry is installed:
- Type:
poetry update
in thekivyhw
directory. Poetry will install everything for you and AUTOSOLVE VERSION CONFLICTS, plus provide you with a virtual environment so you don't break your Windows system Python. - Type
poetry shell
to enter the virtual environment and proceed to the next step.
If poetry is not installed:
-
If
poetry
is not installed, you can still install requirements usingpip
. Be warned! If you make any mistakes with any sort of pip install, you might end up breaking your Windows Python installation and have to reinstall it over again to fix your environment. Because this is just a Hello World tutorial, you do not need to worry about it now. If you have also followed other tutorials, you might get version conflicts for libraries. This is because pip normally installs every library to your system Python unless specified. That means that conflicting versions ofcertifi
will appear and you will have to manually resolve conflicts. If so, please try installing python-poetry and use virtual environments for each project (like the one Kivy School is providing in this tutorial with thepoetry shell
command). -
In the
kivyhw
folder (NOTkivyhw/kivyhw
folder!) type:pip install -r requirements.txt
- Proceed to the next step.
Kivy Win Step 3: Run hello world!
There are two options to run the kivy app:
From the top level kivy hw
folder, run this command: python kivyhw/main.py
It is also possible to cd kivyhw
and run python main.py
If poetry update
was used in installation, you also have access to: task run
in the top level kivyhw
folder courtesy of these lines in the pyproject.toml
and the taskipy
library:
[tool.taskipy.tasks]
run = 'python kivyhw/main.py'
main.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Tkinter Win Step 1: Get the repo from github
You can do this in two ways: git clone https://github.com/kivy-school/tkinterhw
or download the zip file and unzip it somewhere.
How do I git clone?
- Press the ⊞ Windows key and type
cmd
to open command prompt.
- If git is installed, then change directory with the
cd
command to the correct folder you want to work in. dir
in Windows shows you the contents of the current directory.cd
changes to the directory you want.cd ..
goes up 1 directory level.
Then type git clone https://github.com/kivy-school/tkinterhw
Then cd tkinterhw
to enter the project.
Tkinter Win Step 2: Install libraries
If poetry is installed:
- Type:
poetry update
in thetkinterhw
directory. Poetry will install everything for you and AUTOSOLVE VERSION CONFLICTS, plus provide you with a virtual environment so you don't break your Windows system Python. - Type
poetry shell
to enter the virtual environment and proceed to the next step.
If poetry is not installed:
-
If
poetry
is not installed, you can still install requirements usingpip
. Be warned! If you make any mistakes with any sort of pip install, you might end up breaking your Windows Python installation and have to reinstall it over again to fix your environment. Because this is just a Hello World tutorial, you do not need to worry about it now. If you have also followed other tutorials, you might get version conflicts for libraries. This is because pip normally installs every library to your system Python unless specified. That means that conflicting versions ofcertifi
will appear and you will have to manually resolve conflicts. If so, please try installing python-poetry and use virtual environments for each project (like the one Kivy School is providing in this tutorial with thepoetry shell
command). -
In the
tkinterhw
folder (NOTtkinterhw/tkinterhw
folder!) type:pip install -r requirements.txt
- Proceed to the next step.
Tkinter Win Step 3: Run hello world!
There are two options to run the kivy app:
From the top level tkinterhw
folder, run this command: python tkinterhw/main.py
It is also possible to cd tkinterhw
and run python main.py
If poetry update
was used in installation, you also have access to: task run
in the top level tkinterhw
folder courtesy of these lines in the pyproject.toml
and the taskipy
library:
[tool.taskipy.tasks]
run = 'python tkinterhw/main.py'
main.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Kivy Mac Step 1: Get the repo from github
You can do this in two ways: git clone https://github.com/kivy-school/kivyhw
or download the zip file and unzip it somewhere.
How do I git clone?
- Press
Command ⌘
+Spacebar
and typeterminal
to openterminal.app
- If git is installed, then change directory with the
cd
command to the correct folder you want to work in. ls -l
in Mac shows you the contents of the current directory.cd
changes to the directory you want.cd ..
goes up 1 directory level.
Then type git clone https://github.com/kivy-school/kivyhw
Then cd kivyhw
to enter the project.
Kivy Mac Step 2: Install libraries
If poetry is installed:
- Type:
poetry update
in thekivyhw
directory. Poetry will install everything for you and AUTOSOLVE VERSION CONFLICTS, plus provide you with a virtual environment so you don't break your Mac system Python. - Type
poetry shell
to enter the virtual environment and proceed to the next step.
If poetry is not installed:
-
If
poetry
is not installed, you can still install requirements usingpip
. Be warned! If you make any mistakes with any sort of pip install, you might end up breaking your Mac Python installation and have to reinstall it over again to fix your environment. Because this is just a Hello World tutorial, you do not need to worry about it now. If you have also followed other tutorials, you might get version conflicts for libraries. This is because pip normally installs every library to your system Python unless specified. That means that conflicting versions ofcertifi
will appear and you will have to manually resolve conflicts. If so, please try installing python-poetry and use virtual environments for each project (like the one Kivy School is providing in this tutorial with thepoetry shell
command). -
In the
kivyhw
folder (NOTkivyhw/kivyhw
folder!) type:pip install -r requirements.txt
- Proceed to the next step.
Warning
If you cannot see the .venv
folder in the kivyhw
, that is because your Mac Finder settings are set to hide hidden folders.
-
To toggle seeing hidden folders like
.git
and.venv
, use the command:Command ⌘
+Shift
+.
-
Without hidden folders:
- With hidden folders:
Kivy Mac Step 3: Run hello world!
There are two options to run the kivy app:
From the top level kivy hw
folder, run this command: python kivyhw/main.py
It is also possible to cd kivyhw
and run python main.py
If poetry update
was used in installation, you also have access to: task run
in the top level kivyhw
folder courtesy of these lines in the pyproject.toml
and the taskipy
library:
[tool.taskipy.tasks]
run = 'python kivyhw/main.py'
main.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
TKinter Mac Step 1: Get the repo from github
You can do this in two ways: git clone https://github.com/kivy-school/tkinterhw
or download the zip file and unzip it somewhere.
How do I git clone?
- Press
Command ⌘
+Spacebar
and typeterminal
to openterminal.app
- If git is installed, then change directory with the
cd
command to the correct folder you want to work in. dir
in Windows shows you the contents of the current directory.cd
changes to the directory you want.cd ..
goes up 1 directory level.
Then type git clone https://github.com/kivy-school/tkinterhw
Then cd tkinterhw
to enter the project.
TKinter Mac Step 2: Install libraries
If poetry is installed:
- Type:
poetry update
in thetkinterhw
directory. Poetry will install everything for you and AUTOSOLVE VERSION CONFLICTS, plus provide you with a virtual environment so you don't break your Mac system Python. - Type
poetry shell
to enter the virtual environment and proceed to the next step.
If poetry is not installed:
-
If
poetry
is not installed, you can still install requirements usingpip
. Be warned! If you make any mistakes with any sort of pip install, you might end up breaking your Mac Python installation and have to reinstall it over again to fix your environment. Because this is just a Hello World tutorial, you do not need to worry about it now. If you have also followed other tutorials, you might get version conflicts for libraries. This is because pip normally installs every library to your system Python unless specified. That means that conflicting versions ofcertifi
will appear and you will have to manually resolve conflicts. If so, please try installing python-poetry and use virtual environments for each project (like the one Kivy School is providing in this tutorial with thepoetry shell
command). -
In the
tkinterhw
folder (NOTtkinterhw/tkinterhw
folder!) type:pip install -r requirements.txt
- Proceed to the next step.
Warning
If you cannot see the .venv
folder in the kivyhw
, that is because your Mac Finder settings are set to hide hidden folders.
-
To toggle seeing hidden folders like
.git
and.venv
, use the command:Command ⌘
+Shift
+.
-
Without hidden folders:
- With hidden folders:
TKinter Mac Step 3: Run hello world!
There are two options to run the kivy app:
From the top level tkinterhw
folder, run this command: python tkinterhw/main.py
It is also possible to cd tkinterhw
and run python main.py
If poetry update
was used in installation, you also have access to: task run
in the top level tkinterhw
folder courtesy of these lines in the pyproject.toml
and the taskipy
library:
[tool.taskipy.tasks]
run = 'python tkinterhw/main.py'
Why this project folder setup?
Most tutorials just give you the Python code in 1 folder.
In a real project, there are a lot of project management files that are simply not part of the main Python code and should NEVER be shipped, like hidden secrets. In this example, there are many things that should not be given to an end user, like the .git
folder, .venv
virtual environment provided by Poetry, .gitignore
used by git, poetry.lock and pyproject.toml
used by poetry, a github README.md
file and requirements.txt
used by pip
.
Those files are for development only! That is why the management files are in the top level, and the actual Python project is inside an inner folder.
You can see for yourself by inspecting these popular Python libraries:
Kivy
numpy
fastapi
Article Error Reporting
Message @BadMetrics on the Kivy Discord.