Python – Kivy: 标签在函数执行期间不更新

huangapple go评论74阅读模式
英文:

Python - Kivy: Label does not update during a function excution

问题

我看到还有其他与这个主题相关的话题,但我可以让这个简单的项目工作。
我需要在函数运行时更新一个标签以显示函数的状态,只是为了让用户知道程序仍在运行,而标签只在函数完成时更新。

以下是文件

my.kv

WindowManager:
    ChangeLabel:
<ChangeLabel>:
    BoxLayout:
        orientation: 'vertical'
        padding: 200, 20
        Label: 
            id: labeltxt
            text: 'Text to change'
            font_size: 30           
            height: 50
        Button:
            text:'Run Function'
            size_hint_y: None
            height: 30
            on_release:
                root.changeLabel()
        Label: 

test.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.core.window import Window
import time
from kivy.clock import Clock

class ChangeLabel(Screen):
    def changeLabel(self):
        Clock.schedule_once(self.test)

    def test(self, dt):
        self.ids.labeltxt.text = 'Function is running...' #在函数运行时更新此标签
        time.sleep(2) #运行函数
        self.ids.labeltxt.text = 'Completed'


class WindowManager (ScreenManager):
    pass

kv = Builder.load_file('my.kv')
class MyApp(App):
    def build(self):
        return kv

MyApp().run()
英文:

I saw that there are other topics related to this one but I could make this simple project to work.
I need a label to be updated with the function status while it is running just to let the user know that the program still working while the function is processing, but the label is only updated when the function is completed

Here are the files

my.kv

WindowManager:
    ChangeLabel:
&lt;ChangeLabel&gt;:
    BoxLayout:
        orientation: &#39;vertical&#39;
        padding: 200, 20
        Label: 
            id: labeltxt
            text: &#39;Text to change&#39;
            font_size: 30           
            height: 50
        Button:
            text:&#39;Run Function&#39;
            size_hint_y: None
            height: 30
            on_release:
                root.changeLabel()
        Label: 

test.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.core.window import Window
import time
from kivy.clock import Clock

class ChangeLabel(Screen):
    def changeLabel(self):
        Clock.schedule_once(self.test)

    def test(self, dt):
        self.ids.labeltxt.text = &#39;Function is running...&#39;#I wanna this in lablel while function is running
        time.sleep(2)# function to run
        self.ids.labeltxt.text = &#39;Completed&#39;


class WindowManager (ScreenManager):
    pass

kv = Builder.load_file(&#39;my.kv&#39;)
class MyApp(App):
    def build(self):
        return kv

MyApp().run()

答案1

得分: 1

I did this solution, now it is working the way I want, but not sure if this is the best way.

test.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.core.window import Window
import time
from kivy.clock import Clock

class ChangeLabel(Screen):
    i = 0
    def changeLabel(self):
        Clock.schedule_interval(self.test, 0.05)

    def test(self, dt):
        if self.i == 0:
            self.ids.labeltxt.text = 'Function is running...'
            self.i = 1
        else:
            time.sleep(2)# function to run
            self.ids.labeltxt.text = 'Completed'
            self.i = 0
            Clock.unschedule(self.test)

class WindowManager (ScreenManager):
    pass

kv = Builder.load_file('my.kv')
class MyApp(App):
    def build(self):
        return kv

MyApp().run()

(Note: I've removed the HTML entities &#39; and provided the Python code as requested.)

英文:

I did this solution, now it is working the way I want, but not sure if this is the best way.

test.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.core.window import Window
import time
from kivy.clock import Clock

class ChangeLabel(Screen):
    i = 0
    def changeLabel(self):
        Clock.schedule_interval(self.test, 0.05)

    def test(self, dt):
        if self.i == 0:
            self.ids.labeltxt.text = &#39;Function is running...&#39;
            self.i = 1
        else:
            time.sleep(2)# function to run
            self.ids.labeltxt.text = &#39;Completed&#39;
            self.i = 0
            Clock.unschedule(self.test)

class WindowManager (ScreenManager):
    pass

kv = Builder.load_file(&#39;my.kv&#39;)
class MyApp(App):
    def build(self):
        return kv

MyApp().run()

huangapple
  • 本文由 发表于 2020年1月7日 01:48:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定