python mah_button.py
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python mah_button.py相关的知识,希望对你有一定的参考价值。
'''
Little demonstration of various kivy useful features:
- loading kv code
- adding dynamic parts to static parts using ids
- binding events to python expressions on kv side
useful references:
- http://kivy.org/docs/guide/lang.html#referencing-widgets
- http://kivy.org/docs/api-kivy.uix.widget.html?highlight=ids#kivy.uix.widget.Widget.ids
- http://kivy.org/docs/api-kivy.uix.widget.html?highlight=size%20hint#kivy.uix.widget.Widget.size_hint
- http://kivy.org/docs/api-kivy.uix.screenmanager.html?highlight=screenmanager#kivy.uix.screenmanager
'''
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen
from string import letters
kv = ''' # write the string to a variable to pass it to the builder later
BoxLayout:
# Define it as 'BoxLayout' http://kivy.org/docs/api-kivy.uix.layout.html
orientation: 'vertical' # with the property vertical so it can be placed on the top of the screen
BoxLayout:
size_hint_y: None # Otherwise the parent would autoscale it to halfscreen.
height: 30 # height in pixel
id: buttons # The id of the button, we will later use it to integrate it.
ScreenManager:
id: sm # The id of the ScreenManager, we will later use it to integrate it.
<MahButton>:
on_press: app.root.ids.sm.current = self.text # on press this button:
''' # # --> the ScreenManager changes to the SM with the Title that is self.text
class MahButton(Button): # Inherits Mahbutton with Button
pass # Pass does nothing is useful as a placeholder when a statement is required
class MahApp(App): # The main application
def build(self):
self.root = Builder.load_string(kv) # Execute the string kv as kivy code
for l in letters:
s = Screen(name=l) # Creates a new screen
s.add_widget(Label(text=l)) # Adds the letters to the screen
self.root.ids.sm.add_widget(s) # Adds the screen to the ScreenManager
# -> which is located in self.root under the id ids.sm
self.root.ids.buttons.add_widget( # Adds buttons as widgets
MahButton(text=l)) # Adds the Button with the text
return self.root # Return self.root so it can be run
if __name__ == '__main__': # Executes code in this file but excludes imported main()
MahApp().run() # Runs the application.
以上是关于python mah_button.py的主要内容,如果未能解决你的问题,请参考以下文章