📜  浮动动作按钮 kivy 极客 - 任何代码示例

📅  最后修改于: 2022-03-11 15:00:18.053000             🧑  作者: Mango

代码示例1
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
   
# BoxLayout arranges widgets in either
# in a vertical fashion that
# is one on top of another or in a horizontal
# fashion that is one after another.
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
   
# To change the kivy default settings 
# we use this module config 
from kivy.config import Config 
      
# 0 being off 1 being on as in true / false 
# you can use 0 or 1 && True or False 
Config.set('graphics', 'resizable', True)
 
# creating the root widget used in .kv file
Builder.load_string(
'''
:
    id: float_root
    size_hint: (None, None)
    text: ""
    btn_size: (70,70)
    size: (70,70)
    bg_color: (1,0,0,1)
    pos_hint: {'x': .6}

    # Adding shape and all, size, position to button
    Button:
        text: float_root.text
        markup: True
        font_size: 40
        pos_hint: {'x': 5.5, 'y':3.5}
        background_normal: '' # serves to purify color depth/remove tainting
        background_color: 1,0,0,0 # Make the Button be Transparent  so that it can be see through to the background color
        on_press: float_root.bg_color = (1,0,0,1) if self.state == 'normal' else (0,0,1,1) # on press change button color to blue 
        on_release:float_root.bg_color = (0,0,1,1) if float_root.bg_color == ''  else (1,0,0,1) # on release change back to to red
        
        # Below is the background color of the see through button
        canvas.before:
            Color:
                rgba: float_root.bg_color 
            Ellipse:
                size: self.size
                pos: self.pos
:
    #kv_canvas
    canvas.before:
        Color:
            rgba: (1,1,1,1) #(48/255,84/255,150/255,1)
        Rectangle:
            #radius: [58]
            pos: self.pos
            size: self.size

    size: root.width, root.height
    FloatButton:
        text: '+'
        markup: True



        ''')
class MainWindow(BoxLayout):
    pass
     
 
# creating the App class in which name

class MainApp(App):
    # defining build()
    def build(self):
        
        return MainWindow()
 
# run the app
if __name__ == '__main__':
    MainApp().run()