How to run Python script directly in Kivy file?
Last Updated :
06 Jun, 2021
Kivy is a platform-independent GUI tool in Python. It can run on Android, IOS, Linux and Windows, etc. This is the only GUI library from python which can independently run on the android device even we can use it on Raspberry pi also. It is an open-source Python library for the rapid development of applications that make use of innovative user interfaces, such as multi-touch apps. Its graphic engine is built over OpenGL ES 2, and has fast graphics pipeline. If you are new to kivy you can learn from this link.
In this article, we will develop a GUI window using kivy framework of python and we will add a button on this window. Usually what happens is we attach a method to a button and the whole method is defined in another python file but this time we will add button code to same .kv file
The IDE we are going to use is pycharm and the version of python we are going to use is python 3.6.
Approach
- Create new project in pycharm
- Installing required packages
- Add new python file in venv directory of your project. To add file video has been attaches.
- Add new .kv file in project. Implementation us depicted here:
- Adding code to both files
main.py
Python3
# importing image widget of kivy framework
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.app import App
# importing boxlayout for our application
from kivy.uix.boxlayout import BoxLayout
# this will connect MainWindow which we have created in ui.kv with main.py file
class MainWindow(BoxLayout):
pass
"""
Note:- keep in mind that our .kv file name was ui.kv so our rendering class(class which will render our application) name
should be like uiApp otherwise we will not get the desired output!!
"""
# this is the main class which will render the whole application
class uiApp(App):
# method which will render our application
def build(self):
return MainWindow()
# running the application
uiApp().run()
window which contains our button:
<MainWindow>:
BoxLayout:
# adding a button
Button:
# text which will appear on button
text:"click here to open google search"
on_release:
#importing webbrowser module
import webbrowser
# it will open google window in your browser
webbrowser.open('http://www.google.com')
print("see these scripts are now running using kivy file")
Output:
when you click on button then it will open the google page and print the content of print statement defined in ui.kv file on terminal, you can see in the given video.
Similar Reads
How to run bash script in Python? If you are using any major operating system, you are indirectly interacting with bash. If you are running Ubuntu, Linux Mint, or any other Linux distribution, you are interacting with bash every time you use the terminal. Suppose you have written your bash script that needs to be invoked from python
2 min read
How to Run a Python Script Python scripts are Python code files saved with a .py extension. You can run these files on any device if it has Python installed on it. They are very versatile programs and can perform a variety of tasks like data analysis, web development, etc. You might get these Python scripts if you are a begin
6 min read
How to Run a Python Script using Docker? Docker helps you to run your Python application very smoothly in different environments without worrying about underlying platforms. Once you build an image using dockerfile you can run that image wherever you want to run. Docker image will help you to package all the dependencies required for the a
8 min read
Run python script from anywhere in linux In Linux, there is a way to execute python files from anywhere. This can be done by typing several commands in the terminal. Prerequisite: Basic Shell Commands in Linux Basics of python Steps: At first, open the terminal and go to the home directory. To go the home directory type the following comma
2 min read
How To Embed Python Code In Batch Script Embedding Python code in a batch script can be very helpful for automating tasks that require both shell commands and Python scripting. In this article, we will discuss how to embed Python code within a batch script. What are Batch Scripts and Python Code?Batch scripts are text files containing a se
4 min read
Python | Carousel Widget In Kivy using .kv file Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. ???????? Kivy Tutorial - Learn Kivy with Examples. Carousel wid
3 min read