======Kivy: Liste des éléments graphiques Widgets complexes======
**{{tagpage>kivy|Toutes les pages Kivy}}**
**[[http://translate.google.com/translate?hl=&sl=fr&tl=en&u=https%3A%2F%2Fressources.labomedia.org%2Fkivy_liste_des_elements_graphiques_widgets_complexes&sandbox=1|English Version]]**
**[[les_pages_kivy_en_details|Les pages Kivy en détails]]**
===== Buble =====
{{media_05:bubble1.png?200|Bubble1.png Bubble1.png}}
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.bubble import Bubble
from kivy.properties import ObjectProperty
class Cut_copy_paste(Bubble):
pass
class BubbleDemo(FloatLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.but_bubble = Button(text='Press to show bubble')
self.but_bubble.bind(on_release=self.show_bubble)
self.add_widget(self.but_bubble)
self.bubb = Cut_copy_paste()
def show_bubble(self, *l):
if not hasattr(self, 'self.bubb'):
self.add_widget(self.bubb)
else:
values = ('left_top', 'left_mid', 'left_bottom', 'top_left',
'top_mid', 'top_right', 'right_top', 'right_mid',
'right_bottom', 'bottom_left', 'bottom_mid', 'bottom_right')
index = values.index(self.bubb.arrow_pos)
self.bubb.arrow_pos = values[(index + 1) % len(values)]
class BubbleApp(App):
def build(self):
return BubbleDemo()
if __name__ == '__main__':
BubbleApp().run()
:
size_hint: (None, None)
size: (160, 120)
pos_hint: {'center_x': .5, 'y': .6}
BubbleButton:
text: 'Cut'
BubbleButton:
text: 'Copy'
BubbleButton:
text: 'Paste'
===== Drop-Down List =====
Sélection dans une liste et exemple d'utilisation du choix.\\
{{media_03:dropdown1.png?300|fig:Dropdown1.png}}
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Ce script décrit:
* les interactions avec les fichiers py et kv,
* les héritages des class
* les méthodes qu'il faut utiliser.
'''
from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
class CustomDropDown(DropDown):
'''Lit : dans Dropdown.kv'''
pass
class DropdownDemo(FloatLayout):
'''Le code de l'application proprement dite.'''
def __init__(self, **kwargs):
'''Le bouton présent à l'ouverture de la fenêtre est créé ici,
pas dans kv
'''
super().__init__(**kwargs)
self.dropdown = CustomDropDown()
# Création d'un widget bouton
self.mainbutton = Button(text='Etes-vous poli ou mal poli?',
size_hint_x=0.6, size_hint_y=0.15)
# Ajout du bouton au FloatLayout donc hérite cette class
self.add_widget(self.mainbutton)
# Ajout des actions
# Si clic
self.mainbutton.bind(on_release=self.dropdown.open)
# root.select appelle on_select
self.dropdown.bind(on_select=lambda\
instance, x: setattr(self.mainbutton, 'text', x))
self.dropdown.bind(on_select=self.callback)
def callback(self, instance, x):
'''x est self.mainbutton.text actualisé'''
print("Le mode choisi est: {0}".format(x))
class DropdownApp(App):
'''La fonction build retourne root,
ici root = DropdownDemo().
root ne peut être appelé que dans le fichier kv.
'''
def build(self):
return DropdownDemo()
if __name__ == '__main__':
'''La class s'appelle DropdownApp() parce que le fichier *.kv
s'appelle Dropdown.kv, soit:
* Majuscule au début
* App à la fin
'''
DropdownApp().run()
#:kivy 1.8.0
:
Button:
text: 'Ca sert a quoi ce putain de bordel'
size_hint_y: None
height: 44
on_release: root.select('mal poli')
Label:
text: 'Item qui ne fait rien : fainéant !'
size_hint_y: None
height: 44
Button:
text: 'Je dois resté poli !'
size_hint_y: None
height: 44
on_release: root.select('poli')
===== File Chooser =====
{{media_06:filechooser1.png?300|Filechooser1.png Filechooser1.png}}
On cherche à comprendre FileChooser et hop on te colle du Factory.register ! Nul !
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
import os
class LoadDialog(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
class SaveDialog(FloatLayout):
save = ObjectProperty(None)
text_input = ObjectProperty(None)
cancel = ObjectProperty(None)
class Root(FloatLayout):
loadfile = ObjectProperty(None)
savefile = ObjectProperty(None)
text_input = ObjectProperty(None)
def dismiss_popup(self):
self._popup.dismiss()
def show_load(self):
content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
self._popup = Popup(title="Load file", content=content,
size_hint=(0.9, 0.9))
self._popup.open()
def show_save(self):
content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
self._popup = Popup(title="Save file", content=content,
size_hint=(0.9, 0.9))
self._popup.open()
def load(self, path, filename):
with open(os.path.join(path, filename[0])) as stream:
self.text_input.text = stream.read()
self.dismiss_popup()
def save(self, path, filename):
with open(os.path.join(path, filename), 'w') as stream:
stream.write(self.text_input.text)
self.dismiss_popup()
class Filechooser(App):
pass
# Je ne comprends pas
Factory.register('Root', cls=Root)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)
if __name__ == '__main__':
Filechooser().run()
#:kivy 1.1.0
Root:
text_input: text_input
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: None
height: 30
Button:
text: 'Load'
on_release: root.show_load()
Button:
text: 'Save'
on_release: root.show_save()
BoxLayout:
TextInput:
id: text_input
text: ''
RstDocument:
text: text_input.text
show_errors: True
:
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
BoxLayout:
size_hint_y: None
height: 30
Button:
text: "Cancel"
on_release: root.cancel()
Button:
text: "Load"
on_release: root.load(filechooser.path, filechooser.selection)
:
text_input: text_input
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
on_selection: text_input.text = self.selection and self.selection[0] or ''
TextInput:
id: text_input
size_hint_y: None
height: 30
multiline: False
BoxLayout:
size_hint_y: None
height: 30
Button:
text: "Cancel"
on_release: root.cancel()
Button:
text: "Save"
on_release: root.save(filechooser.path, text_input.text)
===== Spinner =====
{{media_12:spinner.jpg?200|Spinner.jpg Spinner.jpg}}
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from kivy.base import runTouchApp
from kivy.uix.spinner import Spinner
spinner = Spinner(
# default value shown
text='Home',
# available values
values=('Home', 'Work', 'Other', 'Custom'),
# just for positioning in our example
size_hint=(None, None),
size=(100, 44),
pos_hint={'center_x': .5, 'center_y': .5})
def show_selected_value(spinner, text):
print('The spinner', spinner, 'have text', text)
spinner.bind(text=show_selected_value)
runTouchApp(spinner)
===== List View =====
{{media_08:listview1.png?300|Listview1.png Listview1.png}}
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.base import runTouchApp
Builder.load_string("""
:
ListView:
item_strings: [str(index) for index in range(10)]
""")
class MyListView(BoxLayout):
pass
if __name__ == '__main__':
runTouchApp(MyListView())
===== Tabbed Panel =====
Des onglets classiques: {{:Tabbed1.png?300|fig:Tabbed1.png}}
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
Builder.load_string("""
:
size_hint: .5, .5
pos_hint: {'center_x': .5, 'center_y': .5}
do_default_tab: False
TabbedPanelItem:
text: 'Onglet 1'
Label:
text: "Label dans l'onglet"
TabbedPanelItem:
text: 'Onglet 2'
BoxLayout:
Label:
text: 'Un label est ici'
Button:
text: 'Bouton qui ne fait rien'
TabbedPanelItem:
text: 'Onglet 3'
RstDocument:
text: '\\n'.join(("Ceci est un RSTDocument"))
""")
class Test(TabbedPanel):
pass
class TabbedPanelApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TabbedPanelApp().run()
===== VideoPlayer =====
Le lecteur lit softboy.avi mais il a besoin aussi de softboy.png.
Double clic pour plein écran ! {{:Videoplayer1.png?300|fig:Videoplayer1.png}}
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import kivy
kivy.require('1.8.0')
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.videoplayer import VideoPlayer
parent = Widget()
parent.size = 400, 300
class MyApp(App):
def build(self):
button = Button(text='Video Player', font_size=14)
button.size = 400, 300
button.bind(on_press=on_button_press)
parent.add_widget(button) #add button
return parent
def on_button_press(self):
video= VideoPlayer(source='softboy.avi', state='play',
options={'allow_fullscreen': True})
parent.add_widget(video) #add videoplayer
return parent
if __name__ == '__main__':
MyApp().run()
===== Virtual Keyboard =====
Le clavier seul: {{:Vkeyboard1.png?300|fig:Vkeyboard1.png}}
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.vkeyboard import VKeyboard
class Test(VKeyboard):
player = VKeyboard()
class VkeyboardApp(App):
def build(self):
return Test()
if __name__ == '__main__':
VkeyboardApp().run()
{{tag> kivy python sb }}