gpt4 book ai didi

python - 属性错误: 'kivy.properties.ObjectProperty' object has no attribute 'text'

转载 作者:太空宇宙 更新时间:2023-11-03 20:46:15 24 4
gpt4 key购买 nike

我正在尝试在 python 中使用套接字和 kivy 制作一个聊天程序。我为客户端编写了一段代码,当服务器发送消息时,获取该消息并更改 TextInput 的文本属性,但出现此错误: AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'

gui.py 文件:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
import client

def RecvMsg(DATA):
# The error occurs in there
ScreenMng.textbox.text += DATA + "\n"

class MainScreen(Screen):
pass

class SecondScreen(Screen):
pass

class ScreenMng(ScreenManager):
textbox = ObjectProperty(None)

def SendMsg(self, DATA):
client.Client_Send(DATA)

def GetIPNAME(self, IP, NAME):
client.Connect(str(IP), str(NAME))

class MyApp(App):
title= "CHAT"
def build(self):
return ScreenMng()

if __name__ == "__main__":
MyApp().run()

my.kv 文件:

#:import FadeTransition kivy.uix.screenmanager.FadeTransition
#:import utils kivy.utils

<Main_Label@Label>
font_size: 0.4 * root.height if root.height < root.width * .85 else 0.32 * root.width
font_name: "Oswald-Medium.ttf"
color: utils.get_color_from_hex("ca3e47")
<Main_TextInput@TextInput>
font_size: 0.65 * root.height if root.height < root.width * .65 else 0.45 * root.width
<Main_Button@Button>
font_size: 0.4 * root.height if root.height < root.width * .85 else 0.32 * root.width

<Button>
font_name: "Oswald-Medium.ttf"
<Label>
font_name: "Oswald-Medium.ttf"

<FillLabel@Label>

<ScreenMng>
textbox: textbox
transition: FadeTransition()
canvas:
Color:
rgb: utils.get_color_from_hex("313131")
Rectangle:
size: root.size
pos: root.pos
MainScreen:
GridLayout:
cols: 1
GridLayout:
orientation: "right"
cols: 2
Main_Label:
text: "IP:"

GridLayout:
padding: 20,0
rows: 3

FillLabel:
Main_TextInput:
id: ip
multiline: False
FillLabel:

Main_Label:
text: "NAME:"

GridLayout:
padding: 20,0
rows: 3
FillLabel:
Main_TextInput:
id: name
multiline: False
FillLabel:

Main_Button:
on_press:
root.GetIPNAME(ip.text, name.text)
root.current = "second"
font_size: 0.3 * root.height if root.height < root.width else 0.3 * root.width
text: "Submit"

SecondScreen:
name: "second"
canvas:
Color:
rgb: utils.get_color_from_hex("313131")
Rectangle:
size: root.size
pos: root.pos

GridLayout:
spacing: 30
padding: 30,20
rows: 2
GridLayout:
spacing: 20
cols: 2
TextInput:
id: textbox
text: ""
readonly: True

TextInput:
text: "-ONLINE USERS-"
do_scroll_x: False
readonly: True
copy: False
size_hint_x: None
size: 200, 0

GridLayout:
cols: 2
size_hint_y: None
size: 0, 50
spacing: 10

TextInput:
id: message
size_hint_y: None
size: 0, 50
multiline: False

Main_Button:
size_hint: None, None
size: 150, 50
text: "SEND"
on_press:
root.SendMsg(message.text)

最佳答案

我认为你只需要改变:

ScreenMng.textbox.text += DATA + "\n"

至:

App.get_running_app().root.textbox.text += DATA + "\n"

ScreenMng 类的 textbox 属性必须作为实例属性进行引用。将其用作 ScreenMng.textbox 只是对 ObjeectProperty 本身的引用。

关于python - 属性错误: 'kivy.properties.ObjectProperty' object has no attribute 'text' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56584782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com