如何使用Kivy打印控制台输出

3

我一直在尝试学习如何使用Kivy python,并想知道如何与操作系统控制台/终端进行交互以运行命令和接收结果。到目前为止,我看到的教程只展示了如何创建小部件、按钮等。例如,如何在Kivy中显示运行命令"uname"的结果。使用以下代码,在“按下”时如何让它与操作系统进行交互,运行命令并将其显示回Kivy应用程序?是否有关于创建桌面应用程序/实用工具的教程?

    from kivy.app import App
    from kivy.uix.button import Button

    class tutap(App):
        def build(self):
            return Button(text="Press here")

    tutap().run()

更新: 以下是我想要实现的示例。这个示例使用了easygui模块:
    import subprocess
    from easygui import *
    msg= "what you want"
    out = subprocess.check_output("uname -a",shell=True)
    title = "My choice"
    choices=["kernel version","nothing"]
    choice=boolbox(msg,title,choices)
    if choice==1:
        msgbox(out)
    elif choice==0:
        msgbox("The End")

可能是这个问题的副本:从Python运行shell命令并捕获输出 - kitti
不仅仅是获取控制台输出..而是使用Kivy构建一个GUI应用程序,可以与控制台进行交互。 - mikie
如果你只是在学习kivy,那么这听起来像是一个相当复杂的项目。也许,subprocess模块可以帮助你?谷歌subprocess.call。 - Totem
1
Kivy教程没有展示这个原因是因为它与Kivy无关。你可以用任何正常的Python方式与操作系统交互,并以任何Kivy方式显示结果,但这些是任务的独立组件。正如其他人所说,如果你想要调用一个简单的命令并获得结果,你可以查看subprocess模块,在Kivy中,你可以像处理其他文本一样在标签中显示结果。 - inclement
如果您想运行一个命令并获取输出,那么这是重复的。如果您想在Kivy中构建类似终端的应用程序,那么这个问题对于SO来说太宽泛了。 - kitti
3个回答

1
我能想到的最简单的方法就是创建一个函数,像下面这样在文件的顶部写入文件:
def printlog(message):
    with open('./log.txt','a') as f: f.write(message+"\n")

在你的程序中,每当你想要打印输出时,只需简单地输入printlog("你想打印的内容!")
该文件将保存在与你的程序相同的文件夹中。理论上你可以在程序运行时打开它,但这更适用于事后分析。

0

我真的不太明白为什么要这样做,但如果你想要的话,你可以在单独的线程中调用App.run()方法,这样就不会阻塞命令行。

一个使用cmd模块的例子:

import logging
logging.getLogger("kivy").disabled = True

from kivy.app import App
from kivy.uix.listview import ListView

from cmd import Cmd
from threading import Thread

class MyApp(App):
    def build(self):
        self.lv = ListView()
        return self.lv  

    def update(self, line):
        self.lv.adapter.data.append(line)
        return "list updated"

class MyCmd(Cmd, object):
    def __init__(self, app, *args):
        super(HelloWorld, self).__init__(*args)
        self.app = app

    def do_EOF(self, line):
        self.app.stop()
        return True

    def default(self, line):
        ret = self.app.update(line)
        print(ret)

if __name__ == '__main__':
    app = MyApp()
    Thread(target=app.run).start()
    MyCmd(app).cmdloop()

我认为这个不是线程安全的。 - undefined

0

这是我获取控制台命令输出的方法。

首先是Python代码:

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.popup import Popup
    from kivy.properties import ObjectProperty
    from kivy.uix.label import Label 
    import subprocess

    class shellcommand(BoxLayout):
        first=ObjectProperty()
        second=ObjectProperty()
        third=ObjectProperty()

        def uname(self):
            v=subprocess.check_output("uname -a",shell=True)
            result=Popup(title="RESULT",content=Label(text="kernel is\n" + v))
            result.open()
        def date(self):
            d=subprocess.check_output("date",shell=True)
            res=Popup(title="DATE",content=Label(text="the date today is\n" + d))
            res.open()
        def last(self):
            last=subprocess.check_output("w",shell=True)
            ls=Popup(title="LOGIN",content=Label(text="logged in \n" + last))
            ls.open()


    class shellApp(App):
        def build(self):
            return shellcommand()

    shellApp().run()

然后是名为shellapp.kv的kivy文件

<shellcommand>:
orientation: "vertical"
first:one
second:two
third:three
canvas:
    Rectangle:
        source: "snaps.png" #location of any picture
        pos: self.pos
        size: self.size



BoxLayout:
    orientation: "horizontal"
    Button:
        id:one
        text: "UNAME"
        background_color: 0,0,0,1
        font_size:32
        size_hint:1,None
        on_press: root.uname()


    Button:
        id:two      
        text: "DATE"
        background_color: 1,1.5,0,1
        font_size:32
        size_hint:1,None
        on_press: root.date()


    Button:
        id: three
        text: "LOGGED IN"
        background_color: 1,0,0,1
        font_size:32
        size_hint: 1,None
        on_press: root.last()

如果有改进这段代码的方法,请告诉我。谢谢。


不要一行文档,一个都不要。 - Nearoo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接