简体   繁体   English

Kivy异步问题-滚动视图后画布无法更新

[英]Kivy asynchronous issue - canvas not get updated after scroll view

I have this piece of code in kivy, after pressing a button, get_liner() function get called. 我有一段代码,按下按钮后,就会调用get_liner()函数。 get_liner() then calls get_canvas(). get_liner()然后调用get_canvas()。 I want the canvas to be updated with a color background after the text in the scrollview gets updated. 我希望在滚动视图中的文本更新后用彩色背景更新画布。 But seems like there is some asynchronous issue that doesn't let the code run in sequence. 但是似乎有些异步问题不允许代码按顺序运行。 But if I press the button the 2nd time, the canvas gets updated after text. 但是,如果我第二次按下按钮,则在文本之后画布会更新。 How can I make this code work? 如何使此代码起作用?

myapp.py myapp.py

def get_liner(self):
    <some code here>
    mythread2 = threading.Thread(target=partial(self.get_canvas),kwargs={'outputtext':outputtext})
    mythread2.start()

def get_canvas(self, outputtext=None):
    self.ids.rst_doc.text = outputtext
    secondhalfview = self.ids.rst_doc
    secondhalfview.canvas.before.clear()
    with secondhalfview.canvas.before:
        Color(1,1,0,1)
        Rectangle(pos=secondhalfview.pos, size=secondhalfview.size)
    self.dismiss_popup()

myapp.kv myapp.kv

Root:
    BoxLayout:
        orientation: 'vertical'
        size: root.size
        pos: root.pos
        BoxLayout:
            id: twohalfviews
            orientation: 'horizontal'          
            size: root.size
            pos: root.pos
            BoxLayout:
                id: secondhalfview
                size_hint_x: 0.7
                ScrollView:
                    id: scrlv
                    size_hint_x: 0.8
                    pos_hint: {'center_x':.5, 'center_y': .5}
                    Label:
                        id: rst_doc
                        text: ' '
                        text_size: self.width,None
                        font_size: '12sp'
                        size_hint_y: None
                        height: self.texture_size[1]
                        color: [1,0,0,1] #red
                        font_name: 'monaco.ttf'

I finally figured out the issue. 我终于弄清楚了这个问题。 It's not async issue, but rather a drawing issue. 这不是异步问题,而是绘图问题。 In my old code, the canvas is cleared, then I redraw Rectangle object and Color object which screws up the drawing sequence. 在我的旧代码中,画布被清除,然后我重新绘制Rectangle对象和Color对象,这会破坏绘制顺序。

Here is the new code where I don't clear the canvas anymore, and clear the rst document instead then update the Color object alone: 这是新的代码,这里我不再清除画布,而是清除第一个文档,然后单独更新Color对象:

def new_get_canvas(self, outputtext=None):
    self.ids.rst_doc.text = outputtext
    rst_doc = self.ids.rst_doc
    rst_doc.canvas.before.clear()
    self.ids.secondhalfview.canvas.before.children.insert(
        0,Color(211/255.0, 211/255.0, 211/255.0, 0.9))
    self.dismiss_popup()

Debug Image: 调试映像:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM