Kivy泡泡箭头模糊

时间:2016-05-26 09:00:30

标签: python python-3.x kivy

我在自定义小部件周围创建了一个Bubble小部件来显示一些选项,但Bubble的箭头出乎意料地大而模糊。所有这些小部件都放在GridLayout内,放在ScrollView内。什么可能导致这个问题?

以下是代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.gridlayout import GridLayout
from kivy.uix.bubble import Bubble
from kivy.properties import ListProperty

Builder.load_string('''
<PopupBubble>:
    size_hint: None, None

<ScrollView>:
    canvas:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

<Message>:
    width: 500
    BoxLayout:
        pos: root.pos
        height: self.height
        canvas:
            Color:
                rgba: 0.8, 0.8, 0.8, 1
            RoundedRectangle:
                pos: root.pos
                size: self.size

        TextInput:
            pos: root.pos
            size: root.size
            id: msg
            background_color: 0, 0, 0, 0
            cursor_color: 0, 0, 0, 0

    PopupBubble:
        pos: root.x, self.height + root.y + root.height
        size: 100, 40
''')

class Message(Widget):
    bg_color = ListProperty([0.99, 0.99, 0.99, 1])

class PopupBubble(Bubble):
    pass

class Chat(Screen):
    pass

class TestApp(App):
    def msg_in(self):
        msg_stack = self.msg_stack
        msg = "test"
        msg_stack.append(Message())

        msg_stack[-1].ids['msg'].text = msg
        msg_stack[-1].width = 160
        msg_stack[-1].height = (len(msg_stack[-1].ids['msg']._lines_labels) + 1) * (msg_stack[-1].ids['msg'].line_height + 5)
        for i in msg_stack[-1].walk():
            i.height = msg_stack[-1].height
            i.width = msg_stack[-1].width
        self.msg_layout.add_widget(msg_stack[-1])

    def build(self):
        self.msg_stack = []
        self.chat = Chat()
        self.sv1_main = ScrollView(pos_hint = {"top":1, "center_x":0.5},
                                   size_hint = (0.97, 0.65))

        self.msg_layout = GridLayout(height = 10,
                                     cols = 1,
                                     padding = 10,
                                     spacing = 10,
                                     size_hint_y = None)
        self.chat.add_widget(self.sv1_main)
        self.sv1_main.add_widget(self.msg_layout)
        for i in range(15):
            self.msg_layout.add_widget(Widget())
            # Just to bring the next widget down

        self.msg_in()
        return self.chat

TestApp().run()

1 个答案:

答案 0 :(得分:1)

你的专栏msg_stack[-1].width = 160瘫痪了。或者更确切地说是在它之后的循环,您可以从该变量设置Bubble的宽度。

你在kv中设置了Bubble size,然后在python中,当你将width作为一个小部件添加到Message时,你再次播放GridLayout并且结果显而易见。设置其大小一次,以kv或python和/或尝试仅设置一个属性。否则它按预期工作:

from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.bubble import Bubble, BubbleButton
Builder.load_string('''
<Test>:
    Button:
        on_release: root.show_bubble()
    FloatLayout:
        id: box
''')
class Test(BoxLayout):
    def show_bubble(self):
        bubble = Bubble(size_hint=[None,None],size=[100,40],pos=[100,300])
        bubble.add_widget(BubbleButton(text='Copy', size=[30,10]))
        bubble.add_widget(BubbleButton(text='Paste'))
        self.ids.box.add_widget(bubble)
runTouchApp(Test())