ElliotDG

Untitled

Jun 13th, 2024
94
0
22 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.lang import Builder
  3. from kivy.uix.behaviors import ButtonBehavior
  4. from kivy.uix.boxlayout import BoxLayout
  5. from kivy.properties import StringProperty
  6.  
  7. from textwrap import dedent
  8.  
  9. kv = """
  10. <PostUnit>
  11.    orientation: 'vertical'
  12.    size_hint_y: None
  13.    height: self.minimum_height
  14.    on_release: root.update_posts()
  15.    canvas:
  16.        Color:
  17.            rgb: .8, .8, .8
  18.        RoundedRectangle:
  19.            size: self.size
  20.            pos: self.pos
  21.    BoxLayout:
  22.        size_hint_y: None
  23.        height: dp(40)
  24.        padding: dp(10)
  25.        Label:
  26.            id: writer
  27.            text: f'Writer : {root.writer}'
  28.            color: 'black'
  29.            text_size: self.size  # text_size allows the alignment to be set
  30.            halign: 'left'
  31.            valign: 'middle'
  32.        Label:
  33.            id: writedate
  34.            text: f'Write date : {root.date}'
  35.            color: 'black'
  36.            text_size: self.size
  37.            halign: 'left'
  38.            valign: 'middle'
  39.    Label:
  40.        id: content
  41.        text: root.content
  42.        color: 'black'
  43.        size_hint_y: None
  44.        height: self.texture_size[1]  # scale widget with text size
  45.        padding: dp(20)
  46.        text_size: self.width, None  # use text size to control wrapping
  47.        halign: 'left'
  48.        valign: 'middle'
  49.  
  50. BoxLayout:
  51.    orientation: 'vertical'
  52.    Label:
  53.        text: 'Example of ScrollView'
  54.        size_hint_y: None
  55.        height: dp(30)  
  56.    ScrollView:
  57.        do_scroll_x: False
  58.        do_scroll_y: True
  59.        BoxLayout:
  60.            id: body
  61.            orientation: 'vertical'
  62.            size_hint_y: None
  63.            spacing: dp(5)
  64.            height: self.minimum_height
  65. """
  66. # content from chatGPT, content and posts used for testing...
  67.  
  68. c0 = dedent('Software development often requires meticulous attention to detail.'
  69.             'Every line of code contributes to the overall functionality, '
  70.             'and even a small bug can lead to significant issues.')
  71.  
  72. c1 = dedent('Version control systems, such as Git, play a crucial role in modern software development.'
  73.             'They allow multiple developers to collaborate on a project simultaneously, '
  74.             'tracking changes and managing versions effectively. By using branches, developers '
  75.             'can work on new features or bug fixes in isolation, merging their changes into the '
  76.             'main codebase only when they are ready. This not only enhances collaboration but also ensures '
  77.             'that the codebase remains stable and reliable throughout the development lifecycle.')
  78.  
  79. c2 = dedent("In addition to coding and testing, documentation is an essential aspect of software development."
  80.             "Well-written documentation provides a clear roadmap for developers, enabling them to understand "
  81.             "the system's architecture, dependencies, and usage. "
  82.             "It also assists new team members in getting up to speed quickly, facilitating smoother onboarding."
  83.             "Comprehensive documentation includes user guides, API references, and troubleshooting manuals, "
  84.             "ensuring that end-users and developers alike can navigate and utilize the software effectively.")
  85.  
  86. posts = [{'writer': 'Big Bob',
  87.           'date': '2020-20-20',
  88.           'content': c0},
  89.          {'writer': 'Smart Sam',
  90.           'date': '2020-20-21',
  91.           'content': c1},
  92.          {'writer': 'Modern Mary',
  93.           'date': '2020-20-22',
  94.           'content': c2},
  95.          ] * 3
  96.  
  97.  
  98. class PostUnit(ButtonBehavior, BoxLayout):  # use ButtonBehavior to add on_release
  99.     writer = StringProperty()  # kivy properties handle kwargs automatically
  100.     date = StringProperty()
  101.     content = StringProperty()
  102.  
  103.     def update_posts(self):
  104.         print(f'A post from {self.writer} was pressed')
  105.  
  106.  
  107. class ShowScrollApp(App):
  108.     def build(self):
  109.         return Builder.load_string(kv)
  110.  
  111.     def on_start(self):
  112.         # create some posts
  113.         for post in posts:
  114.             w = PostUnit(**post)
  115.             self.root.ids.body.add_widget(w)
  116.  
  117.  
  118. ShowScrollApp().run()
  119.  
Add Comment
Please, Sign In to add comment