带有kivy-recv线的插座不能正常工作

时间:2017-04-25 11:49:53

标签: kivy

我做了一个与kivy一起工作的应用程序发送" name"对于服务器(带有套接字),服务器插入" name"到数据库并发回"名称"到客户端(应用程序),客户端打印它。

我没有收到任何错误,但该应用无法正常工作。它没有开放。

我的客户(应用):

# kivy.require("1.8.0")
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout  
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.graphics import Line
from kivy.uix.button import Button
import socket



Builder.load_string("""
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Sign Up'
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.current = 'settings'
        Button:
            text: 'Login'
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.current = 'settings'

<Searchi>:
    BoxLayout:
        Button:
            text: 'send massage'
            font_size: '20sp'
            size_hint: (0.4,0.111)
            pos_hint: {'y': 0,'x':0.6}
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'settings'




<SettingsScreen>:
    BoxLayout:
        FloatLayout:
            LoginScreen
            Button:
                font_size: '20sp'
                size_hint: (0.4,0.111)
                pos_hint: {'y': 0,'x':0.6}
                text: 'Back to menu'
                on_press:
                     root.manager.transition.direction = 'right'
                     root.manager.current = 'menu'
""")

class LoginScreen(GridLayout):
    def __init__(self, **kwargs):
        super(LoginScreen, self).__init__(**kwargs)
        self.cols = 2
        self.my_socket = socket.socket()
        #host = socket.gethostname()
        #port = 8585
        self.my_socket.connect(('127.0.0.1', 8081))

        self.add_widget(Label(text='username'))
        self.username = TextInput(multiline=False)
        self.add_widget(self.username)

        self.add_widget(Label(text='Password'))
        self.password = TextInput(multiline=False, password=True)
        self.add_widget(self.password)

        self.submit_button = Button(text='sumbit',size_hint=(.5, 
.25),font_size=20)
        self.submit_button.bind(on_press=self.submit_username)
        self.add_widget(self.submit_button)

    def submit_username(self, *args):
         # Make sure to validate the input before submitting to the server
        self.my_socket.send(self.username.text.encode('utf-8'))
        sm.current = 'searchi'
        sm.transition.direction = 'left'

# Declare both screens
class MenuScreen(Screen):
    pass

class SettingsScreen(Screen):
    pass

class Searchi(Screen):
    def __init__(self, **kwargs):
        super(Searchi, self).__init__(**kwargs)
        self.my_socket = socket.socket()
        self.my_socket.connect(('127.0.0.1', 8081))
        self.msg=self.my_socket.recv(1024)
        self.name=self.msg.decode('utf-8')


        self.add_widget(Label(text="hello"+self.name,size_hint=(1, 
1.7),font_size=20))


# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(Searchi(name='searchi'))
sm.add_widget(SettingsScreen(name='settings'))

class TestApp(App):

    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

我的服务器:

import socket
import db      # import my db
import time


server_socket = socket.socket()
server_socket.bind(('127.0.0.1',8081))
server_socket.listen(5)

(client_socket, client_address) = server_socket.accept()
while(True):
    client_username = client_socket.recv(1024)
    msg=client_username.decode('utf-8')
    db.data_entry_login(msg,"dsfsds","dsfsfsdd","2")  #insert to my db
    msg=msg+"\r\n"
    client_socket.send(msg.text.encode('utf-8'))

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

问题在于您提到了recv方法 该方法将整个主线程置于保持状态,因此应用程序不会运行 无论何时等待或在主线程中循环,应用程序都将停止更新。并且recv正在等待消息 所以你需要将recv放在一个帖子中。

你可以这样做:

import threading

class Searchi(Screen):
    def __init__(self, **kwargs):
        super(Searchi, self).__init__(**kwargs)
        self.hello_label = Label(text="Connecting...",size_hint=(1,1.7),font_size=20)
        threading.Thread(target=self.recv_msg).start()

    def recv_msg(self):
        self.my_socket = socket.socket()
        self.my_socket.connect(('127.0.0.1', 8081))
        self.msg=self.my_socket.recv(1024)
        self.name=self.msg.decode('utf-8')
        self.hello_label.text = "hello"+self.name