我用kivy做了一个应用程序,它在一个名为LoginScreen
的类中向服务器发送数据,并从另一个名为Searchi
的类中的服务器接收数据。
它不起作用,我真的不知道是什么问题而且我没有任何错误。
我的客户:
# 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
import threading
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.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):
send_data(self.username.text)
sm.current = 'searchi'
sm.transition.direction = 'left'
def send_data(name):
my_socket = socket.socket()
my_socket.connect(('127.0.0.1', 8088))
my_socket.send(name.encode('utf-8'))
my_socket.close()
def recv_data():
my_socket = socket.socket()
my_socket.connect(('127.0.0.1', 8085))
name=my_socket.recv(1024)
msg = name.decode('utf-8')
my_socket.close()
return msg
# Declare both screens
class MenuScreen(Screen):
pass
class SettingsScreen(Screen):
pass
class Searchi(Screen):
def __init__(self, **kwargs):
super(Searchi, self).__init__(**kwargs)
self.msg=threading.Thread(target=recv_data).start()
self.add_widget(self.msg)
# 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 threading
server_socket = socket.socket()
server_socket.bind(('127.0.0.1',8088))
server_socket.listen(5)
oldmsg = ""
while(True):
(client_socket, client_address) = server_socket.accept()
client_username = client_socket.recv(1024)
msg = client_username.decode('utf-8')
print(msg)
if msg != oldmsg:
db.data_entry_login(msg, "dsfsds", "dsfsfsdd", "2") # insert to my db
oldmsg=msg
client_socket.send(msg.encode('utf-8'))