我在Kivy中包装文本时遇到了实际问题。我正在寻找函数“SearchingModal”的结果,显示为列表:但结果可能占用多行。以前我看过用新的行字符来剪切单个文本字符串,但这看起来非常不优雅!
#!
#---------------------------------------------------------------------------------------#
# #
# Question Search Using Acquired Database #
# #
#---------------------------------------------------------------------------------------#
# Include sql database support; kivy modules.
import sqlite3
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.properties import ObjectProperty
from kivy.properties import ListProperty
from kivy.properties import StringProperty
from kivy.uix.scrollview import ScrollView
from kivy.uix.modalview import ModalView
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.listview import ListView
from kivy.uix.gridlayout import GridLayout
#---------------------------------------------------------------------------------------#
# Select Database
#---------------------------------------------------------------------------------------#
con = sqlite3.connect('test.db')
con.text_factory = str
#---------------------------------------------------------------------------------------#
class OrkivRoot(BoxLayout):
pass
#---------------------------------------------------------------------------------------#
class QuestionDetailsForm(AnchorLayout):
question_box = ObjectProperty()
answer_box = ObjectProperty()
def submitquestion(self):
search = self.question_box.text
app = Orkiv.get_running_app()
data = app.run_searchquestion(search) #data returns here
# x=0 #
# for x in range(len(data)): # This was to print results to terminal
# print data[x] #
# print "-" * 40 #
# x+=1 #
modal = SearchingModal(data) # comment these 2 lines to remove popup on submit
modal.open() #
def submitanswer(self):
search = self.answer_box.text
app = Orkiv.get_running_app()
data = app.run_searchanswer(search) #data returns here
#
modal = SearchingModal(data) # comment these 2 lines to remove popup on submit
modal.open() #
#------------------------------------------------------------#
#REDESIGN THE SEARCHING MODAL CLASS, USING THE KV LAYOUT FILE#
##############################################################
class SearchingModal(BoxLayout, ModalView, ListView):
def __init__ (self, data):
counter=0
super(SearchingModal, self).__init__(item_strings=[str(data[counter]) for counter in range(len(data))])
self.dismiss()
button = Button(text="Back To Search")
button.size_hint = (0.5, None)
button.height = "40dp"
button.bind(on_press=self.dismiss)
self.add_widget(button)
class Orkiv(App):
def run_searchquestion(self, search):
print 'searching for', search
cur = con.cursor()
cur.execute('SELECT title, content1 FROM content WHERE title LIKE ?', ('%'+search+'%',))
data = cur.fetchall()
#
return data #Returns the accumulated data to the variable calling the function
def run_searchanswer(self, search):
print 'searching for', search
cur = con.cursor()
cur.execute('SELECT title, content1 FROM content JOIN item on content._id=ref_id WHERE word=?', (search,))
data = cur.fetchall()
#
return data #Returns the accumulated data to the variable calling the function
Orkiv().run()
我还会包含.kv文件,因为没有任何麻烦,似乎能够让我的结果正确包装!
OrkivRoot:
<OrkivRoot>:
QuestionDetailsForm:
<QuestionDetailsForm>:
anchor_y: "center"
question_box: question_input
answer_box: answer_input
BoxLayout:
orientation: "vertical"
height: "350dp"
size_hint_y: None
GridLayout:
cols: 2
row_default_height: "40dp"
row_force_default: True
spacing: "50dp"
padding: "40dp"
Label:
text: "Question"
TextInput:
id: question_input
Label:
text: "Answer"
TextInput:
id: answer_input
Label:
text: " "
Button:
height: "30dp"
size_hint_y: None
text: "Submit Question"
on_press: root.submitquestion()
Label:
text: " "
Button:
size_hint_y: None
height: "30dp"
text: "Submit Answer"
on_press: root.submitanswer()
<SearchingModal>:
orientation: 'vertical'
BoxLayout:
size_hint_y: 0.005
ListView:
ListItemLabel:
font_size: 70
text_size: (root.width, None)
非常令人沮丧。我确定布局文件中的某些代码没有正确执行。例如,似乎无法更改字体大小。我实际上并不想改变字体大小,但想到找到一种方法可以让我能够使用“text size:”来包装文本。
这是我的第一个Kivy程序,也是我进入面向对象编程的第一步。任何建议都会受到赞赏,虽然生病尽力破译并有意义,但非常简单的开始将是一个很大的帮助!
我想,一旦我从数据库中获取信息,并打开了下一个屏幕,其他一切都将非常简单!我真的觉得它只是在.kv文件中稍微改变了一些东西。我只是不知道从哪里开始!!!
提前致谢!
答案 0 :(得分:2)
用&#34; .kv&#34;搞乱了很多。文件,以及主要的python程序,我提出了一个解决方案,采用了稍微不同的方法。
我省略了listview,并坚持使用一个由标签组成的简单模态弹出窗口。然后,我能够遵循this youtube video中的大部分建议,使标签包装和滚动有效。