首页 > 解决方案 > 我正在尝试编写一个打印某些单词的 kivy gui,但它不起作用,我不知道为什么

问题描述

蟒蛇代码

这是代码


import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.core.window import Window
Window.clearcolor = (1, 1, 1, 1)


class MyGrid(Widget):
    name = ObjectProperty(None)
    words = ObjectProperty(None)

    def btn(self):

        self.name.text = txt

    def find_words(self):

        self.words.text = word
        list_words=open("word.txt").read().split()
        for x in word:
            for y in list_words:
                if x==y:
                    print(x)

基维代码

这是基维代码。我又做了两个类,roundbutton 和 mytextinput,它们只是设计上的改变


<RoundButton@Button>:
    background_color: 0,0,0,0
    canvas.before:
        Color:
            rgba: (.7,.7,.7,0.7) if self.state=='normal' else (0.5,1,1,1)
        RoundedRectangle:
            pos: self.pos
            size: self.size
            radius: [10,]


<MyTextInput@TextInput>:
    background_normal: "textinput.png"
    background_color: (0.82,0.96,0.92,1) if self.focus else (1,1,1,0.5)

<MyGrid>:

    txt:txt
    words:words

    FloatLayout:

        size: root.width, root.height


        MyTextInput:
            id: txt
            pos_hint: {"x":0.05, "top":0.8}
            size_hint: 0.63,0.75
        MyTextInput:
            id: words
            on_text: root.find_words()
            pos_hint: {"x": 0.7, "top":0.8}
            size_hint: 0.25,0.75
            text:"hello"

        RoundButton:
            text:"Submit"
            on_press: root.btn()
            pos_hint: {"x":0.05, "top":0.95}
            size_hint: 0.2,0.1

标签: pythonpython-3.xkivykivy-language

解决方案


self.words.text = word
list_words=open("word.txt").read().split()
for x in word:
    for y in list_words:
        if x==y:
            print(x)

在上面的代码中,在哪里word?你没有声明。这就是为什么你有这个错误。

那可能应该是:word = self.words.text而不是self.words.text = word?


推荐阅读