python - 我正在尝试编写一个打印某些单词的 kivy gui,但它不起作用,我不知道为什么
问题描述
蟒蛇代码
这是代码
我正在尝试让代码打印仅在 words.txt 中的单词
但是脚本一直在说我没有定义“单词”
- 谁能告诉我问题是什么?
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
解决方案
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
?
推荐阅读
- java - 在java中操作文件
- reactjs - 如何解耦路由器和导航栏,使导航栏组件不包含组件
- list - groovy 是否有内置的方式来遍历/转换数据结构?
- netbeans-8 - Netbeans 8.0.2 更新后无法启动
- ubuntu - 运行 Airflow 时无法访问 localhost
- java - 尝试使用具有有效购买令牌的 Java Google Play Developer API v3 检索应用内购买信息时出现错误请求(无效值)
- javascript - SQLite (sql.js) 上的 DELETE 语句什么都不做
- csv - 在 java 中解析大型 CSV 文件的最快和最有效的方法
- node.js - 在 Express Web 应用程序中休息全面服务
- html - 用美汤提取网页内容