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.
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.
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.
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.
fromkivy.appimportrunTouchAppfromkivy.langimportBuilderfromkivy.core.windowimportWindow#this is to make the Kivy window always on topWindow.always_on_top=True#set the window titleWindow.set_title('Welcome to Kivy School!')#kv language setting the main widget to be a buttonkvString='''Button: text: "Hello world!"'''#run Kivy apprunTouchApp(Builder.load_string(kvString))
Type: poetry update in the kivyhw 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 using pip. 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 of certifi 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 the poetry shell command).
In the kivyhw folder (NOT kivyhw/kivyhw folder!) type: pip install -r requirements.txt
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 910111213141516171819
importtkinterastk# Create the main windowroot=tk.Tk()# Set window titleroot.title("Welcome to Kivy School!")# Set min window sizeroot.minsize(450,100)# Set window to always be on top (remove this line for a regular GUI)root.attributes('-topmost',True)# Create labellabel=tk.Label(root,text="Hello, World!")# Lay out labellabel.pack()# Run forever!root.mainloop()
Type: poetry update in the tkinterhw 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 using pip. 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 of certifi 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 the poetry shell command).
In the tkinterhw folder (NOT tkinterhw/tkinterhw folder!) type: pip install -r requirements.txt
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:
fromkivy.appimportrunTouchAppfromkivy.langimportBuilderfromkivy.core.windowimportWindow#this is to make the Kivy window always on topWindow.always_on_top=True#set the window titleWindow.set_title('Welcome to Kivy School!')#kv language setting the main widget to be a buttonkvString='''Button: text: "Hello world!"'''#run Kivy apprunTouchApp(Builder.load_string(kvString))
Type: poetry update in the kivyhw 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 using pip. 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 of certifi 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 the poetry shell command).
In the kivyhw folder (NOT kivyhw/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 + .
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 910111213141516171819
importtkinterastk# Create the main windowroot=tk.Tk()# Set window titleroot.title("Welcome to Kivy School!")# Set min window sizeroot.minsize(450,100)# Set window to always be on top (remove this line for a regular GUI)root.attributes('-topmost',True)# Create labellabel=tk.Label(root,text="Hello, World!")# Lay out labellabel.pack()# Run forever!root.mainloop()
Type: poetry update in the tkinterhw 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 using pip. 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 of certifi 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 the poetry shell command).
In the tkinterhw folder (NOT tkinterhw/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 + .
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:
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: