widget.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Kivy example for the Popup widget
  2. # Program to Show how to create a switch
  3. # import kivy module
  4. import kivy
  5. # base Class of your App inherits from the App class.
  6. # app:always refers to the instance of your application
  7. from kivy.app import App
  8. # this restrict the kivy version i.e
  9. # below this kivy version you cannot
  10. # use the app or software
  11. kivy.require("1.9.0")
  12. # The Button is a Label with associated actions
  13. # that is triggered when the button
  14. # is pressed (or released after a click/touch).
  15. from kivy.uix.button import Button
  16. # The GridLayout arranges children in a matrix.
  17. # It takes the available space and
  18. # divides it into columns and rows,
  19. # then adds widgets to the resulting “cells”.
  20. from kivy.uix.gridlayout import GridLayout
  21. # Popup widget is used to create popups.
  22. # By default, the popup will cover
  23. # the whole “parent” window.
  24. # When you are creating a popup,
  25. # you must at least set a Popup.title and Popup.content.
  26. from kivy.uix.popup import Popup
  27. # The Label widget is for rendering text.
  28. from kivy.uix.label import Label
  29. # to change the kivy default settings we use this module config
  30. from kivy.config import Config
  31. # 0 being off 1 being on as in true / false
  32. # you can use 0 or 1 && True or False
  33. Config.set("graphics", "resizable", True)
  34. # Make an app by deriving from the kivy provided app class
  35. class PopupExample(App):
  36. # override the build method and return the root widget of this App
  37. def build(self):
  38. # Define a grid layout for this App
  39. self.layout = GridLayout(cols=1, padding=10)
  40. # Add a button
  41. self.button = Button(text="Click for pop-up")
  42. self.layout.add_widget(self.button)
  43. # Attach a callback for the button press event
  44. self.button.bind(on_press=self.onButtonPress)
  45. return self.layout
  46. # On button press - Create a popup dialog with a label and a close button
  47. def onButtonPress(self, button):
  48. layout = GridLayout(cols=1, padding=10)
  49. popupLabel = Label(text="Click for pop-up")
  50. closeButton = Button(text="Close the pop-up")
  51. layout.add_widget(popupLabel)
  52. layout.add_widget(closeButton)
  53. # Instantiate the modal popup and display
  54. popup = Popup(
  55. title="Demo Popup",
  56. content=layout,
  57. size_hint=(None, None),
  58. size=(200, 200),
  59. )
  60. popup.open()
  61. # Attach close button press with popup.dismiss action
  62. closeButton.bind(on_press=popup.dismiss)
  63. # Run the app
  64. if __name__ == "__main__":
  65. PopupExample().run()