Advertisement
weiojfoiwjef

Untitled

Feb 23rd, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.16 KB | None | 0 0
  1. #Problem on line 333
  2. from kivy.app import App
  3. from kivy.lang import Builder
  4. from kivy.properties import ObjectProperty, StringProperty
  5. from kivy.uix.boxlayout import BoxLayout
  6. from kivy.uix.image import Image
  7. from kivymd.dialog import MDDialog
  8. from kivymd.theming import ThemeManager
  9. from kivymd.navigationdrawer import NavigationLayout
  10. from kivymd.list import OneLineAvatarListItem, ILeftBody
  11. from kivymd.toast import toast
  12. from kivymd.dialog import MDDialog
  13. from kivy.uix.popup import Popup
  14. from threading import Thread
  15.  
  16. import socket
  17.  
  18.  
  19. sock = socket.socket()
  20. sock.connect(('127.0.0.1', 6718))
  21. sock.sendall(b"add_contact hello llo")
  22.  
  23. sock = socket.socket()
  24. sock.connect(('127.0.0.1', 6718))
  25.  
  26. sock.sendall(b'new llo')
  27.  
  28.  
  29. class MyLayout(BoxLayout):
  30.     scr_mngr = ObjectProperty(None)
  31.  
  32.     def check_data_login(self):
  33.         username = self.scr_mngr.screen1.username.text
  34.         password = self.scr_mngr.screen1.password.text
  35.  
  36.         print(username)
  37.         print(password)
  38.         if username == "KivyMD" and password == "kivy":
  39.             self.ids["wrongpass"].text = ""
  40.             self.change_screen("screen2")
  41.         else:
  42.             self.ids["wrongpass"].text = "Wrong username or password, please try again"
  43.  
  44.     def change_screen(self, screen, *args):
  45.         self.scr_mngr.transition.direction = 'left'
  46.         self.scr_mngr.current = screen
  47.  
  48.     def back_to_chat(self):
  49.         self.scr_mngr.transition.direction = 'right'
  50.         self.scr_mngr.current = 'screen2'
  51.  
  52.  
  53. class nav_layout(NavigationLayout):
  54.     def print_text(self):
  55.         print('hello')
  56.  
  57.     def check_data_login(self):
  58.         username = self.ids.screen1.username.text
  59.         password = self.ids.screen1.password.text
  60.  
  61.         print(username)
  62.         print(password)
  63.         if username == "KivyMD" and password == "kivy":
  64.             self.change_screen("screen2")
  65.             self.ids.wrongpass.text = ""
  66.         else:
  67.             self.ids.wrongpass.text = \
  68.                 "Wrong username or password, please try again"
  69.  
  70.     def change_screen(self, screen, *args):
  71.         self.ids.scr_mngr.transition.direction = 'left'
  72.         self.ids.scr_mngr.current = screen
  73.  
  74.     def back_to_chat(self):
  75.         self.ids.scr_mngr.transition.direction = 'right'
  76.         self.ids.scr_mngr.current = 'screen2'
  77.  
  78.     def logout(self):
  79.         # logout function, returns to screen 1
  80.         self.ids.scr_mngr.current = 'screen1'
  81.  
  82. class UploadPopup(Popup):
  83.     def load(self, path, selection):
  84.         print(path, selection)
  85.  
  86.  
  87. KV = """
  88. #:import Toolbar kivymd.toolbar.Toolbar
  89. #:import MDNavigationDrawer kivymd.navigationdrawer.MDNavigationDrawer
  90. #:import NavigationLayout kivymd.navigationdrawer.NavigationLayout
  91. #:import NavigationDrawerDivider kivymd.navigationdrawer.NavigationDrawerDivider
  92. #:import NavigationDrawerToolbar kivymd.navigationdrawer.NavigationDrawerToolbar
  93. #:import MDTextField kivymd.textfields.MDTextField
  94. #:import MDSeparator kivymd.card.MDSeparator
  95. #:import MDThemePicker kivymd.theme_picker.MDThemePicker
  96. #:import CardTransition kivy.uix.screenmanager.CardTransition
  97. #:import Factory kivy.factory.Factory
  98.  
  99.  
  100. <MDCustomIconItem>:
  101.    text: root.text
  102.  
  103.    AvatarSampleWidget:
  104.        source: root.icon
  105.  
  106. <UploadPopup>:
  107.    id: popup
  108.    title: "Upload"
  109.    BoxLayout:
  110.        FileChooserIconView:
  111.            id: FileChoose
  112.            pos_hint_x: 0.5
  113.            pos_hint_y: 0.5
  114.            on_selection: root.load(FileChoose.path, FileChoose.selection)
  115.        MDRaisedButton:
  116.            text: "Upload"
  117.            text_color: (0,0,0,1)
  118.            on_release: root.load(FileChoose.path, FileChoose.selection)
  119.            on_release: popup.dismiss()
  120.  
  121.        MDRaisedButton:
  122.            text: "Close"
  123.            text_color: (0,0,0,1)
  124.            on_release: popup.dismiss()
  125.  
  126.  
  127. nav_layout:
  128.    id: nav_layout
  129.    MDNavigationDrawer:
  130.        id: nav_drawer
  131.        drawer_logo: 'logo.png'
  132.        NavigationDrawerToolbar:
  133.            title: 'hello'
  134.        NavigationDrawerIconButton:
  135.            icon: 'settings'
  136.            text: 'Account Settings'
  137.            on_release: root.change_screen('screen3')
  138.        NavigationDrawerIconButton:
  139.            icon: 'face'
  140.            text: 'Friends'
  141.            on_release: root.print_text()
  142.        NavigationDrawerIconButton:
  143.            icon: 'logout'
  144.            text: 'Logout'
  145.            on_release: root.logout()
  146.        NavigationDrawerDivider:
  147.            height: dp(1)
  148.    MyLayout:
  149.        scr_mngr: scr_mngr
  150.        orientation: 'vertical'
  151.  
  152.  
  153.        ScreenManager:
  154.  
  155.            transition: CardTransition()
  156.            id: scr_mngr
  157.            screen1: screen1
  158.  
  159.            Screen:
  160.                id: screen1
  161.                name: 'screen1'
  162.                username: username
  163.                password: password
  164.  
  165.                BoxLayout:
  166.                    size_hint: None, None
  167.                    size: dp(520), dp(340)
  168.                    pos_hint: {'center_x': 0.5, 'center_y': 0.5}
  169.  
  170.                    BoxLayout:
  171.                        orientation:'vertical'
  172.                        padding: dp(20)
  173.                        spacing:20
  174.  
  175.                        MDLabel:
  176.                            text: 'Chat App'
  177.                            theme_text_color: 'Secondary'
  178.                            font_style:"Title"
  179.                            size_hint_y: None
  180.                            height: dp(36)
  181.  
  182.                        MDSeparator:
  183.                            height: dp(1)
  184.  
  185.                        MDTextField:
  186.                            id: username
  187.                            hint_text: "Username "
  188.                            size_hint_y: 0.9
  189.                            helper_text_mode: "on_focus"
  190.  
  191.                        MDTextField:
  192.                            id: password
  193.                            hint_text: "Password "
  194.                            helper_text_mode: "on_focus"
  195.                            size_hint_y: 0.9
  196.                            password: True
  197.  
  198.                        MDFlatButton:
  199.                            text: "Login"
  200.                            pos_hint: {'center_x': 0.5}
  201.                            on_release: root.check_data_login()
  202.  
  203.                        MDLabel:
  204.                            id: wrongpass
  205.                            color: 1,0,1,1
  206.                            text: ""
  207.  
  208.  
  209.  
  210.            Screen:
  211.                name: 'screen2'
  212.                id: screen2
  213.  
  214.                Toolbar:
  215.                    id: toolbar
  216.                    title: "Welcome ! "
  217.                    pos_hint: {'center_x': 0.5, 'center_y': 0.96}
  218.                    md_bg_color: app.theme_cls.primary_color
  219.                    background_palette: 'DeepPurple'
  220.                    background_hue: 'A400'
  221.                    left_action_items: [['menu', lambda x: app.root.toggle_nav_drawer() ]]
  222.                    right_action_items: [['animation', lambda x: MDThemePicker().open()], ['camera', lambda x: print('hello')]]
  223.  
  224.                MDLabel:
  225.                    font_style: 'Title'
  226.                    theme_text_color: 'Primary'
  227.                    text: "Data :"
  228.                    height: self.texture_size[1] + dp(3)
  229.                    halign: 'center'
  230.                    pos_hint: {'center_x': 0.5, 'center_y': 0.5}
  231.  
  232.  
  233.  
  234.            Screen:
  235.                name: 'screen3'
  236.                id: 'screen3'
  237.  
  238.                Toolbar:
  239.                    id: tools
  240.                    title: "Your Profile"
  241.                    pos_hint: {'center_x': 0.5, 'center_y': 0.96}
  242.                    md_bg_color: app.theme_cls.primary_color
  243.                    background_palette: 'DeepPurple'
  244.                    background_hue: 'A400'
  245.                    left_action_items: [['arrow-left', lambda x: root.back_to_chat()]]
  246.                MDLabel:
  247.                    id: 'Profile_String'
  248.                    font_size: 90
  249.                    text: "XXX"
  250.                    halign: 'center'
  251.                    pos_hint: {'center_x': 0.5, 'center_y': 0.85}
  252.  
  253.            Screen:
  254.                name: 'screen4'
  255.                id: 'screen4'
  256.                
  257.                Toolbar:
  258.                    id: tools
  259.                    title: "XXX"
  260.                    pos_hint: {'center_x': 0.5, 'center_y': 0.96}
  261.                    md_bg_color: app.theme_cls.primary_color
  262.                    background_palette: 'DeepPurple'
  263.                    background_hue: 'A400'
  264.                    left_action_items: [['menu', lambda x: app.root.toggle_nav_drawer() ]]
  265.                    right_action_items: [['animation', lambda x: MDThemePicker().open()], ['camera', lambda x: print('hello')]]
  266.                ScrollView:
  267.                    pos_hint: {'center_x': .55, 'y': .35}
  268.                    MDLabel:
  269.                        id: 'Chat_String'
  270.                        font_size: 40
  271.                        text: "XXX"
  272.                        
  273.  
  274.                MDTextField:
  275.                    id: 'Input_String'
  276.                    hint_text: 'Enter Your Message...'
  277.                    helper_text_mode: 'on_focus'
  278.                    pos_hint: {'center_x': 0.35, 'center_y': 0.2}
  279.                    size_hint_x: 0.6
  280.                    multiline: True
  281.  
  282.  
  283.                MDRaisedButton:
  284.                    id: 'Send_Button'
  285.                    text: 'Send'
  286.                    pos_hint: {'center_x': 0.75, 'center_y': 0.2}
  287.  
  288.                MDRaisedButton:
  289.                    id: 'Choose_Image'
  290.                    text: 'Attach File'
  291.                    pos_hint: {'center_x': 0.9, 'center_y': 0.2}
  292.                    on_release: Factory.UploadPopup().open()
  293. """
  294.  
  295.  
  296. class MDCustomIconItem(OneLineAvatarListItem):
  297.     icon = StringProperty('')
  298.     text = StringProperty()
  299.  
  300.     def _set_active(self, active, list):
  301.         pass
  302.  
  303.  
  304. class AvatarSampleWidget(ILeftBody, Image):
  305.     pass
  306.  
  307.  
  308. class MyApp(App):
  309.     theme_cls = ThemeManager()
  310.     theme_cls.primary_palette = 'Blue'
  311.     title = "Navigation Drawer"
  312.     main_widget = None
  313.  
  314.     def build(self):
  315.         self.main_widget = Builder.load_string(KV)
  316.         return self.main_widget
  317.  
  318.     def callback(self, instance, value):
  319.         self.main_widget.ids.scr_mngr.current = 'screen4'
  320.  
  321.     def recover_data(self):
  322.         print('started')
  323.         while True:
  324.             data = sock.recv(1024)
  325.             data = data.decode()
  326.             if data:
  327.                 print(data)
  328.                 data = data.split()
  329.                 data = data[-1] + ": " + ' '.join(data[:-1])
  330.                 r = data + '\n'
  331.                 open('chat1.txt', 'a+').write(r)
  332.                 e = open('chat1.txt', 'r').readlines()
  333.                 self.main_widget.ids.Chat_String.text = '\n\r'.join(e)#Whenever I received a message, this line throws an AttributError
  334.                 print(data)
  335.  
  336.     def on_start(self):
  337.         Thread(target=self.recover_data).start()
  338.         for i in range(15):
  339.             self.main_widget.ids.nav_drawer.add_widget(
  340.                 MDCustomIconItem(
  341.                     text="Item menu %d" % i,
  342.                     icon='logo.png',
  343.                     on_release=lambda x, y=i: self.callback(x, y)))
  344.  
  345.  
  346. MyApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement