kivy:如何在不挂屏的情况下切换屏幕时运行循环功能?

茱莉亚

我想要实现的是:当屏幕从 ScreenOne 切换到 ScreenTwo 时,运行“while 循环”功能,直到按下 ScreenTwo 上的按钮并中断循环。

此函数应该运行并接收来自连接到我的计算机的条形码扫描仪的输入(意思是,输入是条形码)并使用扫描的条形码数量更新 ScreenTwo 上的标签。

然后一旦我没有更多的条形码要扫描,按 ScreenTwo 上的“完成”按钮 - 这应该发送输入“999”以打破循环功能。

我如何在屏幕切换时尝试运行函数:使用“on_enter”

class ScreenTwo(Screen):
    def on_enter(self):
        getStatus()
        updatePoints()

我面临的问题:

  1. 屏幕从 ScreenOne 切换到 ScreenTwo,该功能运行(我看到它发生在 Mac 终端上)无法按下 ScreenTwo 上的按钮(Mac 色轮旋转)。
  2. 而且我还没有弄清楚如何让“完成”按钮向函数发送输入“999”以打破循环。

我如何解决1?

我如何实现 2?

下面分别是 ScreenOne 和 ScreenTwo 的截图: ScreenOne 的屏幕截图 屏幕二截图


这是 returnStation2.py 文件

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty


def getStatus():
    while True:
        answer = input('What is the box ID? ')
        if answer == 999: #LOOPS BREAK WHEN INPUT IS 999
            break
        elif type(answer) == int:
            do something
        else:
            print('Sorry I did not get that')

def updatePoints():
    do something

class ScreenManagement(ScreenManager):
    screen_one = ObjectProperty(None)
    screen_two = ObjectProperty(None)

class ScreenOne(Screen):
    member_status = ObjectProperty(None)

    def backspace(self, textString):
        newTextString = textString[0:-1]
        self.display.text = newTextString

    def getPoints(self, phoneNumber):
        self.manager.screen_two.member_status.text = phoneNumber

class ScreenTwo(Screen):
    input_text = ObjectProperty(None)

    def on_enter(self):
        getStatus()
        updatePoints()

    def clearField(self):
        self.manager.screen_one.input_text.text = ""

class ReturnStationLayout2App(App):

    def build(self):
        return ScreenManagement()


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

这是 returnStationLayout2.kv

“完成”按钮(在 ScreenTwo 中)位于脚本的底部。

屏幕切换到 ScreenTwo 时无法按下。我希望按下时,它可以输入'999'来中断正在运行的循环函数。

<ScreenManagement>:
    screen_one: screen_one
    screen_two: screen_two

    ScreenOne:
        id: screen_one
        name: 'menu'
    ScreenTwo:
        id: screen_two
        name: 'settings'

<CustButton@Button>:
    font_size: 32

<ScreenOne>:
    input_text : entry
    GridLayout:
        id: numberPad
        rows: 5
        padding: [300,200]
        spacing: 10

        # Where input is displayed
        BoxLayout:
            Label:
                text: "+65"
                font_size: 50
                size_hint: 0.2, 1
            TextInput:
                id: entry
                font_size: 50
                multiline: False
                padding: [20, ( self.height - self.line_height ) / 2]


        BoxLayout:
            spacing: 10
            CustButton:
                text: "1"
                on_press: entry.text += self.text
            CustButton:
                text: "2"
                on_press: entry.text += self.text
            CustButton:
                text: "3"
                on_press: entry.text += self.text
            CustButton:
                text: "DEL"
                on_press: root.backspace(entry.text)

        BoxLayout:
            spacing: 10
            CustButton:
                text: "4"
                on_press: entry.text += self.text
            CustButton:
                text: "5"
                on_press: entry.text += self.text
            CustButton:
                text: "6"
                on_press: entry.text += self.text
            CustButton:
                text: "AC"
                on_press: entry.text = ""

        BoxLayout:
            spacing: 10
            CustButton:
                text: "7"
                on_press: entry.text += self.text
            CustButton:
                text: "8"
                on_press: entry.text += self.text
            CustButton:
                text: "9"
                on_press: entry.text += self.text
            CustButton:
                text: "Enter" #HERE IS THE ENTER BUTTON
                on_press:
                    root.manager.transition.direction = 'left'
                    root.manager.transition.duration = 1
                    root.manager.current = 'settings'
                    root.getPoints(entry.text)

        BoxLayout:
            spacing: 10
            Label:
                text: ""
            CustButton:
                text: "0"
                on_press: entry.text += self.text
            Label:
                text: ""
            Label:
                text: ""

<ScreenTwo>:
    member_status: memberStatus
    BoxLayout:
        Label:
            id: memberStatus
            text: ''  
        GridLayout:
            rows: 3
            padding: [100,500]
            spacing: 10
            BoxLayout:
                Label:
                    text: "You have scanned:"
            BoxLayout:
                CustButton:
                    text: "Done" #THIS IS THE BUTTON I HOPE TO BE ABLE TO BREAK THE LOOP FUNCTION
                    on_press:
                        root.manager.transition.direction = "right"
                        root.manager.current = 'menu'
                        root.clearField()
卡佩尔·弗洛里安斯基

解决方案

This answer is based on discussion in comments section present under the question. The code below is written under an assumption that the scanner sends a specific signal when a barcode is scanned. The overall idea is to run a function after that signal was sent.

Clock cycle

我建议熟悉 kivy 的Clock对象。可以创建一个监听器函数来检查信号是否每 n 秒发送一次。准确地说,假设您想process()在检测到信号后运行函数。scanned如果条形码被成功扫描,我们还声明一个变量来存储信息,并创建一个侦听器来检查信号是否已发送(因此检查scanned变量是否保持True)。以下代码示例scanned每 2 秒设置一次变量True以模拟扫描行为。

from kivy.app import App
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen

# Define constants and the scanned variable, for easy example
INTERVAL = 0.01
scanned = False


# Process method runs every 0.01 seconds, note the use of dt argument (explained in docs)
def process(dt):
    # Retrieve the global variable, for easy example
    global scanned
        
    # Check if scanned, note setting scanned to False once an item was scanned.
    # Here you can also check the uniqueness of the scanned barcode (to avoid having the same barcode processed many times)
    if scanned is True:
        print("Scanned! Processing the data and setting scanned to False.")
        scanned = False
    else:
        print("Not scanned yet!")


# Mimic scanning behaviour
def scan(dt):
    # Retrieve the global variable and set it to true 
    global scanned
    scanned = True


class Main(App):

    def __init__(self):
        super(Main, self).__init__()
        
        # Schedule the functions to be called every n seconds
        Clock.schedule_interval(process, INTERVAL)
        Clock.schedule_interval(scan, INTERVAL*200)

    def build(self):
        # Display screen with a single button for easy example
        scr = Screen()
        btn = Button(text="You can press me but nothing will happen!")
        scr.add_widget(btn)
        return scr


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

输出:

Not scanned yet!
.
.
.
Not scanned yet!
Scanned! Processing the data and setting scanned to False.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在不使用C / C ++循环的情况下切换n次?

如何在不运行应用程序的情况下切换日食工作区?

OSX El Captian:如何在没有Apple Magic Mouse鼠标或触控板的情况下切换屏幕

在切换情况下运行php语句

如何在不使用屏幕的情况下在tty之间切换?

如何在kivy中切换屏幕?

如何在没有重复效果的情况下延长滑动切换的延迟?

Outlook和AutoHotkey,如何在不切换字体的情况下输出Unicode符号?

SwiftUI如何在不切换标签栏的情况下刷新单个标签视图

Git:如何在不接触工作目录的情况下切换分支?

如何在不重新启动活动的情况下切换主题(夜间模式)?

如何在附加某些条件的情况下切换requestAnimationFrame函数?

如何在没有jQuery的情况下切换类?

如何在没有代码的情况下更改切换按钮颜色

如何在没有选择列表的情况下切换jQuery的盲目效果(默认)

如何在不重新加载Vagrant的情况下切换同步的文件夹?

如何在不切换的情况下查看所有teamviewer监视器?

如何在没有任何条件的情况下返回切换参数

如何在没有NavigationView或Popover的情况下切换视图?

如何在不使用transform:scale的情况下更改CSS切换开关的大小?

如何在不破坏原始导航栏布局的情况下添加切换栏?

如何在没有后退按钮的情况下切换Xamarin Shell中的页面?

如何在没有“隐藏的div”或“不显示”的情况下切换内容?

如何在没有 Javascript 或 Jquery 的情况下切换背景颜色?

如何在不切换到root的情况下以root用户身份运行smartctl?

如何在不按下按钮的情况下通过 c# 代码在内容控件之间切换?

如何在不切换plot()参数的情况下切换MATLAB图上x和y值的显示

在不丢失功能的情况下实时切换 Wordpress 主题

如何在不刷新整个页面的情况下在 PHP/WordPress 中执行切换功能 jQuery?