more example code

This commit is contained in:
Daniel afx 2020-09-29 01:38:35 +03:00
parent 5d10bd2e80
commit 6c57d78a2e
2 changed files with 207 additions and 5 deletions

129
main.kv
View file

@ -1,4 +1,132 @@
#: import sm kivy.uix.screenmanager #: import sm kivy.uix.screenmanager
#: import CheckBox kivy.uix.checkbox
<CustLabel@Label>
color: 1, 1, 1, 1
<CustomPopup>:
size_hint: .5, .5
auto_dismiss: False
title: "The Popup"
Button:
text: "Close"
on_press: root.dismiss()
SampleBoxLayout:
<SampleBoxLayout>:
orientation: "vertical"
padding: 10
spacing: 10
BoxLayout:
orientation: "horizontal"
size_hint_x: .22
CustLabel:
text: "Are you over 18"
size_hint_x: .80
CheckBox:
on_active: root.checkbox_18_clicked(self, self.active)
size_hint_x: .55
BoxLayout:
orientation: "horizontal"
size_hint_x: .55
CustLabel:
text: "Favorite Color"
size_hint_x: .265
CheckBox:
group: "fav_color"
value: root.blue
size_hint_x: .05
CustLabel:
text: "Blue"
size_hint_x: .15
CheckBox:
group: "fav_color"
value: root.red
size_hint_x: .05
CustLabel:
text: "Red"
size_hint_x: .15
CheckBox:
group: "fav_color"
value: root.green
size_hint_x: .05
CustLabel:
text: "Green"
size_hint_x: .15
BoxLayout:
orientation: "horizontal"
height: 30
BoxLayout:
orientation: "horizontal"
size_hint_x: .25
CustLabel:
text: str(slider_id.value)
Slider:
id: slider_id
min: -100
max: 100
value: 0
step: 1
BoxLayout:
orientation: "horizontal"
size_hint_x: .25
CustLabel:
text: "On / Off"
Switch:
id: switch_id
on_active: root.switch_on(self, self.active)
BoxLayout:
orientation: "horizontal"
height: 30
BoxLayout:
orientation: "horizontal"
size_hint_x: .25
Button:
text: "Open Popup"
on_press: root.open_popup()
BoxLayout:
orientation: "horizontal"
size_hint_x: .25
Spinner:
text: "First"
values: ["First", "Second", "Third"]
id: spinner_id
on_text: root.spinner_clicked(spinner_id.text)
BoxLayout:
orientation: "horizontal"
size_hint_x: 30
BoxLayout:
orientation: "horizontal"
size_hint_x: .25
TabbedPanel:
do_default_tab: False
TabbedPanelItem:
text: "1st Tab"
Label:
text: "Content of First Panel"
color: 1, 1, 1, 1
TabbedPanelItem:
text: "2nd Tab"
Label:
text: "Content of Second Panel"
color: 1, 1, 1, 1
TabbedPanelItem:
text: "3rd Tab"
Label:
text: "Content of Third Panel"
color: 1, 1, 1, 1
<ScreenManagement>: <ScreenManagement>:
transition: sm.FadeTransition() transition: sm.FadeTransition()
MainScreen: MainScreen:
@ -6,5 +134,4 @@
<MainScreen>: <MainScreen>:
<AboutScreen>: <AboutScreen>:

83
main.py
View file

@ -2,15 +2,49 @@ from random import randrange
from kivy.app import App from kivy.app import App
from kivy.clock import Clock from kivy.clock import Clock
from kivy.lang import Builder
from kivy.core.window import Window from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.widget import Widget from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.graphics import Rectangle from kivy.graphics import Rectangle
Builder.load_string("""
<ScreenOne>:
BoxLayout:
Button:
text: "Go to Screen 2"
on_press:
root.manager.transition.direction = "left"
root.manager.transition.duration = 1
root.manager.current = "screen_two"
<ScreenTwo>:
BoxLayout:
Button:
text: "Go to Screen 1"
on_press:
root.manager.transition.direction = "right"
root.manager.transition.duration = 1
root.manager.current = "screen_one"
""")
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
screen_manager = ScreenManager()
screen_manager.add_widget(ScreenOne(name="screen_one"))
screen_manager.add_widget(ScreenTwo(name="screen_two"))
def collides(rect1, rect2): def collides(rect1, rect2):
r1x = rect1[0][0] r1x = rect1[0][0]
r1y = rect1[0][1] r1y = rect1[0][1]
@ -85,6 +119,47 @@ class GlobalForest(App):
#return Label(text='Hello world') #return Label(text='Hello world')
return GameWidget() return GameWidget()
class CustomPopup(Popup):
pass
class SampleBoxLayout(BoxLayout):
checkbox_is_active = ObjectProperty(False)
def checkbox_18_clicked(self, instance, value):
if value is True:
print("Checkbox Checked")
else:
print("Checkbox is Unchecked")
blue = ObjectProperty(True)
red = ObjectProperty(False)
green = ObjectProperty(False)
def switch_on(self, instance, value):
if value is True:
print("Switch On")
else:
print("Switch Off")
def open_popup(self):
the_popup = CustomPopup()
the_popup.open()
def spinner_clicked(self, value):
print("Spinner Value " + value)
class MainApp(App):
def build(self):
#Window.clearcolor = (1, 1, 1, 1)
return screen_manager
global app, screens
app = self
screens = {'main_screen': MainScreen(name='main_screen'),
'about_screen': AboutScreen(name='about_screen')}
self.screen_manager = ScreenManager
self.main()
if __name__ == '__main__': if __name__ == '__main__':
app = GlobalForest() #app = GlobalForest()
app = MainApp()
app.run() app.run()