Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- from datetime import datetime
- from kivy.app import App
- from kivy.lang import Builder
- from kivy.core.window import Window
- from kivy.effects.scroll import ScrollEffect
- from kivy.uix.screenmanager import Screen, ScreenManager, NoTransition
- from kivy.uix.label import Label
- from kivy.uix.widget import Widget
- from kivy.uix.textinput import TextInput
- from kivy.uix.button import Button
- from kivy.uix.scrollview import ScrollView
- from kivy.uix.floatlayout import FloatLayout
- from kivy.uix.boxlayout import BoxLayout
- global msg_stack, pr_msg_y
- msg_stack = []
- pr_msg_y = 0
- Builder.load_string('''
- <ScrollView>:
- canvas:
- Color:
- rgba: (1, 1, 1, 1)
- Rectangle:
- pos: self.pos
- size: self.size
- <Message>:
- FloatLayout:
- canvas:
- Color:
- rgba: 0, 0, 0, 1
- RoundedRectangle:
- Color:
- rgba: 1, 1, 1, 1
- RoundedRectangle:
- pos: self.x + 1, self.y + 1
- size: self.width - 2, self.height - 2
- Button:
- background_normal: ''
- background_down: ''
- background_color: 0, 0, 0, 0
- on_press: root.test()
- TextInput:
- id: msg
- font_name: "E:\Games\Python 3.4\OpenSans\Arev.ttf"
- background_color: 0, 0, 0, 0
- readonly: True
- text: str(msg)
- cursor_color: 0, 0, 0, 0
- Label:
- id: time
- size: 30, self.height
- background_color: 0, 0, 0, 1
- ''')
- class Message(Widget):
- def __init__(self, **kwargs):
- super(Message, self).__init__(**kwargs)
- print("Message created")
- def test(self, *args, **kwargs):
- print("Touch received")
- class Chat(Screen):
- pass
- class ChatApp(App):
- def build(self):
- def msg_in(*args, **kwargs):
- global pr_msg_y, msg_stack
- msg = tx1_main.text.strip("\n ")
- if msg not in string.whitespace:
- msg_stack.append(Message())
- time = datetime.now().strftime("%H:%M")
- msg_stack[-1].ids['time'].text = time
- msg_stack[-1].ids['msg'].text = msg
- print((len(msg_stack[-1].ids['msg']._lines) + 1) * msg_stack[-1].ids['msg'].line_height)
- msg_stack[-1].height = (len(msg_stack[-1].ids['msg']._lines) + 1) * msg_stack[-1].ids['msg'].line_height
- msg_stack[-1].y = sv1_main.height - 5 - pr_msg_y - msg_stack[-1].height
- msg_stack[-1].x = 5
- tx1_main.text = ''
- msg_float.add_widget(msg_stack[-1])
- pr_msg_y += msg_stack[-1].height + 5
- Screens = ScreenManager(transition = NoTransition())
- chat = Chat(name = "main")
- tx1_main = TextInput(size_hint = (0.9, 0.155),
- pos_hint = {"top":0.177, "center_x":0.465})
- sv1_main = ScrollView(pos_hint = {"top":0.87, "center_x":0.5},
- size_hint = (0.97, 0.65))
- msg_float = FloatLayout()
- bt1_main = Button(size_hint = (0.061, 0.078),
- pos_hint = {"top":0.097, "center_x":0.951},
- text = "Send",
- on_press = msg_in)
- chat.add_widget(tx1_main)
- chat.add_widget(sv1_main)
- sv1_main.add_widget(msg_float)
- chat.add_widget(bt1_main)
- Screens.add_widget(chat)
- return Screens
- if __name__ == "__main__":
- ChatApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement