Python 2.7 Python:在boxlayout中添加按钮,只需按一下按钮即可滚动
好吧,这听起来可能让人困惑,但我想添加从mysql派生的数据,然后将其作为BoxLayout中可以滚动的按钮堆栈。 但是在这个问题上,没有错误,但是点击按钮就不起作用了Python 2.7 Python:在boxlayout中添加按钮,只需按一下按钮即可滚动,python-2.7,kivy,Python 2.7,Kivy,好吧,这听起来可能让人困惑,但我想添加从mysql派生的数据,然后将其作为BoxLayout中可以滚动的按钮堆栈。 但是在这个问题上,没有错误,但是点击按钮就不起作用了 def a_but(self): nodes = ObjectProperty() db = mysql.connector.connect(user='****', password='*******', host='localhost', db='***') curso
def a_but(self):
nodes = ObjectProperty()
db = mysql.connector.connect(user='****', password='*******', host='localhost', db='***')
cursor = db.cursor()
cursor.execute("SELECT Cos_name FROM costumerinfo WHERE Cos_name LIKE 'A%'")
result = cursor.fetchall()
if result:
for i in result:
self.nodes.add_widget(Button(text=i, height='200sp'))
self.nodes.size = (1, None)
这是kv文件
BoxLayout:
Button:
text: "A"
background_color: (1, 1, 1, 0.8)
on_release: root.a_but()
size_hint: 1, .5
#this is the part where the buttons should appear but in this case it doesnt
BoxLayout:
orientation:'vertical'
size_hint: 1, 1
ScrollView:
id: scrlv
bar_width: 10
BoxLayout:
id: nodes
orientation: 'vertical'
size_hint: 1, None
1) 删除以下内容,因为它将产生错误,ValueError:None不允许用于BoxLayout.height
self.nodes.size = (1, None)
2) 设置boxlayout的最小高度,以便有东西可以滚动
layout.bind(minimum_height=layout.setter('height'))
示例-Kivy、SQLite
main.py
试验电压(千伏)
#:kivy 1.10.0
:
节点:节点
盒子布局:
按钮:
案文:“A”
背景颜色:(1,1,1,0.8)
发布时:root.a\u but()
尺寸提示:1.5
盒子布局:
方向:'垂直'
大小提示:1,1
滚动视图:
id:scrlv
钢筋宽度:10
盒子布局:
id:节点
方向:“垂直”
大小提示:1,无
输出
感谢您抽出时间回答此问题。我想澄清一下,如果有机会,我会在每个按钮上添加一个on_release值,在按钮下方添加一个textinput,以显示有关按钮的信息,这是否可行。
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
import sqlite3 as lite
class RootWidget(BoxLayout):
nodes = ObjectProperty(None)
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
self.nodes.bind(minimum_height=self.nodes.setter('height'))
def a_but(self):
''' Retrieve from database'''
db = lite.connect('chinook.db')
cursor = db.cursor()
cursor.execute("SELECT FirstName FROM customers ORDER BY CustomerId ASC")
result = cursor.fetchall()
if result:
for row in result:
for col in row:
self.nodes.add_widget(Button(text=col, size_hint_y=None, height='200sp'))
class TestApp(App):
def build(self):
return RootWidget()
if __name__ == "__main__":
TestApp().run()
#:kivy 1.10.0
<RootWidget>:
nodes: nodes
BoxLayout:
Button:
text: "A"
background_color: (1, 1, 1, 0.8)
on_release: root.a_but()
size_hint: 1, .5
BoxLayout:
orientation:'vertical'
size_hint: 1, 1
ScrollView:
id: scrlv
bar_width: 10
BoxLayout:
id: nodes
orientation: 'vertical'
size_hint: 1, None