我想用 kv 语言制作很多动态按钮.但是现在我不能......我现在将在此下显示源代码.
I want to make a lot of Buttons at dynamic in kv language. But now I cannot...... I will show now source under this.
BoxLayout:
orientation: 'vertical'
pos: root.pos
size: root.size
GridLayout:
rows: 2
spacing: 5
padding: 5
Button:
text: "X0"
on_press: root.X(0)
Button:
text: "X1"
on_press: root.X(1)
我想在code下做like
I want to make like under code
BoxLayout:
orientation: 'vertical'
pos: root.pos
size: root.size
GridLayout:
rows: 2
spacing:5
padding:5
for i
Button:
text: "X#{i}"
on_press: root.X(i)
我该怎么办?
这样的循环在 kv 语言中是不可能的,除了做一些肮脏的 hack.
Such loops aren't possible in kv language, other than doing some dirty hacks.
要动态创建一组按钮,请使用 ListView 或将它们添加到 py 文件中的循环中.
To create a set of buttons dynamically, either use ListView or add them in a loop inside a py file.
例子:
from functools import partial
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.add_buttons()
def add_buttons(self):
for i in xrange(5):
button = Button(
text='X' + str(i),
on_press=partial(self.X, number=i)
)
self.add_widget(button)
def X(self, caller, number):
print caller, number
这篇关于如何在 kv 语言中动态制作很多按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何制作一个在 Python 中提供角色的不和谐机器人How to make a discord bot that gives roles in Python?(如何制作一个在 Python 中提供角色的不和谐机器人?)
Discord 机器人没有响应命令Discord bot isn#39;t responding to commands(Discord 机器人没有响应命令)
你能得到“关于我"吗?Discord 机器人的功能?Can you Get the quot;About mequot; feature on Discord bot#39;s? (Discord.py)(你能得到“关于我吗?Discord 机器人的功能?(不和谐.py))
message.channel.id Discord PYmessage.channel.id Discord PY(message.channel.id Discord PY)
如何在 heroku 上托管我的 discord.py 机器人?How do I host my discord.py bot on heroku?(如何在 heroku 上托管我的 discord.py 机器人?)
discord.py - 自动更改角色颜色discord.py - Automaticaly Change an Role Color(discord.py - 自动更改角色颜色)