main1.py
main1.py
— 2.6 KB
Содержимое файла
import kivy # base Class of your App inherits from the App class. # app:always refers to the instance of your application from kivy.app import App # this restrict the kivy version i.e # below this kivy version you cannot # use the app or software kivy.require('1.9.0') # drop-down menu is a list of items that # appear whenever a piece of text or a # button is clicked. # To use drop down you must have ti import it from kivy.uix.dropdown import DropDown # module consists the floatlayout # to work with FloatLayout first # you have to import it from kivy.uix.floatlayout import FloatLayout # The Button is a Label with associated actions that # are triggered when the button is pressed ( # or released after a click / touch). from kivy. uix . button import Button class CustomDropDown(DropDown): pass class DropdownDemo(FloatLayout): '''The code of the application itself.''' def __init__(self, **kwargs): '''The button at the opening of the window is created here, not in kv ''' super(DropdownDemo, self).__init__(**kwargs) #self.dropdown = CustomDropDown() dropdown = CustomDropDown() notes = ['Features', 'Suggestions', 'Abreviations', 'Miscellaneous'] for note in range(5): # when adding widgets, we need to specify the height manually (disabling # the size_hint_y) so the dropdown can calculate the area it needs. btn = Button(text='%r' % str(note), size_hint_y=None, height=30) # for each button, attach a callback that will call the select() method # on the dropdown. We'll pass the text of the button as the data of the # selection. btn.bind(on_release=lambda btn: dropdown.select(btn.text)) # then add the button inside the dropdown dropdown.add_widget(btn) # create a big main button # Creating a self widget bouton self.mainbutton = Button(text ='Do you in college?', size_hint_x = 0.6, size_hint_y = 0.15) # Added button to FloatLayout so inherits this class self.add_widget(self.mainbutton) # Adding actions # If click self.mainbutton.bind(on_release=dropdown.open) # root.select on_select called dropdown.bind(on_select=lambda instance, x: setattr(self.mainbutton, 'text', x)) def callback(self, instance, x): '''x is self.mainbutton.text refreshed''' print ( "The chosen mode is: {0}" . format ( x ) ) class Main1App(App): '''The build function returns root, here root = DropdownDemo (). root can only be called in the kv file. ''' def build(self): return DropdownDemo() if __name__ == '__main__': Main1App().run()