Advertisement
Leva7

Untitled

Mar 20th, 2016
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. import string
  2. from datetime import datetime
  3.  
  4. from kivy.app import App
  5. from kivy.lang import Builder
  6. from kivy.core.window import Window
  7. from kivy.effects.scroll import ScrollEffect
  8. from kivy.uix.screenmanager import Screen, ScreenManager, NoTransition
  9. from kivy.uix.label import Label
  10. from kivy.uix.widget import Widget
  11. from kivy.uix.textinput import TextInput
  12. from kivy.uix.button import Button
  13. from kivy.uix.scrollview import ScrollView
  14. from kivy.uix.floatlayout import FloatLayout
  15. from kivy.uix.boxlayout import BoxLayout
  16.  
  17. global msg_stack, pr_msg_y
  18.  
  19. msg_stack = []
  20. pr_msg_y = 0
  21.  
  22. Builder.load_string('''    
  23. <ScrollView>:
  24.    canvas:
  25.        Color:
  26.            rgba: (1, 1, 1, 1)
  27.        Rectangle:
  28.            pos: self.pos
  29.            size: self.size
  30.  
  31. <Message>:
  32.    FloatLayout:
  33.        canvas:
  34.            Color:
  35.                rgba: 0, 0, 0, 1
  36.            RoundedRectangle:
  37.            Color:
  38.                rgba: 1, 1, 1, 1
  39.            RoundedRectangle:
  40.                pos: self.x + 1, self.y + 1
  41.                size: self.width - 2, self.height - 2
  42.  
  43.        Button:
  44.            background_normal: ''
  45.            background_down: ''
  46.            background_color: 0, 0, 0, 0
  47.            on_press: root.test()
  48.            TextInput:
  49.                id: msg
  50.                font_name: "E:\Games\Python 3.4\OpenSans\Arev.ttf"
  51.                background_color: 0, 0, 0, 0
  52.                readonly: True
  53.                text: str(msg)
  54.                cursor_color: 0, 0, 0, 0
  55.  
  56.        Label:
  57.            id: time
  58.            size: 30, self.height
  59.            background_color: 0, 0, 0, 1
  60. ''')
  61.  
  62. class Message(Widget):
  63.     def __init__(self, **kwargs):
  64.         super(Message, self).__init__(**kwargs)
  65.         print("Message created")
  66.  
  67.     def test(self, *args, **kwargs):
  68.         print("Touch received")
  69.  
  70. class Chat(Screen):
  71.     pass       
  72.  
  73. class ChatApp(App):
  74.     def build(self):
  75.         def msg_in(*args, **kwargs):
  76.             global pr_msg_y, msg_stack
  77.             msg = tx1_main.text.strip("\n ")
  78.             if msg not in string.whitespace:
  79.                 msg_stack.append(Message())  
  80.  
  81.                 time = datetime.now().strftime("%H:%M")
  82.                 msg_stack[-1].ids['time'].text = time
  83.  
  84.                 msg_stack[-1].ids['msg'].text = msg
  85.  
  86.                 print((len(msg_stack[-1].ids['msg']._lines) + 1) * msg_stack[-1].ids['msg'].line_height)
  87.                 msg_stack[-1].height = (len(msg_stack[-1].ids['msg']._lines) + 1) * msg_stack[-1].ids['msg'].line_height
  88.                 msg_stack[-1].y = sv1_main.height - 5 - pr_msg_y - msg_stack[-1].height                  
  89.                 msg_stack[-1].x = 5
  90.  
  91.                 tx1_main.text = ''
  92.  
  93.                 msg_float.add_widget(msg_stack[-1])
  94.                 pr_msg_y += msg_stack[-1].height + 5
  95.  
  96.         Screens = ScreenManager(transition = NoTransition())
  97.         chat = Chat(name = "main")
  98.  
  99.         tx1_main = TextInput(size_hint = (0.9, 0.155),         
  100.                              pos_hint = {"top":0.177, "center_x":0.465})
  101.  
  102.         sv1_main = ScrollView(pos_hint = {"top":0.87, "center_x":0.5},
  103.                               size_hint = (0.97, 0.65))
  104.  
  105.         msg_float = FloatLayout()
  106.  
  107.         bt1_main = Button(size_hint = (0.061, 0.078),
  108.                           pos_hint = {"top":0.097, "center_x":0.951},
  109.                           text = "Send",
  110.                           on_press = msg_in)
  111.  
  112.         chat.add_widget(tx1_main)
  113.         chat.add_widget(sv1_main)
  114.         sv1_main.add_widget(msg_float)
  115.         chat.add_widget(bt1_main)
  116.  
  117.         Screens.add_widget(chat)
  118.  
  119.         return Screens
  120.  
  121. if __name__ == "__main__":
  122.     ChatApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement