Kivy 案例: 複製黏貼按鈕彈窗 - 图1
© dotmodus

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

Kivy 案例: 複製黏貼按鈕彈窗 - 图2
example from document

  1. '''
  2. Bubble
  3. ======
  4. Test of the widget Bubble.
  5. '''
  6. from kivy.app import App
  7. from kivy.uix.floatlayout import FloatLayout
  8. from kivy.uix.button import Button
  9. from kivy.lang import Builder
  10. from kivy.uix.bubble import Bubble
  11. Builder.load_string('''
  12. <cut_copy_paste>
  13. size_hint: (None, None)
  14. size: (160, 120)
  15. pos_hint: {'center_x': .5, 'y': .6}
  16. BubbleButton:
  17. text: 'Cut'
  18. BubbleButton:
  19. text: 'Copy'
  20. BubbleButton:
  21. text: 'Paste'
  22. ''')
  23. class cut_copy_paste(Bubble):
  24. pass
  25. class BubbleShowcase(FloatLayout):
  26. def __init__(self, **kwargs):
  27. super(BubbleShowcase, self).__init__(**kwargs)
  28. self.but_bubble = Button(text='Press to show bubble')
  29. self.but_bubble.bind(on_release=self.show_bubble)
  30. self.add_widget(self.but_bubble)
  31. def show_bubble(self, *l):
  32. if not hasattr(self, 'bubb'):
  33. self.bubb = bubb = cut_copy_paste()
  34. self.add_widget(bubb)
  35. else:
  36. values = ('left_top', 'left_mid', 'left_bottom', 'top_left',
  37. 'top_mid', 'top_right', 'right_top', 'right_mid',
  38. 'right_bottom', 'bottom_left', 'bottom_mid', 'bottom_right')
  39. index = values.index(self.bubb.arrow_pos)
  40. self.bubb.arrow_pos = values[(index + 1) % len(values)]
  41. class TestBubbleApp(App):
  42. def build(self):
  43. return BubbleShowcase()
  44. if __name__ == '__main__':
  45. TestBubbleApp().run()

Show bubble when touch the Input box

origin source: ikolim, stackoverflow

  1. from kivy.app import App
  2. from kivy.uix.floatlayout import FloatLayout
  3. from kivy.uix.bubble import Bubble, BubbleButton
  4. from kivy.uix.label import Label
  5. from kivy.properties import ObjectProperty
  6. from kivy.lang import Builder
  7. Clipboard = None
  8. from kivy.core.clipboard import Clipboard
  9. from kivy.clock import Clock
  10. import time
  11. class CustomBubbleButton(BubbleButton):
  12. pass
  13. class NumericKeyboard(Bubble):
  14. layout = ObjectProperty(None)
  15. def __init__(self, **kwargs):
  16. super(NumericKeyboard, self).__init__(**kwargs)
  17. self.create_bubble_button()
  18. def create_bubble_button(self):
  19. numeric_keypad = [Clipboard.paste()]
  20. for x in numeric_keypad:
  21. if len(x) == 0:
  22. self.layout.add_widget(Label(text=""))
  23. else:
  24. bubb_btn = CustomBubbleButton(text=str(x))
  25. self.layout.add_widget(bubb_btn)
  26. #Clock.schedule_once(self.REMOVE, 1)
  27. #self.layout.add_widget(bubb_btn)
  28. class BubbleShowcase(FloatLayout):
  29. text_input = ObjectProperty(None)
  30. def REMOVE(self, dt):
  31. self.remove_widget(self.bubb)
  32. #print('12')
  33. def show_bubble(self, *l):
  34. # If you annotate this line, the wedge would survive from self.REMOVE by clicking the bubble.
  35. if not hasattr(self, 'bubb'):
  36. self.bubb = bubb = NumericKeyboard()
  37. self.bubb.arrow_pos = "bottom_mid"
  38. self.add_widget(self.bubb)
  39. # close the bubble after 3s
  40. Clock.schedule_once(self.REMOVE, 3)
  41. #self.remove_widget(self.bubb)
  42. Builder.load_file("test.kv")
  43. class TestBubbleApp(App):
  44. title = "Numeric Key Pad - Using Bubble"
  45. def build(self):
  46. return BubbleShowcase()
  47. if __name__ == '__main__':
  48. TestBubbleApp().run()

test.kv

  1. ##:kivy 1.10.0
  2. <CustomBubbleButton>:
  3. on_release:
  4. app.root.text_input.text += self.text
  5. <NumericKeyboard>:
  6. layout: layout
  7. size_hint: (None, None)
  8. size: (160, 120)
  9. pos_hint: {'center_x': .5, 'y': .6}
  10. GridLayout:
  11. id: layout
  12. cols: 3
  13. <BubbleShowcase>:
  14. text_input: text_input
  15. canvas:
  16. Color:
  17. rgba: 0, 1, 1, 1
  18. Rectangle:
  19. size: self.width, self.height
  20. TextInput:
  21. id: text_input
  22. pos_hint: {'center_x': .5, 'y': .54}
  23. size_hint: (0.2, 0.06)
  24. cursor_blink: True
  25. font_size: 20
  26. multiline: False
  27. on_focus:
  28. root.show_bubble()

Kivy 案例: 複製黏貼按鈕彈窗 - 图3

So, by setting the clock, it could automatically vanished.


Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗