【问题标题】:How to display text or Popup in kivy while python sript is runningpython脚本运行时如何在kivy中显示文本或弹出窗口
【发布时间】:2021-11-24 18:55:21
【问题描述】:

大家好,阅读本主题。

我想做的是:在 if 语句中的值匹配后自动在标签中显示文本。 第二个选项可以是 Popup insted Label。 第三种方式可以跳转到不同的屏幕。

请帮帮我, 我真的不知道如何在不使用任何按钮的情况下将其与 kivy 连接。 以下是代码:

蟒蛇:

from kivy.app import App
import random
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

class Main(ScreenManager):
    pass

class Numbers(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)


    def process(self):

        num_list = ['four','three']
        number = random.choice(num_list)
        self.number.text = number
        print(number)
        while True:

            if number == 'null':
                pass
            elif number == 'three':
                pass
            elif number == 'four':
                self.root.ids.numb.text="four"
                print(' for example Here i need connect code with kivy to display on screen text as string or jump to another screen')
                break
            else:
                pass
kv = Builder.load_file('temp.kv')


class NumbersApp(App):
    def build(self):
        return kv

if __name__ == '__main__':
    NumbersApp().run()

和kv文件:

Main:
    Numbers:
<Numbers>:
    number:numb
    cols:1
    RelativeLayout:
        Label:
            id: numb
            text: 'Text display after getting \ninfo from script in while loop \nin process def'
            font_size: '15'
            hint_size: None, None
            pos_hint: {'x': 0.04, 'y': -0.4}
            color: 1,1,1,1

【问题讨论】:

  • 在你的 elif 语句下添加这个 self.root.ids.numb.text="four"
  • 它仍然无法正常工作,标签上的文字保持不变。出于某种原因,我什至没有在名为“数字”的控制台中打印出字符串。

标签: python-3.x kivy kivy-language


【解决方案1】:

您的线路:

self.root.ids.numb.text="four"

不正确,因为numb idNumbers 对象中,但您正试图访问Numbers 对象的root 属性。只需将该行更改为:

self.ids.numb.text = "four"

访问正确的ids

所以这里是您的kv 的修改版本,其中包括一个Button 以启动process()

Main:
    Numbers:
<Numbers>:
    number:numb
    cols:1
    RelativeLayout:
        Label:
            id: numb
            text: 'Text display after getting \\ninfo from script in while loop \\nin process def'
            font_size: '15'
            hint_size: None, None
            pos_hint: {'x': 0.04, 'y': -0.4}
            color: 1,1,1,1
        Button:
            text: 'Start Process'
            size_hint: None, None
            size: self.texture_size
            pos_hint: {'center_x': 0.5, 'top': 1}
            on_release: root.start_process()

这是您的 Numbers 类的修改版本,它使用另一个 ThreadClock.schedule_once()

class Numbers(Screen):
    def start_process(self):
        # run process() on another thread
        threading.Thread(target=self.process, daemon=True).start()

    def process(self):
        print('process')
        num_list = ['four','three']
        number = random.choice(num_list)
        print(number)
        while True:

            if number == 'null':
                pass
            elif number == 'three':
                pass
            elif number == 'four':
                print(' for example Here i need connect code with kivy to display on screen text as string or jump to another screen')
                Clock.schedule_once(self.set_text)  # set text on the main thread
                break
            else:
                pass

    def set_text(self, dt):
        self.ids.numb.text = "four"

【讨论】:

  • 它现在没有任何错误,但只有我得到的是在条件正确后在控制台中输出,但我在 kivy gui 中没有得到任何更新。
  • 你是如何启动process() 方法的?
  • 这很好!我添加按钮来启动一个功能,现在一切正常。感谢您的快速帮助,谢谢。
  • 当我使用中断功能时,文本会在 gui 中更新。事情是在该过程功能中断之后,我需要再次按下按钮。有什么方法可以自动化这个过程以保持在while循环中?基本上我想在没有中断功能的while循环中运行时,每次都在改变时自行更新Label。
  • 如果您通过按Button 运行process(),那么您将在主线程上运行它。 Kivy 使用主线程来更新 GUI。因此,只要您在主线程上运行某些东西,GUI 就无法更新,直到 process() 完成。我建议在另一个线程中运行process(),并使用Clock.schedule_once() 安排调用另一个执行self.ids.numb.text = "four" 的方法。
猜你喜欢
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
相关资源
最近更新 更多