建立Excel档案时使用Kivy应用程式 [英] Use Kivy app while excel file is being built

查看:141
本文介绍了建立Excel档案时使用Kivy应用程式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试创建一个Kivy应用程序,该应用程序允许用户控制和监视各种硬件组件.代码的一部分将构建并不断更新Excel工作表,该工作表将从硬件的通讯端口导入温度读数以及时间戳.到目前为止,我已经能够实现所有这些功能,但是在构建/更新Excel工作表时(即,在进行硬件测试时),我无法与Kivy应用程序进行交互,而使我无法使用该应用程序的功能在测试运行期间(例如暂停"或中止"按钮),直到不再更改工作表为止.所以我的问题是:是否可以同时使用Kivy应用程序导出到Excel文件?如果可以,怎么办?

So I am trying to create a Kivy app that allows a user to control and monitor various hardware components. Part of the code builds and continuously updates an Excel worksheet that imports temperature readings from the hardware's comm port, along with a time-stamp. I have been able to implement all of this so far, but I am unable to interact with the Kivy app while the Excel worksheet is being built/updated (i.e. while my hardware test is underway), and leaves me unable to use the app's features while the test is running (Such as the 'Pause' or 'Abort' buttons) until the worksheet is no longer being altered. So my question is: Is it possible to export to an Excel file while being able to simultaneously use the Kivy app? And if so, how?

这是设置Excel工作表的代码的一部分.预先谢谢你!

This is part of my code that sets up the Excel worksheet. Thank you in advance!

from kivy.app import App
from openpyxl import Workbook, load_workbook
import time

class HomeScreen(Screen):
    def build(self):
        return HomeScreen()

    def RunExcelFile(self):
        wb = Workbook()
        ws = wb.active
        a = 0
        i = 2
        while (a < 5):
            ws.cell('A1').value = 'Time'
            ws.cell('B1').value = 'Batch 1'
            ws.cell('C1').value = 'Batch 2'
            column = 'A'
            row = i
            time_cell = column + str(row)
            t = time.localtime()
            ws.cell(time_cell).value = time.asctime(t)
            a = (a + 1)
            i = (i + 1)
            time.sleep(1)
        wb.save("scatter.xlsx")

推荐答案

如果您正在做一些后台工作而不接触窗口小部件或属性,则可以使用threading模块而不会出现问题.否则,您将需要使用 @mainthread 装饰器或时钟.

If you are doing some background job without touching widgets or properties, you can use threading module without problems. Otherwise, you would need to use @mainthread decorator or Clock.

import time
import threading


class HomeScreen(Screen):

    def run_excel_file(self):

        def job():
            for i in xrange(5):
                print i
                time.sleep(1)
            print 'job done'

        threading.Thread(target=job).start()

这篇关于建立Excel档案时使用Kivy应用程式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆