【问题标题】:How to display the content of a file on a screen using kivy and python?如何使用 kivy 和 python 在屏幕上显示文件的内容?
【发布时间】:2021-10-30 15:46:43
【问题描述】:

这是python中用于读取特定文件的代码。

类 Display2(屏幕):

def data_even(self):
    w_f = "abc.docs"
    try:
        with open(f"{w_f}") as w:
            f = w.readlines()
           
            self.ids.display.text = str(f)

    except FileNotFoundError:
        self.ids.display.text = "Not found, Sorry, the user has no data entered yet."

在 KIVY

KV=''' :

name: "Display2" 
GridLayout:
    cols: 1
    Label:
        id: display
        text: "I will be displaying your data"
        color: "#1e272e"
    MDFloatLayout:
        MDFillRoundFlatButton:
            pos_hint: {'center_x': 0.1, 'center_y': 0.1}
            id:data
            text: "Show Data"
            on_press: root.data_even()

        MDFillRoundFlatButton:
            pos_hint: {'center_x': 0.9, 'center_y': 0.1}
            text: "Close"
            on_release:
                app.root.current = "Evening"
                root.manager.transition.direction = "left"

'''

当我尝试运行上面的代码 sn-p 时,我无法看到文件中存在的文本,而只会出现一个黑色标签。我希望我能清楚地解释自己。谢谢!!

请注意,这不是完整的代码,只是程序的一些sn-ps

【问题讨论】:

  • 你能用 Kivy 展示任何有意义的东西吗?即使按照教程一步一步?您是否尝试逐渐更改教程代码,直到它与您的示例匹配?如果你这样做,它是否有助于你找到问题?
  • 另外:你认为标签应该是什么颜色?您的代码中是否有某些部分应该选择该颜色?在哪里?我假设您希望 "#1e272e" 指定文本的颜色。你觉得这个颜色应该是什么样子的?你认为它会在黑色背景下脱颖而出吗?
  • @KarlKnechtel 我能够显示的只是一条黑色,文本的颜色,即“#1e272e”是一种黑色阴影。我的背景是白色的,所以这种颜色应该可以。
  • 您是否考虑过将文本放在条带顶部以使其不引人注意的可能性?
  • @KarlKnechtel 因为我想显示文本而不是条带,所以我无法检查条带出现的原因。而且它也没有给出任何错误。我尝试了例外情况,它显示“未找到文件”,按预期显示在屏幕上,但不显示文件内容。

标签: python android kivy display kivymd


【解决方案1】:

如何使用kivy和python在屏幕上显示文件内容?

from kivy.app import App
from kivy.uix.label import Label

def get_text(file_path):
    try:
        with open(f"{file_path}") as w:
            f = w.readlines()
        return str(f)
    except:
        return "Not found, Sorry, the user has no data entered yet."

class MyApp(App):
    def build(self):
        return Label(text=get_text("abc.txt"), color="#ffc100")

MyApp().run()

【讨论】:

  • 当我将文件路径指定为 abc.docs 时出现错误,它显示“.. BuilderException: Parser: File "", line 274: ..."跨度>
  • 检查如何用 python 读取.docs 和你在上面的数据,尝试只写"Hello World !
【解决方案2】:

我的问题得到了答案!!!

我用 texinput 替换了标签,现在程序运行正常。

这是python中用于读取特定文件的代码。

类 Display2(屏幕):

def data_even(self): w_f = "abc.docs" 尝试: 使用 open(f"{w_f}") 作为 w: f = w.readlines()

        self.ids.display.text = str(f)

except FileNotFoundError:
    self.ids.display.text = "Not found, Sorry, the user has no data entered yet."

在 KIVY

KV= ''' :

名称:“显示器2”

网格布局:

cols: 1
**TextInput:**
    id: display
    text: "I will be displaying your data"
    color: "#1e272e"
MDFloatLayout:
    MDFillRoundFlatButton:
        pos_hint: {'center_x': 0.1, 'center_y': 0.1}
        id:data
        text: "Show Data"
        on_press: root.data_even()

    MDFillRoundFlatButton:
        pos_hint: {'center_x': 0.9, 'center_y': 0.1}
        text: "Close"
        on_release:
            app.root.current = "Evening"
            root.manager.transition.direction = "left"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    相关资源
    最近更新 更多