This is our twelfth article in Python Kivy, in this article we are going to learn Creating Kivy Popup Window.
The Popup
widget is used to create modal popups. By default, the popup will cover the whole “parent” window. When you are creating a popup, you must at least set a Popup.title
and Popup.content
.
Remember that the default size of a Widget is size_hint=(1, 1). If you don’t want your popup to be full screen, either use size hints with values less than 1 (for instance size_hint=(.8, .8)) or deactivate the size_hint and use fixed size attributes.
Also if you are interested in Python GUI Development with different libraries, you can check the below links.
1: PyQt5 GUI Development Tutorials
2: Pyside2 GUI Development Tutorials
3: wxPython GUI Development Tutorials
4: TKinter GUI Development Tutorials
Also you can watch the complete video for this article
OK so first of all create a python file , and add these codes in that file. our first class extends from Popup class and we are not going to add anything in that class, the second class extends from RelativeLayout class, because we want to use Relative Layout in this tutorial. and we simply create a method in here, because in my gui i will have a button and i want to connect this method with that button, in this method we have created our Popup class object. after that we have created our main class that extends from the App class, and we have returned our MyRelativeLayout class in our main class .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from kivy.app import App from kivy.uix.popup import Popup from kivy.uix.relativelayout import RelativeLayout #our popup class class MyPopup(Popup): pass #we are using relative layout class MyRelativeLayout(RelativeLayout): def open_popup(self): pops = MyPopup() pops.open() #our main window class class PopUpWindow(App): def build(self): return MyRelativeLayout() #in here we need to run our applicaiton if __name__ == "__main__": window = PopUpWindow() window.run() |
Now let’s create our .kv file, i have named my kv file popupwindow.kv, make sure that this name should be similar to the main class name, in my case it is PopUpWindow class. we are going to define some rules for our RelativeLayout also we are going to add a button in our Relative Layout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#:kivy 1.10.0 <MyRelativeLayout>: Button: text:"Open Popup" background_color:0,1,1,1 pos_hint: {"x":0, "y":0} size_hint:0.3,0.3 on_press:root.open_popup() <MyPopup>: id:pop size_hint:.4,.4 auto_dismiss:False title:'PopUp Window' Button: text:"Click Here To Dismiss PopUp" on_press: pop.dismiss() |
You can see that we have connected our button from MyRelative class in our kv file like this.
1 |
on_press:root.open_popup() |
So run the complete code this will be the result
Subscribe and Get Free Video Courses & Articles in your Email
Thanks for share your knowledge, this help me to understand the use of the popups
your welcome