我在Kivy刚起步,当我导入kivy.uix.listview模块以创建ListItemButton时,出现一个错误,提示找不到该模块。
上周我对这一行代码没有任何问题,因此我尝试使用以下方法卸载并重新安装Kivy,但是没有运气:
python -m pip install-升级pip wheel setuptools
python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew --extra-index-url https://kivy.org/downloads/packages/simple/
python -m pip install kivy
我一直在关注对我有帮助的教程,但是他似乎没有这个问题。 来源:http://www.newthinktank.com/2016/10/kivy-tutorial-4/
# ------ studentdb.py ------
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.listview import ListItemButton
class StudentListButton(ListItemButton):
pass
class StudentDB(BoxLayout):
# Connects the value in the TextInput widget to these
# fields
first_name_text_input = ObjectProperty()
last_name_text_input = ObjectProperty()
student_list = ObjectProperty()
def submit_student(self):
# Get the student name from the TextInputs
student_name = self.first_name_text_input.text + " " + self.last_name_text_input.text
# Add the student to the ListView
self.student_list.adapter.data.extend([student_name])
# Reset the ListView
self.student_list._trigger_reset_populate()
def delete_student(self, *args):
# If a list item is selected
if self.student_list.adapter.selection:
# Get the text from the item selected
selection = self.student_list.adapter.selection[0].text
# Remove the matching item
self.student_list.adapter.data.remove(selection)
# Reset the ListView
self.student_list._trigger_reset_populate()
def replace_student(self, *args):
# If a list item is selected
if self.student_list.adapter.selection:
# Get the text from the item selected
selection = self.student_list.adapter.selection[0].text
# Remove the matching item
self.student_list.adapter.data.remove(selection)
# Get the student name from the TextInputs
student_name = self.first_name_text_input.text + " " + self.last_name_text_input.text
# Add the updated data to the list
self.student_list.adapter.data.extend([student_name])
# Reset the ListView
self.student_list._trigger_reset_populate()
class StudentDBApp(App):
def build(self):
return StudentDB()
dbApp = StudentDBApp()
dbApp.run()
# ------- studentdb.kv -------
StudentDB:
<StudentDB>:
orientation: "vertical"
first_name_text_input: first_name
last_name_text_input: last_name
student_list: students_list_view
padding: 10
spacing: 10
BoxLayout:
size_hint_y: None
height: "40dp"
Label:
text: "First Name"
TextInput:
id: first_name
Label:
text: "Last Name"
TextInput:
id: last_name
BoxLayout:
size_hint_y: None
height: "40dp"
Button:
text: "Submit"
size_hint_x: 15
on_press: root.submit_student()
Button:
text: "Delete"
size_hint_x: 15
on_press: root.delete_student()
Button:
text: "Replace"
size_hint_x: 15
on_press: root.replace_student()
# Define starting data and point to the ListItemButton
# in the Python code
ListView:
id: students_list_view
adapter:
ListAdapter(data=["Doug Smith"], cls=main.StudentListButton)
# ------ ERROR MESSAGE -----
Traceback (most recent call last):
File "studentdb.py", line 4, in <module>
from kivy.uix.listview import ListItemButton
ModuleNotFoundError: No module named 'kivy.uix.listview'
答案 0 :(得分:0)
不推荐使用listview
模块。如果您仍要使用它,
步骤1: 安装cython。
pip install Cython==0.29.9
步骤2: 安装 kivy 1.10.0
pip install kivy==1.10.0