wim*_*rks 5 python properties python-3.x kivy kivy-language
目标
我有一个带有属性的小部件类c_description。我需要能够创建标签(或其他类型的小部件)并将其添加到c_description作为文本继承的小部件。并将更改c_description传播到标签的文本。我需要能够使用函数来运行此标签的创建/添加。
我基本上需要完全按照另一个问题的
要求去做。
我做了什么以及遇到了什么问题我
格式化了这个标签类StretchingLabel拉伸以适合其内容。我有一个kivy 脚本,它显示了我希望它如何工作。最终结果应该是这样的。

这是另一个脚本,在将其text属性绑定到c_description.
如果我运行它,我会得到如下所示的结果。

标签的文本是“”而不是 的内容c_description,所以这是一个问题。但是,如果我删除属性绑定语句并更改c_label = StretchingLabel()为c_label = StretchingLabel(pos=self.pos, width=self.width, text=self.c_description),我们至少应该能够看到成功的属性绑定是什么样子的。
当我这样做时,结果看起来像这样。
这不是我想要的。我希望它看起来像第一张图片。
我的代码
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.uix.textinput import TextInput
Builder.load_string('''
<StretchingLabel>:
size_hint_y: None
text_size: self.width, None
height: self.texture_size[1]
group: 'test'
canvas.before:
Color:
rgba: .7, .7, .7, 1
Rectangle:
pos: self.pos
size: self.size
<MyLabelFrame>:
id: xLabel
<ContainerBox>:
orientation: 'horizontal'
Button:
text: 'h1'
group: 'test'
BoxLayout:
orientation: 'vertical'
size: root.size
pos: root.pos
Label:
text: 'Description'
size_hint_y: None
height: 30
bold: True
MyLabelFrame:
Label:
''')
class StretchingLabel(Label):
def __init__(self, **kwargs):
super(StretchingLabel, self).__init__(**kwargs)
#This is for debugging
Clock.schedule_once(lambda dt: print("StretchingLabel.init(): ", self.text), timeout=0.01)
def on_double_click(self, instance, p_ignoreme):
#This is also for debugging
print("StretchingLabel.on_double_click():", self.text)
class MyLabelFrame(Widget):
c_description = StringProperty(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n\nProin vitae turpis ornare urna elementum pharetra non et tortor. Curabitur semper mattis viverra. \nPellentesque et lobortis purus, eu ultricies est. Nulla varius ac dolor quis mattis. Pellentesque vel accumsan tellus. Donec a nunc urna. Nulla convallis dignissim leo, tempor sagittis orci sollicitudin aliquet. Duis efficitur ex vel auctor ultricies. Etiam feugiat hendrerit mauris suscipit gravida. Quisque lobortis vitae ligula eget tristique. Nullam a nulla id enim finibus elementum eu sit amet elit.')
def __init__(self, **kwargs):
super(MyLabelFrame, self).__init__(**kwargs)
Clock.schedule_once(lambda dt: self.makeLabel(), timeout=0.01)
def makeLabel(self):
c_label = StretchingLabel()
#HERE! This vvv does not seem to work for some reason.
self.bind(pos=c_label.setter('pos'), width=c_label.setter('width'), c_description=c_label.setter('text'))
#This vvv didn't work either.
#c_label.bind(pos=self.setter('pos'), width=self.setter('width'), text=self.setter('c_description'))
self.add_widget(c_label)
class ContainerBox(BoxLayout):
def __init__(self, **kwargs):
super(ContainerBox, self).__init__(**kwargs)
class Nested2App(App):
def build(self):
return ContainerBox()
if __name__ == '__main__':
Nested2App().run()
Run Code Online (Sandbox Code Playgroud)
我向你提出的问题
self.bind(c_description=c_label.setter('text'))?正在创建标签,但未从 获取文本c_description。我究竟做错了什么?makeLabel(). 我需要能够使用该makeLabel()功能正确地完成此操作。笔记
我在这里看到另一个问题,它与我遇到的问题完全相同(没有格式问题),但由于某种原因,答案对我不起作用。
为什么 self.bind(c_description=c_label.setter('text')) 不起作用?正在创建标签,但未从 c_description 获取文本。我究竟做错了什么?
您已获得正确的代码,
self.bind(pos=c_label.setter('pos'), width=c_label.setter('width'), c_description=c_label.setter('text'))
Run Code Online (Sandbox Code Playgroud)
绑定过程不会立即更新text. text只有改变时才会改变c_description。
以下示例说明了以下内容:
c_descripton更改后显示,但以小部件的默认pos=[0,0]值显示width=100c_description显示在正确的位置,因为pos和width已更改。添加color: 0, 0, 0, 1 # black colour text到类规则中,<StretchingLabel>:因为文本不可见。这是因为背景颜色是白色,并且标签文本的默认颜色也是白色。
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
Builder.load_string('''
<StretchingLabel>:
color: 0, 0, 0, 1 # black color text
size_hint_y: None
text_size: self.width, None
height: self.texture_size[1]
group: 'test'
canvas.before:
Color:
rgba: .7, .7, .7, 1
Rectangle:
pos: self.pos
size: self.size
<MyLabelFrame>:
id: xLabel
<ContainerBox>:
orientation: 'horizontal'
Button:
text: 'h1'
group: 'test'
BoxLayout:
orientation: 'vertical'
size: root.size
pos: root.pos
Label:
text: 'Description'
size_hint_y: None
height: 30
bold: True
MyLabelFrame:
Label:
''')
class StretchingLabel(Label):
def __init__(self, **kwargs):
super(StretchingLabel, self).__init__(**kwargs)
# This is for debugging
Clock.schedule_once(lambda dt: print("StretchingLabel.init(): ", self.text), timeout=0.01)
def on_double_click(self, instance, p_ignoreme):
# This is also for debugging
print("StretchingLabel.on_double_click():", self.text)
class MyLabelFrame(Widget):
c_description = StringProperty(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n\nProin vitae turpis ornare urna elementum pharetra non et tortor. Curabitur semper mattis viverra. \nPellentesque et lobortis purus, eu ultricies est. Nulla varius ac dolor quis mattis. Pellentesque vel accumsan tellus. Donec a nunc urna. Nulla convallis dignissim leo, tempor sagittis orci sollicitudin aliquet. Duis efficitur ex vel auctor ultricies. Etiam feugiat hendrerit mauris suscipit gravida. Quisque lobortis vitae ligula eget tristique. Nullam a nulla id enim finibus elementum eu sit amet elit.')
def __init__(self, **kwargs):
super(MyLabelFrame, self).__init__(**kwargs)
Clock.schedule_once(lambda dt: self.makeLabel(), timeout=0.8)
def makeLabel(self):
c_label = StretchingLabel()
self.bind(pos=c_label.setter('pos'), width=c_label.setter('width'), c_description=c_label.setter('text'))
self.add_widget(c_label)
Clock.schedule_once(lambda dt: self.chg_text(), 1)
def chg_text(self):
self.c_description = 'Updated: ...' + self.c_description
class ContainerBox(BoxLayout):
def __init__(self, **kwargs):
super(ContainerBox, self).__init__(**kwargs)
class Nested2App(App):
def build(self):
return ContainerBox()
if __name__ == '__main__':
Nested2App().run()
Run Code Online (Sandbox Code Playgroud)
为什么创建的标签格式错误?我在 kv 代码中传递了与 makeLabel() 中相同的属性。我需要能够使用 makeLabel() 函数正确地完成此操作。
问题是由于 Kivy 的造型尚未完成。
将timeout值增加到至少 0.8 秒。该值会有所不同,即取决于计算机的速度。
def __init__(self, **kwargs):
super(MyLabelFrame, self).__init__(**kwargs)
Clock.schedule_once(lambda dt: self.makeLabel(), timeout=0.8)
def makeLabel(self):
c_label = StretchingLabel(pos=self.pos, width=self.width, text=self.c_description)
self.add_widget(c_label)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
605 次 |
| 最近记录: |