Ich brauche eure Hilfe bei der Frage, wie ich in meinem verkürzt gezeigten Code, einen vom Server zurückgesendeten String (answer) auf der Python-Seite app_client.py an ein Label (id: input) auf der
Kivy-Seite app_client.kv übergeben kann. Zur verkürzten Darstellung habe ich die Klasse Communication weggelassen.
Hier der Code app_client.py:
Code: Alles auswählen
class FirstScreen(Screen):
def get_communication(self):
if communication.get_ip_address() is True:
self.parent.current = "second"
communication.get_username(self.ids.nickname.text)
receive_thread = threading.Thread(target=communication.receive)
receive_thread.start()
write_thread = threading.Thread(target=communication.write)
write_thread.start()
else:
PopupWindow().open()
class SecondScreen(Screen):
def receive(self, answer):
print(answer)
# self.ids.input.text = answer # ???
def write(self):
communication.write(self.ids.output.text)
@staticmethod
def close():
global connection
connection = False
communication.close()
sys.exit()
class ScreenManagement(ScreenManager):
"""
def __init__(self, *args, **kwargs):
super(ScreenManagement, self).__init__(*args, **kwargs)
@mainthread
def delayed():
first_screen = self.get_screen('first')
second_screen = self.get_screen('second')
delayed()
"""
pass
class ClientApp(App):
def build(self):
return Builder.load_file("app_client.kv")
class PopupWindow(Popup):
pass
if __name__ == "__main__":
communication = Communication()
app = ClientApp()
app.run()
Code: Alles auswählen
ScreenManagement:
FirstScreen:
name: 'first'
SecondScreen:
name: 'second'
<FirstScreen>
name: "first"
BoxLayout:
orientation: 'vertical'
padding: 25
BoxLayout:
orientation: 'horizontal'
size_hint: 1, .3
Button:
text: '<'
size_hint: .1, 1
font_size: 75
# background_normal: ""
background_color: 1, .5, .92, 1
on_release: app.root.current = root.close()
Label:
text: 'CHOOSE USERNAME'
font_size: 50
canvas.before:
Color:
rgba: 0.18, .5, .92, 1
Rectangle:
pos: self.pos
size: self.size
Widget:
size_hint: .1, 1
canvas.before:
Color:
rgba: 1, .5, .92, 1
Rectangle:
pos: self.pos
size: self.size
GridLayout:
cols: 2
rows: 1
size_hint_y: None
height: 80
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: 'Username:'
font_size: 40
size_hint: .3, 1
color: 0.18, .5, .92, 1
pos_hint: {'center': 1}
TextInput:
id: nickname
text: "DMD"
size_hint: .6, 1
font_size: 50
multiline: False
Button:
text: '>> Join In'
font_size: 35
size_hint: 1, .3
# background_normal: ""
background_color: 0.18, .5, .92, 1
on_release: root.get_communication()
Label:
text: 'Support'
color: 0.18, .5, .92, 1
halign: 'left'
font_size: 25
size_hint: 1, .3
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
<SecondScreen>
name: "second"
output: output
input: input
BoxLayout:
orientation: 'vertical'
padding: 25
Label:
text: "CHAT ROOM"
font_size: 50
TextInput:
id: output
text: "Your message here..."
font_size: 25
multiline: False
Label:
id: input # hier versuche ich die Serverantwort "answer" zu übergeben
text: "Hello world!"
font_size: 25
Button:
text: ">> Send"
font_size: 35
on_release: root.write()
Button:
text: ">> Close"
font_size: 35
on_release: app.root.current = root.close()
